Showing posts with label Unmanaged Memory Operation. Show all posts
Showing posts with label Unmanaged Memory Operation. Show all posts

2009/07/30

C# - Passing Pointer / Pointer-to-Pointer as Input Parameter to DLL Methods

C Method Declaration

int TestMethod1(Struct1** handle);

int TestMethod2(Struct2* handle);




C# Dll Import

[DllImport("TestMethods.dll")]

private static extern int TestMethod1(ref IntPtr ptrHandle);

[DllImport("TestMethods.dll")]

private static extern int TestMethod2(IntPtr ptrHandle);




C# Calling Dll Methods

IntPtr ptrHandle = IntPtr.Zero;

ptrHandle = Marshal.AllocHGlobal(IntPtr.Size);

TestMethod1(ref ptrHandle);

TestMethod2(ptrHandle);

Marshal.FreeHGlobal(ptrHandle);

2009/07/07

C# - Reading Unmanaged Memory Using Pointer

Marshall Copy Method
Marshal.Copy(IntPtr source, byte[] destination, int startIndex, int endIndex);

C# - Setting & Zeroizing Unmanaged Memory Using Pointer

Unmanaged Memory Setting
Marshal.Copy(byte[] source, int startIndex, IntPtr ptrHandle, int endIndex);



Unmanaged Memory Zeroizing
// IntPtr ptrHandle;
byte[] zeroBytes = new byte[1024];
Marshal.Copy(zeroBytes , 0, ptrHandle, zeroBytes.Length - 1);

Labels

Google Analytics

Blog Archive

Followers