I am in the process of converting my Win32 p/invoke code to use SafeHandle
classes instead of the typical IntPtr
handles.
While everything works pretty well in DllImport
method signatures, I cannot for the life of me get them to work when marshaling Win32 structs (i.e. PROCESS_INFORMATION
).
// This works without issue.
[StructLayout(LayoutKind.Sequential)]
internal struct Win32ProcessInformation
{
public IntPtr ProcessHandle { get; set; }
public IntPtr ThreadHandle { get; set; }
public int ProcessId { get; set; }
public int ThreadId { get; set; }
}
// This does not work!
[StructLayout(LayoutKind.Sequential)]
internal struct Win32ProcessInformation
{
public ProcessSafeHandle ProcessHandle { get; set; }
public ThreadSafeHandle ThreadHandle { get; set; }
public int ProcessId { get; set; }
public int ThreadId { get; set; }
}
The ProcessSafeHandle
and ThreadSafeHandle
classes work just fine with methods like ReadProcessMemory
or WriteProcessMemory
, but I cannot use them in Win32 structs like above.
Am I missing some kind of annotation magic?