Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing structs between C# and C

Status
Not open for further replies.

cwinans

Programmer
Mar 26, 2001
102
0
0
US
I'm currently working on a language binding for the Y Window system. I'm attempting to pass a struct from my C# library to a C library. I'm very new to this but I have considerable experience with C#.

Here is the struct defined in C:
Code:
struct object
{
  int oid, cid;
  struct object *next;
  struct eventType *firstET;
}

struct eventType
{
  struct eventType *next;
  const char *name;
  void * (*eventCallBack) (struct object *, void *);
}

I'm only interested in the oid and cid fields of the object struct... the rest is unimportant to me (at least for the foreseeable future).


Most external functions that I'm calling from C# take the object struct as an argument and one returns an object struct.

The C functions I must use are:
Code:
struct object *YObject_CreateFromClassName(char *name);

void YObject_SetProperty(struct object *object, const char *name, const char *value);

char * YObject_GetProperty(struct object *object, const char *name);

int YSignal_Connect(struct object *object,
  const char *name, void *(*eventCallBack) (struct object *, void *));

What I need is help providing the struct to the C library and getting the struct from the C library. Has anyone had experience with this? Can anyone point me to a good source of information on this?

--
Casey Winans
 
Since the object struct has a pointer in it, you'll need to use the "unsafe" keyword in order to use them.

Wish I could have been of more help, but maybe this will get you started.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
I've found a work-around... at least I'm going to try it.

I will be using an IntPtr in place of trying to use the object struct. I can do this because I don't have to access anything within the object struct directly. The c library has a couple of functions to get the ObjectID and ClassID for me by passing the point of the object struct to them.


These are the C functions I must call:
Code:
struct object *YObject_CreateFromClassName(char *name);

void YObject_SetProperty(struct object *object, const char *name, const char *value);

char * YObject_GetProperty(struct object *object, const char *name);

int YSignal_Connect(struct object *object,
  const char *name, void *(*eventCallBack) (struct object *, void *));

Here are the equivalent C# wrapper functions:
Code:
  private delegate void CallbackSignal(IntPtr obj, string args);

  [DllImport("libYc.so")]
  private static extern void YSignal_Connect(IntPtr obj, string signal, CallbackSignal callback);

  [DllImport("libYc.so", EntryPoint="yGetCid")]
  private static extern int YObject_GetClassId(IntPtr obj);

  [DllImport("libYc.so", EntryPoint="yGetOid")]
  private static extern int YObject_GetObjectId(IntPtr obj);

  [DllImport("libYc.so")]
  private static extern void YObject_SetProperty(IntPtr obj, string name, string value);

  [DllImport("libYc.so")]
  private static extern string YObject_InvokeMethod(IntPtr obj, string name, int expectReturn);

  [DllImport("libYc.so")]
  private static extern string YObject_GetProperty(IntPtr obj, string name);

  [DllImport("libYc.so")]
  private static extern IntPtr YObject_CreateFromClassName(string className);

Thanks for the help.

--
Casey Winans
 
I can also do without having to resort to unsafe code. [wink]

--
Casey Winans
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top