how to define this?

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!
 
F

Fabian Schmied

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
 
Z

ZhangZQ

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
 
F

Fabian Schmied

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
 
Z

ZhangZQ

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
 

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