Groups | Blog | Home
all groups > dotnet interop > july 2003 >

dotnet interop : IntPtr value type via P/Invoke



Michel Gallant
7/5/2003 12:43:37 PM
Probably a simple marshaling question:

..NET value types passed as arguments via P/Invoke must be prototyped
and passed as ref to marshal back changes to the value type. Fine.

According to the .NET MSDN docs, IntPtr is a value type.
Why then when a prototype is configured (for example to get a returned pointer to a struct) as:
ManagedProto(.., IntPtr myptr, ...)

and called as ManagedProto(..., myptr, ...)

does the value successfully get changed?

(compared to using IntPtr to get a reference handle where ref IntPtr is
prototyped)?

Thanks,
- Mitch

Michel Gallant
7/6/2003 10:47:54 AM
Ok, I think I understand now (please correct if not right):

When an IntPtr myPtr argument is prototyped, one must actually
define the pointer value, usually by a memory allocation like:
myPtr = Marshal.AllocHGlobal(bBytesneeded);
When the win32 function is called and returns, the myPtr value
is the same (value type), but the called function changes the data
(buffer, structure ... whatever) pointed to by myPtr.

When used to GET a handle from a function, the function actually
returns the pointer value, so ref IntPtr is used.

- Mitch

[quoted text, click to view]

AddThis Social Bookmark Button