Kernel32 API, Module32First() and VB.NET

J

Jan Jeitziner

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.NullReferenceException: Object reference not set to an instance
of an object.
at SSAM_Sesam_AddIn.Sesam.Module32First(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_SNAPMODULE = &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 CreateToolhelpSnapshot Lib "kernel32" _
Alias "CreateToolhelp32Snapshot" _
(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 GetCurrentProcessId 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 = GetCurrentProcessId()
Debug.WriteLine(pID.ToString)
lhSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPMODULE, CLng(pID))

Me32.dwSize = Len(Me32)

lRet = Module32First(lhSnapShot, Me32)


Do While lRet
Try
If Me32.th32ProcessID = CLng(pID) Then
With Me32
iLen = InStr(.szExePath, Chr(0))
If iLen = 0 Then
sModule = CStr(.szExePath)
Else
sModule = Left(.szExePath, iLen - 1)
End If
Debug.WriteLine(sModule.ToString)
End With
End If
lRet = Module32Next(lhSnapShot, Me32)
Loop

CloseHandle(lhSnapShot)

Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try
End Function
 
A

Armin Zingler

Jan Jeitziner said:
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.

Why not use the System.Diagnostics.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:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
J

Jan Jeitziner

Why not use the System.Diagnostics.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.
Thank you! I could solve the problem with your suggestions!

Have a nice day!

Jan
 

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