H
Howard Kaikow
Given:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
private struct PROCESSENTRY32
{
public int dwSize;
public int cntUsage;
public int th32ProcessID;
public int th32DefaultHeapID;
public int th32ModuleID;
public int cntThreads;
public int th32ParentProcessID;
public int pcPriClassBase;
public int dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=MAX_PATH)]
public char szExeFile;
}
and
PROCESSENTRY32 pe32 = new PROCESSENTRY32();
I was using the following:
pe32.dwSize =sizeof(PROCESSENTRY32);
But that returned the wrong value 40 instead of the correct value 296, as
MAX_PATH is 260.
So I tried the following:
pe32.dwSize = Marshal.SizeOf(PROCESSENTRY32);
Which results in the following error at build time:
D:\Visual Basic
Code\API\EnumProcesses\Code-GetUsageCount\CsharpGetUsageCount\Form1.cs(340):
'CsharpGetUsageCount.Form1.MODULEENTRY32' denotes a 'class' where a
'variable' was expected
So, I tried
pe32.dwSize = Marshal.SizeOf(pe32);
Alas, that ended up causing an exception, with the message:
"Type PROCESSENTRY32 can not be marshaled as an unmanaged structure; no
meaningful size or offset can be computed."
What do I need to do to correct the problem?
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
private struct PROCESSENTRY32
{
public int dwSize;
public int cntUsage;
public int th32ProcessID;
public int th32DefaultHeapID;
public int th32ModuleID;
public int cntThreads;
public int th32ParentProcessID;
public int pcPriClassBase;
public int dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=MAX_PATH)]
public char szExeFile;
}
and
PROCESSENTRY32 pe32 = new PROCESSENTRY32();
I was using the following:
pe32.dwSize =sizeof(PROCESSENTRY32);
But that returned the wrong value 40 instead of the correct value 296, as
MAX_PATH is 260.
So I tried the following:
pe32.dwSize = Marshal.SizeOf(PROCESSENTRY32);
Which results in the following error at build time:
D:\Visual Basic
Code\API\EnumProcesses\Code-GetUsageCount\CsharpGetUsageCount\Form1.cs(340):
'CsharpGetUsageCount.Form1.MODULEENTRY32' denotes a 'class' where a
'variable' was expected
So, I tried
pe32.dwSize = Marshal.SizeOf(pe32);
Alas, that ended up causing an exception, with the message:
"Type PROCESSENTRY32 can not be marshaled as an unmanaged structure; no
meaningful size or offset can be computed."
What do I need to do to correct the problem?