how to define this?

  • Thread starter Thread starter ZhangZQ
  • Start date Start date
Z

ZhangZQ

if there is a C function in a DLL(My.dll) has this declaration,

struct MyStruct
{
int a;
int b;
};

int MyFunc(int param1, MyStruct** param2);


how to define this in C# ? if there is another C function like this int
MyFunc1(int param1, char** szRtn);, I can define it as

[DllImport("My.Dll")]
int MyFunc1(int param1, out string szRtn);


I want to know how to define the MyFunc?



Thanks!
 
if there is a C function in a DLL(My.dll) has this declaration,
struct MyStruct
{
int a;
int b;
};

int MyFunc(int param1, MyStruct** param2);


how to define this in C# ?

In C#, don't define MyStruct as a struct, define it as a class. This will
add an additional layer of indirection. Then,

[DllImport("My.dll")]
static extern int MyFunc(int param1, ref MyStruct param2);

should work, I think.

(If this is really a C function, you might need to use
CallingConvention.Cdecl the default is StdCall.)

Fabian
 
Thanks for your reply, but it doesn't work, I got this message, "Unhandled
Exception: System.InvalidCastException: Cannot cast from source type to
destination type."

I want to know the different definition of these 2 C functions in C#, int
MyFunc(int param1, MyStruct** param2); and int MyFunc(int param1, MyStruct*
param2);, what is the different declaration in C#?


Regards,
ZhangZQ



Fabian Schmied said:
if there is a C function in a DLL(My.dll) has this declaration,

struct MyStruct
{
int a;
int b;
};

int MyFunc(int param1, MyStruct** param2);


how to define this in C# ?

In C#, don't define MyStruct as a struct, define it as a class. This will
add an additional layer of indirection. Then,

[DllImport("My.dll")]
static extern int MyFunc(int param1, ref MyStruct param2);

should work, I think.

(If this is really a C function, you might need to use
CallingConvention.Cdecl the default is StdCall.)

Fabian
 
Thanks for your reply, but it doesn't work, I got this message,
"Unhandled
Exception: System.InvalidCastException: Cannot cast from source type to
destination type."

Hmm, I just tested it with a DLL I quickly wrote, and it seems to work
alright. Where exactly do you get this exception?
I want to know the different definition of these 2 C functions in C#, int
MyFunc(int param1, MyStruct** param2); and int MyFunc(int param1,
MyStruct*
param2);, what is the different declaration in C#?

int MyFunc(int param1, MyStruct** param2);
becomes:
(with MyStruct being a C# class)
static extern int MyFunc(int param1, ref MyStruct param2);
(I don't know of a way to do this with MyStruct being a C# struct)

int MyFunc(int param1, MyStruct* param2);
becomes:
(with MyStruct being a C# class)
static extern int MyFunc(int param1, MyStruct param2);
(with MyStruct being a C# struct)
static extern int MyFunc(int param1, ref MyStruct param2);

So, you see, "class" adds one indirection (*), "ref" also does.

Of course, if you don't need to use param2 in your C# program or if you
only pass it to another unmanaged function as an opaque handle, you can
also use IntPtr or ref IntPtr.

Fabian
 
Thank you very much!

It works now, I am using ref IntPtr, that param2 is only to be passed to the
unmanaged code.


Regards,
ZhangZQ
 
Back
Top