473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Using PInvoke to call functions has multiple behaviors

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
Nov 16 '05 #1
3 2652
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

"John Smith" <jo********@x-formation.com> wrote in message
news:uC**************@TK2MSFTNGP09.phx.gbl...
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

Nov 16 '05 #2
> 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
Nov 16 '05 #3
Hi, John.
You can cast any delegate type into Delegate or MulticastDelegate.

Ming Chen

"John Smith" <jo********@x-formation.com> wrote in message
news:Oq**************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tony Johansson | last post by:
Hello experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that sound strange. Here is the whole section: It says" Because...
3
by: Brett Robichaud | last post by:
I have created a simple background thread to make one pinvoke call into a DLL I've created. My Winforms app spawns the thread in the form load event then go about it's business. The problem is...
2
by: Bob Dankert | last post by:
I need to use functions through PInvoke which accept and return pointers to strings. What is the best way to get string pointers (IntPtr I am guessing?) to pass into these functions, and the best...
4
by: Rohith | last post by:
I need to import dlls that are present in the remote machine. Its a dll written in C that exposes methods. I want to import that dll in my C# application. But that dll is not present in the local...
4
by: yaron | last post by:
Hi, I want to use my unmanaged c++ class library from a c# client. my unmanaged c++ class library use polymorpism, is this polymorphism also exported to my c# client via PInvoke ? thanks.
6
by: greek_bill | last post by:
Hi, I'm interested in developing an application that needs to run on more than one operating system. Naturally, a lot of the code will be shared between the various OSs, with OS specific...
3
by: Beorne | last post by:
I have a propertary library dll (used to drive a device) that I call from my C# code. Calling the functions from C++ is really faster than calling them in C+ +. From C++ the call is almost...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
0
by: Aldev | last post by:
Hi There, I am quite new to PInvoke and calling COM functions from C#. I have managed so far to call some unmanaged functions ib my C++ dll from my C# code but at the moment I am having trouble...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.