Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / July 2004

Tip: Looking for answers? Try searching our database.

how to define this?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ZhangZQ - 21 Jul 2004 08:53 GMT
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!
Fabian Schmied - 22 Jul 2004 07:51 GMT
> if there is a C function in a DLL(My.dll) has this declaration,
>
[quoted text clipped - 7 lines]
>
> 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
ZhangZQ - 22 Jul 2004 08:37 GMT
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

> > if there is a C function in a DLL(My.dll) has this declaration,
> >
[quoted text clipped - 20 lines]
>
> Fabian
Fabian Schmied - 22 Jul 2004 09:48 GMT
> 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
ZhangZQ - 22 Jul 2004 10:12 GMT
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

> > Thanks for your reply, but it doesn't work, I got this message,
> > "Unhandled
[quoted text clipped - 29 lines]
>
> Fabian

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.