Marshal.SizeOf

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?
 
B

Bart Mermuys

Hi,
[Inline]

Howard Kaikow said:
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;
}

The last part should be:
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=MAX_PATH)]
public string szExeFile;

and

PROCESSENTRY32 pe32 = new PROCESSENTRY32();

I was using the following:

pe32.dwSize =sizeof(PROCESSENTRY32);

use Marshal.SizeOf
But that returned the wrong value 40 instead of the correct value 296, as
MAX_PATH is 260.

Yeah, but since you chosen Auto for charset, strings will be wide on nt
versions of windows. So the struct would be at least 260*2, this doesn't
have to be a problem though.
So I tried the following:

pe32.dwSize = Marshal.SizeOf(PROCESSENTRY32);

Should be :
pe32.dwSize = Marshal.SizeOf(typeof(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);

That should work now.

HTH,
greetings
 
H

Howard Kaikow

Thanx.

Your suggestion worked.
Now the following

Process32First(hProcessSnap, pe32)

Results in the error

//Object reference not set to an instance of an object.

Where I have:

[DllImport("kernel32", EntryPoint="Process32First", ExactSpelling=false,
CharSet=CharSet.Ansi, SetLastError=true)]
private static extern bool Process32First(int hSnapshot, PROCESSENTRY32
lppe);

For your information, the goal is to convert the code at

http://msdn.microsoft.com/library/d...e/taking_a_snapshot_and_viewing_processes.asp

I already have the code running in C++ .NET 2003

http://www.standards.com/OtherDownloads/GetUsageCounts/UnmanagedC++
GetUsageCount.zip

Goal is to get code working in C#, VB .NET and VB 6.
 
B

Bart Mermuys

Hi,

Howard Kaikow said:
Thanx.

Your suggestion worked.
Now the following

Process32First(hProcessSnap, pe32)

Results in the error

//Object reference not set to an instance of an object.

Where I have:

[DllImport("kernel32", EntryPoint="Process32First", ExactSpelling=false,
CharSet=CharSet.Ansi, SetLastError=true)]
private static extern bool Process32First(int hSnapshot, PROCESSENTRY32
lppe);


- use IntPtr for C/C++ HANDLE types
- since you declared PROCESSENTRY32 as a _struct_ and you need to pass a
pointer, you have to use to ref keyword :
- use the same CharSet on both the structure and the api

[DllImport("kernel32", CharSet=CharSet.Auto, SetLastError=true)]
private static extern bool Process32First(IntPtr hSnapshot, ref
PROCESSENTRY32 lppe);


Example:

[StructLayout(LayoutKind.Sequential)]
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=260)]
public string szExeFile;
}

const uint TH32CS_SNAPPROCESS = 0x00000002;

[DllImport("kernel32.dll",SetLastError=true)]
public static extern IntPtr CreateToolhelp32Snapshot(
uint dwFlags,
uint th32ProcessID );

[DllImport("kernel32.dll",SetLastError=true)]
public static extern bool Process32First(
IntPtr hSnapshot,
ref PROCESSENTRY32 lppe );

[DllImport("kernel32.dll",SetLastError=true)]
public static extern bool Process32Next(
IntPtr hSnapshot,
ref PROCESSENTRY32 lppe );

[DllImport("kernel32.dll",SetLastError=true)]
public static extern bool CloseHandle(
IntPtr hObject // handle to object);


/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
IntPtr p = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );

PROCESSENTRY32 pe = new PROCESSENTRY32();
pe.dwSize = Marshal.SizeOf(pe);
bool ret = Process32First( p, ref pe );

while ( ret )
{
// show current
Console.WriteLine( pe.szExeFile + "\t" + pe.th32ProcessID + "\t" +
pe.cntThreads );

// get next
pe.dwSize = Marshal.SizeOf(pe);
ret = Process32Next( p, ref pe );
}

CloseHandle(p);
Console.ReadLine();
}

HTH,
greetings
 
H

Howard Kaikow

Thanx.

The problem appears to have been the missing ref.

Note that the code did not work when I used IntPtr.

What's the best book to address the type of questions I've asked?

Previously, I had only:

C# Essentials
A Progranmmer's Introduction to C#

This past week. I ordered:

The MSFT Press Step by Step book (received yesterday).
Liberty's Programming C#..
Hejlsberg's book.

Again, thanx, now it's on to do a VB .NET version.
That task should be easier since the issue of the ref has been solved.

At some point, I will be posting an article at my web site, goving the code
for the C++, C#, VB .NET and VB 6 versions, and describing whether the
intended problem has been solved, but I do not want to digress now.
 
W

Willy Denoyette [MVP]

Howard Kaikow said:
Thanx.

Your suggestion worked.
Now the following

Process32First(hProcessSnap, pe32)

Results in the error

//Object reference not set to an instance of an object.

Where I have:

[DllImport("kernel32", EntryPoint="Process32First", ExactSpelling=false,
CharSet=CharSet.Ansi, SetLastError=true)]
private static extern bool Process32First(int hSnapshot, PROCESSENTRY32
lppe);

For your information, the goal is to convert the code at

http://msdn.microsoft.com/library/d...e/taking_a_snapshot_and_viewing_processes.asp

I already have the code running in C++ .NET 2003

http://www.standards.com/OtherDownloads/GetUsageCounts/UnmanagedC++
GetUsageCount.zip

Goal is to get code working in C#, VB .NET and VB 6.

Why convert and not simply use what's offered by the FCL, especially the
System.Diagnostics and System.Management namespace classes are just what you
need to achieve what you are looking for.

If you have to PInvoke that much in C#, it means you didn't check the FCL
for a managed solution and you missed the point of .NET where the FCL is
key, not the language you use to implement, or you might have chosen the
wrong language.

Willy.
 
H

Howard Kaikow

Why convert and not simply use what's offered by the FCL, especially the
System.Diagnostics and System.Management namespace classes are just what you
need to achieve what you are looking for.

If you have to PInvoke that much in C#, it means you didn't check the FCL
for a managed solution and you missed the point of .NET where the FCL is
key, not the language you use to implement, or you might have chosen the
wrong language.

Yes, but the goal is to do the deed in VB 6.

I was having difficulty with the task in VB 6, so I decided to try running
the C code example at
http://msdn.microsoft.com/library/d...e/taking_a_snapshot_and_viewing_processes.asp

I first put the code thru MSFT C++ V6 Learning edition.
Then I import that workspace into C++ .NET 2002, then into C++ .NET 2003.
Then to C#, and I just finished converting to VB .NET this evening.

I should be able to more easily implement a VB 6 version, using the VB .NET
version as a guide.

I do have a partial solution using the Framework stiff.
I found 11 of the 12 APIs I'm using are alleged to have Framework
equivalents.
Other is CloseHandle, have not yet looked for that equivalent.
 

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