[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

ICustomeMarshaler and ref p/invoke

Peter

11/19/2007 2:47:00 AM

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.


2 Answers

Christian Fröschlin

11/19/2007 8:32:00 AM

0

> 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

11/21/2007 3:55:00 AM

0

Christian,

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



Thanks

Peter.

"Christian Fröschlin" <froeschlin@mvtec.com> wrote in message
news:fhrhl4$j5s$1@svr7.m-online.net...
>> 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.