Kernel32 API, Module32First() and VB.NET

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

    Kernel32 API, Module32First() and VB.NET

    I want to find out all dll's a process have. I've found example code on
    the page http://www.freevbcode.com/ShowCode.asp?ID=295
    The problem is, the sample is written in vb6, but Application is written
    in vb.NET.
    I've rewritten the code for vb.net, but if I run the application, there
    is following runtime error message

    System.NullRefe renceException: Object reference not set to an instance
    of an object.
    at SSAM_Sesam_AddI n.Sesam.Module3 2First(Int64 hSnapShot,
    MODULEENTRY32 lpMe32)

    I don't know which object is meant, because both objects (hSnapShot,
    lpMe32) aren't Null.

    Thank you very much for your help.

    Jan Jeitziner


    My Code
    ......
    '<DECLARATION>
    Private Const TH32CS_SNAPMODU LE = &H8
    Private Structure MODULEENTRY32
    Dim dwSize As Long
    Dim th32ModuleID As Long
    Dim th32ProcessID As Long
    Dim GlblcntUsage As Long
    Dim ProccntUsage As Long
    Dim modBaseAddr As Long
    Dim modBaseSize As Long
    Dim hModule As Long
    Dim szModule As String
    Dim szExePath As String
    End Structure

    Private Declare Function CreateToolhelpS napshot Lib "kernel32" _
    Alias "CreateToolhelp 32Snapshot" _
    (ByVal lFlags As Long, ByVal lProcessID As Long) As Long

    Private Declare Function Module32First Lib "kernel32" _
    (ByVal hSnapShot As Long, ByVal lpMe32 As MODULEENTRY32) As Long

    Private Declare Function Module32Next Lib "kernel32" _
    (ByVal hSnapShot As Long, ByVal lpMe32 As MODULEENTRY32) As Long

    Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)

    Private Declare Function RtlMoveMemory Lib "kernel32" _
    (ByVal pDest As Long, ByVal pSource As Long, ByVal ByteLen As Long) As
    Long
    Private Declare Function GetCurrentProce ssId Lib "kernel32" () As Long

    ....
    '<CODE>

    Public Shared Function ListDLL()

    Dim Me32 As MODULEENTRY32
    Dim lRet As Long
    Dim lhSnapShot As Int64
    Dim pID As Long
    Dim iLen As Integer
    Dim sModule As String

    pID = GetCurrentProce ssId()
    Debug.WriteLine (pID.ToString)
    lhSnapShot = CreateToolhelpS napshot(TH32CS_ SNAPMODULE, CLng(pID))

    Me32.dwSize = Len(Me32)

    lRet = Module32First(l hSnapShot, Me32)


    Do While lRet
    Try
    If Me32.th32Proces sID = CLng(pID) Then
    With Me32
    iLen = InStr(.szExePat h, Chr(0))
    If iLen = 0 Then
    sModule = CStr(.szExePath )
    Else
    sModule = Left(.szExePath , iLen - 1)
    End If
    Debug.WriteLine (sModule.ToStri ng)
    End With
    End If
    lRet = Module32Next(lh SnapShot, Me32)
    Loop

    CloseHandle(lhS napShot)

    Catch ex As Exception
    Debug.WriteLine (ex.ToString)
    End Try
    End Function
  • Armin Zingler

    #2
    Re: Kernel32 API, Module32First() and VB.NET

    "Jan Jeitziner" <spacejay@bluew in.ch> schrieb[color=blue]
    > I want to find out all dll's a process have. I've found example code
    > on the page http://www.freevbcode.com/ShowCode.asp?ID=295
    > The problem is, the sample is written in vb6, but Application is
    > written in vb.NET.[/color]

    Why not use the System.Diagnost ics.Process class instead? There are some
    shared Get* methods to retrieve (a) Process object(s). Each Process has a
    Modules property returning the ProcessModules. .an so on.


    --
    Armin

    How to quote and why:



    Comment

    • Jan Jeitziner

      #3
      Re: Kernel32 API, Module32First() and VB.NET

      In article <4083b3cd$0$176 $9b622d9e@news. freenet.de>,
      az.nospam@freen et.de says...[color=blue]
      > "Jan Jeitziner" <spacejay@bluew in.ch> schrieb[color=green]
      > > I want to find out all dll's a process have. I've found example code
      > > on the page http://www.freevbcode.com/ShowCode.asp?ID=295
      > > The problem is, the sample is written in vb6, but Application is
      > > written in vb.NET.[/color]
      >
      > Why not use the System.Diagnost ics.Process class instead? There are some
      > shared Get* methods to retrieve (a) Process object(s). Each Process has a
      > Modules property returning the ProcessModules. .an so on.
      >[/color]
      Thank you! I could solve the problem with your suggestions!

      Have a nice day!

      Jan

      Comment

      Working...