CreateToolhelp32Snapshot

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TyBreaker

    #1

    CreateToolhelp32Snapshot

    I'm using the following declaration for the PROCESSENTRY32 data
    structure obtained from http://pinvoke.net:

    <StructLayout(L ayoutKind.Seque ntial)Public Structure PROCESSENTRY32
    Public dwSize As Int32
    Public cntUsage As Int32
    Public th32ProcessID As Int32
    Public th32DefaultHeap ID As Int32
    Public th32ModuleID As Int32
    Public cntThreads As Int32
    Public th32ParentProce ssID As Int32
    Public pcPriClassBase As Int32
    Public dwFlags As Int32
    <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=260) Public
    szExeFile As String
    End Structure

    But when I use it in my API call thus:

    Declare Function CreateToolhelp3 2Snapshot Lib "kernel32.d ll" (ByVal
    dwFlags As Long, ByVal th32ProcessID As Long) As Long
    Dim beforeSnapshot as Long

    beforeSnapshot = CreateToolhelp3 2Snapshot(TH32C S_SNAPPROCESS, 0)

    I get a run-time error in VB.Net saying "A call to PInvoke function
    'CreateToolhelp 32Snapshot' 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.
  • Mattias Sjögren

    #2
    Re: CreateToolhelp3 2Snapshot

    >Does this mean the declarations for the data structure or the API
    >function are incorrect?
    Yep. The parameters to CreateToolhelp3 2Snapshot 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.

    Comment

    • TyBreaker

      #3
      Re: CreateToolhelp3 2Snapshot

      Mattias Sjögren wrote:
      >Does this mean the declarations for the data structure or the API
      >function are incorrect?
      >
      Yep. The parameters to CreateToolhelp3 2Snapshot 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.

      Comment

      Working...