Home All Groups Group Topic Archive Search About

CreateToolhelp32Snapshot

Author
17 Sep 2006 12:48 AM
TyBreaker
I'm using the following declaration for the PROCESSENTRY32 data
structure obtained from http://pinvoke.net:

<StructLayout(LayoutKind.Sequential)> Public Structure PROCESSENTRY32
         Public dwSize As Int32
         Public cntUsage As Int32
         Public th32ProcessID As Int32
         Public th32DefaultHeapID As Int32
         Public th32ModuleID As Int32
         Public cntThreads As Int32
         Public th32ParentProcessID As Int32
         Public pcPriClassBase As Int32
         Public dwFlags As Int32
         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public
szExeFile As String
     End Structure

But when I use it in my API call thus:

Declare Function CreateToolhelp32Snapshot Lib "kernel32.dll" (ByVal
dwFlags As Long, ByVal th32ProcessID As Long) As Long
Dim beforeSnapshot as Long

beforeSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)

I get a run-time error in VB.Net saying "A call to PInvoke function
'CreateToolhelp32Snapshot' has unbalanced the stack. This is likely
because the managed PInvoke signature does not match the unmanaged
target signature. Check that the calling convention and parameters of
the PInvoke signature match the target unmanaged signature."

Does this mean the declarations for the data structure or the API
function are incorrect?
--
  ______     ___               __
/_  __/_ __/ _ )_______ ___ _/ /_____ ____
  / / / // / _  / __/ -_) _ `/  '_/ -_) __/
/_/  \_, /____/_/  \__/\_,_/_/\_\\__/_/
     /___/

There are 10 types of people in this world; those who understand the
binary numbering system and those who don't.

There's no place like 127.0.0.1.

ASCII a silly question, get a silly ANSI.

Author
17 Sep 2006 9:50 AM
Mattias Sjögren
>Does this mean the declarations for the data structure or the API
>function are incorrect?

Yep. The parameters to CreateToolhelp32Snapshot should be Integers and
the return type IntPtr.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
17 Sep 2006 9:49 PM
TyBreaker
Mattias Sjögren wrote:
>> Does this mean the declarations for the data structure or the API
>> function are incorrect?
>
> Yep. The parameters to CreateToolhelp32Snapshot should be Integers and
> the return type IntPtr.
>
>
> Mattias
>

Thank you, I finally stumbled across that through trial and error.  I've
got it working now but I hit a dead end in that I thought I was going to
be able to get a snapshot of CPU usage for each process with this
function but none of the fields in the PROCESSENTRY32 data structure
apparently record that info anyway :).

I already had a managed solution working so looks like I may as well
persevere with that instead.  My reading seemed to indicate that I'd
achieve better performance by using the snapshot but I guess that was
primarily for people who are only interested in enumerating basic info
about the processes.

--
  ______     ___               __
/_  __/_ __/ _ )_______ ___ _/ /_____ ____
  / / / // / _  / __/ -_) _ `/  '_/ -_) __/
/_/  \_, /____/_/  \__/\_,_/_/\_\\__/_/
     /___/

There are 10 types of people in this world; those who understand the
binary numbering system and those who don't.

There's no place like 127.0.0.1.

ASCII a silly question, get a silly ANSI.