Data type conversion of external function declaration written by c++

P

Peter

Hi,

I got a function in third party dll which its C++ declaration is like
this:

int __stdcall function1(const unsigned char *c1, int w, int h,
datatype *d1, int *n, int r = 0, const datatype2 *d2 = NULL, bool b1 =
false, bool b2 = true, char c = 0);

where datatype1 and datatype2 is another data structure I have
declared also.

I would like to convert it to C# by dllimport however I have tried so
many times and it won't work.

I think the problem maybe conversion of "const unsigned char *" to c#
equivalent, now I convert it to a IntPtr.

Or it is anything I need to consider for "const" parameter and
parameter with default value?

Thanks in advance.

Peter
 
P

Peter

Peter,

Thanks for your comment. Sorry for missing my try in C#:

[DllImport("3rparty.dll")]
public static extern int function1(
IntPtr c1,
int w,
int h,
IntPtr d1,
ref int n,
int r ,
IntPtr d2 ,
bool b1,
bool b2,
byte c);

Anything wrong with my type conversion?

Peter

I got a function in third party dll which its C++ declaration is like
this:
int __stdcall function1(const unsigned char *c1, int w, int h,
datatype *d1, int *n, int r = 0, const datatype2 *d2 = NULL, bool b1 =
false, bool b2 = true, char c = 0);
[...]
I think the problem maybe conversion of "const unsigned char *" to c#
equivalent, now I convert it to a IntPtr.
Or it is anything I need to consider for "const" parameter and
parameter with default value?

The "const" is irrelevant to C#.  There's no way to express that in the
language.

Passing an IntPtr for a C++ pointer type (e.g. "unsigned char *") is
fine, assuming that IntPtr actually points to something useful or
passing null (IntPtr.Zero) is allowed.  But you might prefer to pass a
byte[] instead, with the appropriate marshaling attribute.

Without knowing what you've tried, there's no way to point out the
mistake in what you've tried.

Pete
 
P

Peter

Not working at all... I will try to seek advice from the dll provider.

Thanks for your help, Pete :)

Cheers,
Peter

Thanks for your comment. Sorry for missing my try in C#:
         [DllImport("3rparty.dll")]
         public static extern int function1(
             IntPtr c1,
             int w,
             int h,
             IntPtr d1,
             ref int n,
             int r ,
             IntPtr d2 ,
             bool b1,
             bool b2,
             byte c);
Anything wrong with my type conversion?

Seems fine to me.  Does it work?

The IntPtr parameters are going to complicate your life as compared to
writing a more specific declaration.  But there's nothing fundamentally
wrong with them.

Pete
 

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