Using PInvoke to call functions has multiple behaviors

J

John Smith

I wrote some code in C in a dll which I would like to call from C#.

However I'm stuck because of the strongly typed behavior of C# which makes
limitations.

Here are the prototypes for two functions which I have trouble mapping:
int _SetOption(int nOption, void *pSetting);

void *_GetDataField(int nType, int *npLength);

For the first function depending on the variable nOption it accepts either a
string, function pointer (for callbacks) or an integer.
In C you can just typecast anything to (void *) but unfortunatly it's not
that easy in C#.

For the 2nd function it can return either a unsigned char * array or a char
* zero terminated string depending on nType. Again in C you can just
typecast it to anything.

Any suggestions how to map my functions? The worst case scenario is to
create one prototype for each possible behavior for both functions. This is
tiredsome and errorprone to create 20+ prototypes for the same function.

Thanks in advance for any advice :)
-- John
 
M

Ming Chen

Hi, Hohn.
In C a void* normally implies a simple memory block, whatever the content
is. So passing a byte[] should work in most cases. For example, you can
convert string to byte[] with Encoding classes, convert int or long to
byte[] with BitConverter class, etc. Using the resulting byte[] as the
second parameter should work just fine.
The only exception is function pointer. I'm not aware of any way to
convert a delegate to byte array. So you would probably have to have at
least two prototypes of the function, one accepts a byte[], the second
accepts a delegate. (BTW: the callback at C side would have to be stdcall
convention).

For the second function call, the function can always be marshalled as
returning an IntPtr, then you can use the set of functions provided by
Marshal class to interpret the content of the memory pointer returned. For
example, use Marshal.PtrToStructure to get a struct, PtrToString to get
strings and Copy to get the raw byte[]. Again, if function pointer is among
the potential return types, you may have some problem: you may not convert a
function pointer to a delegate through any mananged code.

Hope this helps.
Ming Chen
 
J

John Smith

In C a void* normally implies a simple memory block, whatever the
content
is. So passing a byte[] should work in most cases. For example, you can
convert string to byte[] with Encoding classes, convert int or long to
byte[] with BitConverter class, etc. Using the resulting byte[] as the
second parameter should work just fine.
The only exception is function pointer. I'm not aware of any way to
convert a delegate to byte array. So you would probably have to have at
least two prototypes of the function, one accepts a byte[], the second
accepts a delegate. (BTW: the callback at C side would have to be stdcall
convention).

Thanks for the advice. After working through you said I solved most of the
issues I had. Now I just got one issue left. I got 5 different function
pointer types. Is there some generic delegate type which I could typecast
each of these function pointers to, so I will avoid having more prototypes
then nessecary?
Fortunatly none of my functions return function pointers which you described
could cause further problems.

Thanks in advance.

-- John
 
M

Ming Chen

Hi, John.
You can cast any delegate type into Delegate or MulticastDelegate.

Ming Chen

John Smith said:
In C a void* normally implies a simple memory block, whatever the content
is. So passing a byte[] should work in most cases. For example, you can
convert string to byte[] with Encoding classes, convert int or long to
byte[] with BitConverter class, etc. Using the resulting byte[] as the
second parameter should work just fine.
The only exception is function pointer. I'm not aware of any way to
convert a delegate to byte array. So you would probably have to have at
least two prototypes of the function, one accepts a byte[], the second
accepts a delegate. (BTW: the callback at C side would have to be stdcall
convention).

Thanks for the advice. After working through you said I solved most of the
issues I had. Now I just got one issue left. I got 5 different function
pointer types. Is there some generic delegate type which I could typecast
each of these function pointers to, so I will avoid having more prototypes
then nessecary?
Fortunatly none of my functions return function pointers which you described
could cause further problems.

Thanks in advance.

-- John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top