PC Review


Reply
Thread Tools Rate Thread

how to define this?

 
 
ZhangZQ
Guest
Posts: n/a
 
      21st Jul 2004
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!


 
Reply With Quote
 
 
 
 
Fabian Schmied
Guest
Posts: n/a
 
      22nd Jul 2004
> 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
 
Reply With Quote
 
ZhangZQ
Guest
Posts: n/a
 
      22nd Jul 2004
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" <REMOVETHISfabianDOTschmied@fhs-hagenbergDOTacDOTat> ????
news(E-Mail Removed)...
> > 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



 
Reply With Quote
 
Fabian Schmied
Guest
Posts: n/a
 
      22nd Jul 2004
> 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
 
Reply With Quote
 
ZhangZQ
Guest
Posts: n/a
 
      22nd Jul 2004
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


"Fabian Schmied" <REMOVETHISfabianDOTschmied@fhs-hagenbergDOTacDOTat> ????
news(E-Mail Removed)...
> > 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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Define name David Microsoft Excel Programming 4 9th Mar 2006 09:19 PM
define name =?Utf-8?B?c3Vuc2hpbmU=?= Microsoft Excel Misc 1 29th Mar 2005 01:37 AM
Define Value Fernando Peixito Microsoft Access Macros 1 3rd Feb 2004 11:50 AM
#define A.M Microsoft C# .NET 3 13th Jan 2004 08:31 PM
How to define this.... Alex Sanchez. Microsoft C# .NET 1 21st Nov 2003 04:53 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:00 PM.