Pinvoke Signature needed,...

  • Thread starter Kerem Gümrükcü
  • Start date
K

Kerem Gümrükcü

Hi,

thats what the original looks like:

typedef struct _PROCESS_HEAP_ENTRY {
PVOID lpData;
DWORD cbData;
BYTE cbOverhead;
BYTE iRegionIndex;
WORD wFlags;
union {
struct {
HANDLE hMem;
DWORD dwReserved[3];
} Block;
struct {
DWORD dwCommittedSize;
DWORD dwUnCommittedSize;
LPVOID lpFirstBlock;
LPVOID lpLastBlock;
} Region;
} ;
}PROCESS_HEAP_ENTRY

Thats my signature translation:

[Flags]
public enum PROCESS_HEAP_ENTRY_WFLAGS : ushort
{
PROCESS_HEAP_ENTRY_BUSY = 0x0004,
PROCESS_HEAP_ENTRY_DDESHARE = 0x0020,
PROCESS_HEAP_ENTRY_MOVEABLE = 0x0010,
PROCESS_HEAP_REGION = 0x0001,
PROCESS_HEAP_UNCOMMITTED_RANGE = 0x0002,
}


[StructLayoutAttribute(LayoutKind.Explicit)]
public struct UNION_BLOCK
{
[FieldOffset(0)]
public STRUCT_BLOCK Block;

[FieldOffset(0)]
public STRUCT_REGION Region;
}

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct STRUCT_BLOCK
{
public IntPtr hMem;
[MarshalAsAttribute(UnmanagedType.ByValArray,SizeConst=3,
ArraySubType=UnmanagedType.U4)]
public uint[] dwReserved;
}

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct STRUCT_REGION
{
public uint dwCommittedSize;
public uint dwUnCommittedSize;
public IntPtr lpFirstBlock;
public IntPtr lpLastBlock;
}

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct PROCESS_HEAP_ENTRY
{
public IntPtr lpData;
public uint cbData;
public byte cbOverhead;
public byte iRegionIndex;
public PROCESS_HEAP_ENTRY_WFLAGS wFlags;
public UNION_BLOCK UnionBlock;
}


that is what i constructed, it compiles just fine, but throws a runtime
exception that either something is wrong with data alignement or
that the field has been overlapped by a non-object field. Thats pretty
unclear to me, why this happens,...can someone please give me the
right signature for this or at least tell me whats wrong on that
signature,...

Thanks in advance,...

Regards

Kerem


--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
 
P

Peter Duniho

Kerem said:
[...]
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct PROCESS_HEAP_ENTRY
{
public IntPtr lpData;
public uint cbData;
public byte cbOverhead;
public byte iRegionIndex;
public PROCESS_HEAP_ENTRY_WFLAGS wFlags;
public UNION_BLOCK UnionBlock;
}


that is what i constructed, it compiles just fine, but throws a runtime
exception that either something is wrong with data alignement or
that the field has been overlapped by a non-object field. Thats pretty
unclear to me, why this happens,...can someone please give me the
right signature for this or at least tell me whats wrong on that
signature,...

Shouldn't you be also specifying a Pack value of 1?

If that's not it, I'll suggest that you provide a concise-but-complete
code example that reliable demonstrates the problem, as well as an exact
description of the exception that occurs. You haven't even stated what
Win32 function you're trying to call, never mind how you've declared
your managed p/invoke for the function nor shown how you call it.

Pete
 
K

Kerem Gümrükcü

Hi Pete,
Shouldn't you be also specifying a Pack value of 1?

No, i dont see any need here, since you pecify explicit structure attribute
and sequential on the others, though i gave it a try, it also did not work.
If that's not it, I'll suggest that you provide a concise-but-complete
code example that reliable demonstrates the problem,

Thats all you need:

MessageBox.Show(Marshal.SizeOf(typeof(PROCESS_HEAP_ENTRY)).ToString());
as well as an exact description of the exception that occurs.

You wil see then for yourself,...

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
 
K

Kerem Gümrükcü

Hi,

the issue has been solved, here are the full pinvokes and signatures,
just for the case someone will face the same problem or needs the
same information:

[Flags]
public enum PROCESS_HEAP_ENTRY_WFLAGS : ushort
{
PROCESS_HEAP_ENTRY_BUSY = 0x0004,
PROCESS_HEAP_ENTRY_DDESHARE = 0x0020,
PROCESS_HEAP_ENTRY_MOVEABLE = 0x0010,
PROCESS_HEAP_REGION = 0x0001,
PROCESS_HEAP_UNCOMMITTED_RANGE = 0x0002,
}


[StructLayoutAttribute(LayoutKind.Explicit)]
public struct UNION_BLOCK
{
[FieldOffset(0)]
public STRUCT_BLOCK Block;

[FieldOffset(0)]
public STRUCT_REGION Region;
}

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct STRUCT_BLOCK
{
public IntPtr hMem;
public uint dwReserved1_1;
public uint dwReserved1_2;
public uint dwReserved1_3;
}

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct STRUCT_REGION
{
public uint dwCommittedSize;
public uint dwUnCommittedSize;
public IntPtr lpFirstBlock;
public IntPtr lpLastBlock;
}

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct PROCESS_HEAP_ENTRY
{
public IntPtr lpData;
public uint cbData;
public byte cbOverhead;
public byte iRegionIndex;
public PROCESS_HEAP_ENTRY_WFLAGS wFlags;
public UNION_BLOCK UnionBlock;
}

[Flags]
public enum HeapCompactFlags : uint
{
HEAP_NO_FLAGS_ZERO = 0x00000000,
HEAP_NO_SERIALIZE = 0x00000001
}

[Flags]
public enum HeapValidateFlags : uint
{
HEAP_NO_FLAGS_ZERO = 0x00000000,
HEAP_NO_SERIALIZE = 0x00000001
}

[Flags]
public enum HeapFreeFlags : uint
{
HEAP_NO_FLAGS_ZERO = 0x00000000,
HEAP_NO_SERIALIZE = 0x00000001
}

[Flags]
public enum HeapAllocFlags : uint
{
HEAP_NO_FLAGS_ZERO = 0x00000000,
HEAP_GENERATE_EXCEPTIONS = 0x00000004,
HEAP_NO_SERIALIZE = 0x00000001,
HEAP_ZERO_MEMORY = 0x00000008
}

[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetProcessHeap();

[DllImport("kernel32.dll", SetLastError = true)]
public static extern uint GetProcessHeaps(uint NumberOfHeaps,
IntPtr[] ProcessHeaps);

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool HeapWalk(IntPtr hHeap,
ref PROCESS_HEAP_ENTRY lpEntry);

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool HeapValidate(IntPtr hHeap,
HeapValidateFlags dwFlags,
IntPtr lpMem);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern int HeapCompact(
IntPtr hHeap,
HeapCompactFlags dwFlags);

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool HeapFree(int hHeap,
HeapFreeFlags dwFlags,
IntPtr lpMemory);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr HeapAlloc(IntPtr hHeap,
HeapAllocFlags dwFlags,
uint dwBytes);

Hope this helps someone some day,...

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
 

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