s-news
[Top] [All Lists]

[S] Mime-Version: 1.0

To: S-news@wubios.wustl.edu
Subject: [S] Mime-Version: 1.0
From: "Rob Reynolds" <reynolds@fincad.com>
Date: Sat, 11 Sep 1999 08:26:52 -0700
Sender: owner-s-news@wubios.wustl.edu
Hi, I have just recently started looking at a trial version of S-Plus. 
I'm interesting in calling functions from a financial analytics library
written in C.
I'm running into a little trouble and am looking for some help in figuring
out the right syntax.

I've generated wrappers for the library functions that should be compatible
with S-Plus.
Here is an example of the wrapper for the function I want to call.

void aaAccrual_days_sp (double * d_e, double * d_t, long * acc, long *
return_days)
{
LPXLOPER resultptr = NULL;

//  call dll
  resultptr = aaAcc_Days_dll((double) *d_e, (double) *d_t, (int) *acc);

  *return_days = (long) resultptr;
  return;

}

The wrapper function makes a call to the c function in the financial
analytics library.

I want to call this function directly from S-Plus and use a utility
function to extract the results
from the data structure. I am typecasting the pointer as long to allow it
to be passed back to
the S-Plus script. Here is the S-Plus script that I have written so far.

dll.load("fcsplus.dll","aaAccrual_days_sp" )
dll.load("fcsputil.dll", "GetTableElement")
x<-double(1)
y<-integer(1)
.C ("aaAccrual_days_sp", as.double(34000), as.double(34236), as.integer(1),
as.integer(y))
.C ("GetTableElement", as.integer(y), as.integer(1), as.integer(1),
as.double(x))

It seems to work until I get to the second .C function call. The second
call is designed
to get the value out of the data structure by passing the pointer to a
utility function.
Here is the code for the utility function.

void CCONV GetTableElement( long * spmem, long *sprow, long *spcol, double
* value )
{
int positionX, positionY, numofcols;
LPXLOPER Table; int row, col;

Table = (LPXLOPER) *spmem;
row = *sprow;
col = *spcol;

if ((Table->xltype != 64) && (Table->xltype != (64|xlbitDLLFree)))
  {
  if (Table->xltype == 1)
                {
                *value = Table->val.num;   /* if not an array then always 
return value */
                return;
                }
  else
                {
                *value = -1; 
    return;              /* if a bad pointer then return -1 */
                }
  }

/* if asking for data past end of table return -1 */
if (row > Table->val.array.rows)
        {
        *value = -1;
  return;
        }

if (col > Table->val.array.columns)
        {
        *value = -1;
  return;
        }

if ((row - 1) < 0)
        {
        *value = -1;
  return;
        }

if ((col - 1) < 0) 
        {
        *value = -1;
  return;
        }

positionX = row - 1;
positionY = col - 1;
numofcols = Table->val.array.columns;

*value = (Table->val.array.lparray+(positionX*numofcols)+positionY)->val.num;
return;
} /* GetTableElement */

I'll also include a definition of the data structure that I'm using in the 
C library.

typedef struct xloper
  {
   union
   {
    double num;
    LPSTR str;
    WORD bool;
    WORD err;
    struct
      {
      struct xloper far * lparray;
      WORD rows;
      WORD columns;
      } array;
   } val;
   WORD xltype;
  } XLOPER, FAR * LPXLOPER;







-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>