I have a third party Dll with a function which requires an IntPtr but the
data is in a byte array. I need to get the IntPtr for this byte array so I
can pass it into the function.

Thanks in advance,
~Logan

Re: Get IntPtr for a byte array by Mattias

Mattias
Sun Aug 31 06:47:34 CDT 2003

Logan,

>I found the answer,
>use GCHandle, code sample follows
>----------------------------------------------------------------------------
>-------------
>//b is the byte array
>//ToSend is an IntPtr that is set to the address of b
>GCHandle gch = GCHandle.Alloc(b,GCHandleType.Pinned);
>ToSend = gch.AddrOfPinnedObject();
>----------------------------------------------------------------------------
>-------------

Just don't forget to Free the GCHandle when you're done with it.

Another possible solution is to change the DLL function declaration to
take a byte array parameter instead of an IntPtr.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Re: Get IntPtr for a byte array by Oscar

Oscar
Sun Aug 31 14:37:06 CDT 2003

try the fixed statement

fixed (byte *p=ByteArray) {
IntPtr MyIntPtr= (IntPtr)p;
SomeDllMethod(MyIntPtr);
}

don't forget to use /unsafe when compiling

Oscar

"Logan McKinley" <logan@globalweb.net> wrote in message
news:ucy6S$jbDHA.656@tk2msftngp13.phx.gbl...
> I have a third party Dll with a function which requires an IntPtr but the
> data is in a byte array. I need to get the IntPtr for this byte array so
I
> can pass it into the function.
>
> Thanks in advance,
> ~Logan
>
>