Get IntPtr for a byte array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Logan McKinley

    #1

    Get IntPtr for a byte array

    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


  • Mattias Sjögren

    #2
    Re: Get IntPtr for a byte array

    Logan,
    [color=blue]
    >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.AddrOfPinne dObject();
    >----------------------------------------------------------------------------
    >-------------[/color]

    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

    Please reply only to the newsgroup.

    Comment

    • Oscar Papel

      #3
      Re: Get IntPtr for a byte array

      try the fixed statement

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

      don't forget to use /unsafe when compiling

      Oscar

      "Logan McKinley" <logan@globalwe b.net> wrote in message
      news:ucy6S$jbDH A.656@tk2msftng p13.phx.gbl...[color=blue]
      > 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[/color]
      I[color=blue]
      > can pass it into the function.
      >
      > Thanks in advance,
      > ~Logan
      >
      >[/color]


      Comment

      Working...