Search code examples
c#interopintptristream

Getting an IntPtr to a ulong variable in C#


I need to pass an IntPtr to IStream.Read, and the IntPtr should point to a ulong variable. How do I get this IntPtr that points to my ulong variable?


Solution

  • The best way is to change the IStream definition:

    void Read([Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] byte[] pv,
              int cb, /*IntPtr*/ ref int pcbRead);
    

    Then you can write

    int pcbRead = 0;
    Read(..., ref pcbRead);