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 / Interop / November 2007

Tip: Looking for answers? Try searching our database.

ICustomeMarshaler and ref p/invoke

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter - 19 Nov 2007 02:47 GMT
I'm using C#  in VS2005 and having a problem when I implement a custom
marshaler and I call a reference type by ref.

Any help greatly appriciated.

I'm using ICustomMarshaler because of the structure contents I'm trying to
marshal. The sample code that follows doesn't show that structure, but I do
need ICustomMarshaler.

My c# code is...

MyClass myClass;        // MyClass is a class
CallNativeCode(myClass);

[DllImport(....)]
protected static extern int CallNativeCode([In, Out,
MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef....] MyClass c1);

Now that works OK when passing data to my native code, my problem is that
the native code can update the marshaled class (hence the In, Out ) but I
don't seem to be able to get the values back into my c# code.  The obvious
thing to do is change the code so that...

MyClass myClass;
CallNativeCode(ref myClass);

[DllImport(....)]
protected static extern int CallNativeCode([In, Out,
MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef....] ref MyClass
c1);

However this caused me problems. First on calling my natvie code, I see
there is another level of indirection. I can't change the native code, so
ref is no good to me!

It seems to me that I need to do the In, Out in the MarshalAs to get things
back from the native side, then the ref to get that data back into the main
line code, however the ref is also being used during the managed to native
marshal.

Am I missing something?

Thanks for any suggestions.

Peter.
Christian Fröschlin - 19 Nov 2007 08:31 GMT
> However this caused me problems. First on calling my natvie code, I see
> there is another level of indirection. I can't change the native code, so
> ref is no good to me!

One possible solution would be to write a native wrapper function,
another to declare the DllImport method parameter as IntPtr and
write a wrapper function on the .NET side which maps the state of
myclass to a value type struct or unmanaged memory block which has
the same layout as expected by the native code, passes the pointer
and maps the state back into myclass.
Peter - 21 Nov 2007 03:55 GMT
Christian,

Thanks for the suggestion, I've tried that and it seems to be doing what I
want

Thanks

Peter.

>> However this caused me problems. First on calling my natvie code, I see
>> there is another level of indirection. I can't change the native code, so
[quoted text clipped - 6 lines]
> the same layout as expected by the native code, passes the pointer
> and maps the state back into myclass.

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.