Question about StructureToPtr

A

afatdog

[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public ulong cbData;
public IntPtr lpData;
}

public struct AAA
{
public byte No; // ǹºÅ
}

COPYDATASTRUCT CopyDataStruct=new COPYDATASTRUCT();
AAA aaa=new AAA();
aaa.No =1;
CopyDataStruct.cbData=(ulong)Marshal.SizeOf(AAA.GetType());
Marshal.StructureToPtr(QueryPumpCount, CopyDataStruct.lpData, true); // <-
Error : Value cannot be null. Parameter name: ptr

SendMessage(...);



Why?
 
M

Mattias Sjögren

[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public ulong cbData;
public IntPtr lpData;
}

cbData should be a 32-bit uint.

Marshal.StructureToPtr(QueryPumpCount, CopyDataStruct.lpData, true); // <-
Error : Value cannot be null. Parameter name: ptr [...]
Why?

Because CoyDataStruct is zero, not a valid pointer. Allocate some
memory with Marshal.Alloc*



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top