c-ffi 
Author Message
 c-ffi

In the previous post, I mentioned mcadincl.h
file for mathcad.

There are some special quesitons:
1. typedef const char* const q1;
2. typedef const char* q2;
3. typedef char* const q3;
4. typedef char** q41;
   typedef double ** q42;
How to define c-ffi?

This is the original mcadincl.h.
(I hope I can post it here.)

#ifndef _MCADINCL_H_
#define _MCADINCL_H_

#include <windows.h>

#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus

// complex scalar type
typedef struct tagCOMPLEXSCALAR {
    double real;
    double imag;

Quote:
} COMPLEXSCALAR;

// this is the complex scalar type received from mathcad
typedef const COMPLEXSCALAR * const LPCCOMPLEXSCALAR;
// this is the complex scalar type that should be returned to mathcad
typedef COMPLEXSCALAR * const LPCOMPLEXSCALAR;

// complex array type
typedef struct tagCOMPLEXARRAY  {
    unsigned int rows;
    unsigned int cols;
    double **hReal; // hReal[cols][rows],  == NULL when the real part
is zero
    double **hImag; // hImag[cols][rows],  == NULL when the imaginary
part is zero

Quote:
} COMPLEXARRAY;

// this is the complex array type received from mathcad
typedef const COMPLEXARRAY * const LPCCOMPLEXARRAY;    
// this is the complex array type that should be returned to mathcad
typedef COMPLEXARRAY * const LPCOMPLEXARRAY;

// types to be used in declaration of the function's
// arguments and of the return value
#define COMPLEX_SCALAR  1
#define COMPLEX_ARRAY   2    

// use this structure to create a function
#define MAX_ARGS        10

typedef LRESULT (* LPCFUNCTION ) ( void * const, const void * const,
... );    

typedef struct tagFUNCTIONINFO {
    char *  lpstrName;
    char *  lpstrParameters;
    char *  lpstrDescription;
    LPCFUNCTION lpfnMyCFunction;
    long unsigned int returnType;
    unsigned int nArgs;
    long unsigned int argType[MAX_ARGS];

Quote:
} FUNCTIONINFO;

const void * CreateUserFunction( HINSTANCE, FUNCTIONINFO * );

BOOL CreateUserErrorMessageTable(   HINSTANCE,
                                    unsigned int nErrorMessages,
                                    char  * ErrorMessageTable[] );

// memory management routines
char * MathcadAllocate( unsigned int size );
void MathcadFree( char * address );

// array allocation -- should be used to allocate
// return array
BOOL    MathcadArrayAllocate(   COMPLEXARRAY * const,
                                unsigned int rows,  
                                unsigned int cols,
                                BOOL allocateReal,
                                BOOL allocateImag );
// should be used to free ( in case of an error )
// Mathcad allocated return array
void MathcadArrayFree( COMPLEXARRAY * const );

// this routine can be used to find out
// whether the user has attempted to interrupt
// Mathcad
// this routine slows down the execution -- so use judiciously
BOOL isUserInterrupted( void );

#ifdef __cplusplus

Quote:
}

#endif // __cplusplus

#endif // _MCADINCL_H_  
// end of mcadincl.h

// This is the dylan file I wrote.  Of course, it has a lot of errors.

// mcadincl.dylan
define C-struct <COMPLEXSCALAR>
  slot real :: <C-double>;
  slot imag :: <C-double>;
  pointer-type-name: <COMPLEXSCALAR*>
end C-struct <COMPLEXSCALAR>;    

define constant <LPCCOMPLEXSCALAR> = <COMPLEXSCALAR*>;
define constant <LPCOMPLEXSCALAR> = <COMPLEXSCALAR*>;

define C-struct <COMPLEXARRAY>
  slot rows :: <C-unsigned-int>;
  slot cols :: <C-unsigned-int>;
  slot hReal :: <C-double**>;
  slot hImag :: <C-double**>;
  pointer-type-name: <COMPLEXARRAY*>;
end C-struct <COMPLEXARRAY>;

define constant <LPCCOMPLEXARRAY> = <COMPLEXARRAY*>;
define constant <LPCOMPLEXARRAY> = <COMPLEXARRAY*>;

define constant $complex-scalar = 1;

define constant $complex-array = 2;

define constant $max-args = 10;

define C-struct <FUNCTIONINFO>
  slot lpstrName :: <C-char*>;
  slot lpstrParameters :: <C-char*>;
  slot lpstrDescription :: <C-char*>;
  slot lpfnMYCFunction :: <>;
  slot returnType :: <C-unsigned-long>;
  slot nArgs :: <C-unsigned-int>;
  slot

define C-function CreateUserFunction
   parameter hDLL :: <HINSTANCE>;
   parameter functionInfo :: <FUNCTIONINFO*>;
   result functionPointer :: <C-void*>;
   c-name: "CreateUserFunction";
end C-function CreateUserFunction;

define C-function CreateUserErrorMessageTable
  parameter hDLL :: <HINSTANCE>;
  parameter n :: <C-unsigned-int>;
  parameter ErrorMessageTable :: <C-char*???>;
  result value :: <boolean>;
  cname: "CreateUserErrorMessageTable";
end C-function CreateUserErrorMessageTable;

define C-function MathcadAllocate
  parameter size :: <C-unsigned-int>;
  result :: <C-char*>;
  c-name: "MathcadAllocate";
end C-function MathcadAllocate;

define C-function MathcadFree
  parameter address :: <C-char*>;
  result :: <C-void>;
  c-name: "MathcadFree";
end C-function MathcadFree;

define C-function MathcadArrayAllocate
  output parameter out :: <LPCCOMPLEXARRAY>;
  parameter rows :: <C-unsigned-int>;
  parameter cols :: <C-unsigned-int>;
  parameter allocateReal :: <boolean>;
  parameter allocateImag :: <boolean>;
  result :: <boolean>;
  c-name: "MathcadArrayAllocate";
end C-function MathcadArrayAllocate;

define C-function MathcadArrayFree
  parameter array :: <LPCCOMPLEXARRAY>;
  result :: <C-void>;
  c-name: "MathcadArrayFree";
end C-function MathcadArrayFree;

//end of file



Tue, 18 Sep 2001 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. FFI survey for smalltalk

2. portable FFI representation

3. Calling Haskell function from C (FFI) causes seg-fault (GHC,linux)

4. FFI survey

5. Monads, GreenCard and ffi

6. Fun-O Win32 FFI Question

7. c-ffi question involving c-struct

8. C FFI question

9. FFI question #2: Getting the address of a C function

10. FFI question: Setting array elements

11. FFI questions

12. Assorted C-FFI goodies

 

 
Powered by phpBB® Forum Software