I declared the function Process32FirstW
and the structure PROCESSENTRY32W
like this:
[DllImport("KERNEL32.DLL", CallingConvention = CallingConvention.StdCall, EntryPoint = "Process32FirstW")]
private static extern bool Process32FirstW (IntPtr hSnapshot, ref ProcessEntry pProcessEntry);
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Size = 568)]
internal struct ProcessEntry {
[FieldOffset(0)] public int Size;
[FieldOffset(8)] public int ProcessId;
[FieldOffset(32)] public int ParentProcessID;
[FieldOffset(44), MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string ExeFile;
}
When calling Process32FirstW
(with a 64-bit process), I always get a TypeLoadException
saying
The type
ProcessEntry
couldn't be loaded, because the object field at offset 44 is aligned wrong or is overlapped by another field, which isn't an object field.
I also tried using char[]
instead of string
for ProcessEntry.ExeFile
and using Pack=4
and Pack=8
in the structure's StructLayoutAttribute
. I always set ProcessEntry.Size
to 568 and I copied the offset data from a C++ program (64-bit build):
typedef unsigned long long ulong;
PROCESSENTRY32W entry;
wcout << sizeof(PROCESSENTRY32W) << endl; // 568
wcout << (ulong)&entry.dwSize - (ulong)&entry << endl; // 0
wcout << (ulong)&entry.th32ProcessID - (ulong)&entry << endl; // 8
wcout << (ulong)&entry.th32ParentProcessID - (ulong)&entry << endl; // 32
wcout << (ulong)&entry.szExeFile - (ulong)&entry << endl; // 44
I can't figure out, what is going wrong, so how to declare PROCESSENTRY32W
in C# for a 64-bit application? Do I have to use C++/CLI or am I simply doing something wrong here?
EDIT: Running this code as a 64-bit program works perfectly fine for me
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32W entry;
entry.dwSize = sizeof(PROCESSENTRY32W);
if (Process32FirstW(hSnapshot, &entry)) {
do {
// Do stuff
} while (Process32NextW(hSnapshot, &entry));
}
CloseHandle(hSnapshot);
ULONG_PTR
is differently sized on 32 and 64 bit systems, wrote up a full answer. – Macmacabre