I was interested to enumerate all running Exe and following code does it.
But I found, that under NT/2000 it's possible to retrieve also DLL's list.
I thought that this knowledge can be useful, for example, to detect "gifts" in own process.
Somebody knows how to retrieve the DLL's list under 9x ?
(it's not very important and I already solved initial task, but interesting ...)
------------------
[This message has been edited by Semen Matusovski (edited August 01, 2000).]
Code:
' Based on HOWTO: List Running Processes ID: Q187913 #Compile Exe #Dim All #Register None #Include "Win32Api.Inc" Global hDlg As Long %TH32CS_SNAPPROCESS = &H2& Type PROCESSENTRY32 dwSize As Dword cntUsage As Dword th32ProcessID As Dword ' This process th32DefaultHeapID As Long Ptr th32ModuleID As Dword ' Associated exe cntThreads As Dword th32ParentProcessID As Dword ' This process's parent process pcPriClassBase As Long ' Base priority of process threads dwFlags As Dword szExeFile As Asciiz * %MAX_PATH End Type ' For 9x Declare Function Process32First _ (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long Declare Function Process32Next _ (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long Declare Function CreateToolhelp32Snapshot _ (ByVal dwFlags As Dword, ByVal th32ProcessID As Dword) As Long ' For Nt/2000 Declare Function EnumProcesses _ (lpidProcess As Dword, ByVal cb As Dword, cbNeeded As Dword) As Long Declare Function GetModuleFileNameEx _ (ByVal hProcess As Dword, ByVal hModule As Dword, ModuleName As Asciiz, ByVal nSize As Dword) As Dword Declare Function EnumProcessModules _ (ByVal hProcess As Dword, ByRef lphModule As Dword, ByVal cb As Dword, cbNeeded As Dword) As Long Sub EnumModules Dim os As OSVERSIONINFO, Proc As PROCESSENTRY32 Dim cb As Dword, cbNeeded As Dword Dim i As Long, j As Long, nModules As Long, nProcesses As Long, hProcess As Dword, lResult As Long Dim hKernel32 As Dword, hCreateToolhelp32Snapshot As Dword, hProcess32Next As Dword, hProcess32First As Dword Dim hPsApiDll As Dword, hEnumProcesses As Dword, hGetModuleFileNameEx As Dword, hEnumProcessModules As Dword os.dwOSVersionInfoSize = SizeOf(os) GetVersionEx ByVal VarPtr(os) ListBox Reset hDlg, 101 If IsFalse(os.dwPlatformId = %VER_PLATFORM_WIN32_NT) Then ' Windows 95/98 hKernel32 = GetModuleHandle("kernel32.dll") hCreateToolhelp32Snapshot = GetProcAddress(hKernel32, "CreateToolhelp32Snapshot") hProcess32Next = GetProcAddress(hKernel32, "Process32Next") hProcess32First = GetProcAddress(hKernel32, "Process32First") Call Dword hCreateToolhelp32Snapshot Using _ CreateToolhelp32Snapshot (%TH32CS_SNAPPROCESS, 0&) To i If i = 0 Then Exit Sub Proc.dwSize = SizeOf(Proc) Call Dword hProcess32First Using Process32First (i, Proc) To j While j ListBox Add hDlg, 101, Proc.szExeFile Call Dword hProcess32Next Using Process32Next (i, Proc) To j Wend Else ' Windows NT hPsApiDll = GetModuleHandle("psApi.dll") If hPsApiDll = 0 Then hPsApiDll = LoadLibrary("psApi.dll") hEnumProcesses = GetProcAddress(hPsApiDll, "EnumProcesses") hGetModuleFileNameEx = GetProcAddress(hPsApiDll, "GetModuleFileNameExA") hEnumProcessModules = GetProcAddress(hPsApiDll, "EnumProcessModules") cb = 100 Do ReDim ProcessIDs(1 To cb / 4) As Dword Call Dword hEnumProcesses Using EnumProcesses (ProcessIDs(1), cb, cbNeeded) To lResult If cb > cbNeeded Then Exit Do cb = cb * 2 Loop nProcesses = cbNeeded / 4 For i = 1 To nProcesses hProcess = OpenProcess(%PROCESS_QUERY_INFORMATION Or %PROCESS_VM_READ, %False, ProcessIDs(i)) If hProcess <> 0 Then cb = 100 Do ReDim Modules(1 To cb / 4) As Dword Call Dword hEnumProcessModules Using _ EnumProcessModules (hProcess, Modules(1), cb, cbNeeded) To lResult If lResult = 0 Then cbNeeded = 0: Exit Do If cb > cbNeeded Then Exit Do Else cb = cb * 2 Loop nModules = cbNeeded / 4 For j = 1 To nModules Call Dword hGetModuleFileNameEx Using GetModuleFileNameEx _ (hProcess, Modules(j), Proc.szExeFile, SizeOf(Proc.szExeFile)) To lResult If lResult Then If j = 1 Then ListBox Add hDlg, 101, Proc.szExeFile Else _ ListBox Add hDlg, 101, " " + Proc.szExeFile Next CloseHandle hProcess End If Next End If End Sub CallBack Function DlgProc Select Case CbMsg Case %WM_INITDIALOG EnumModules Case %WM_SIZE Dim rc As RECT GetClientRect CbHndl, rc SetWindowPos GetDlgItem(CbHndl, 101), 0, _ 0.03 * rc.nRight, 0.02 * rc.nBottom, 0.94 * rc.nRight, _ 0.96 * rc.nBottom, %SWP_NOACTIVATE Or %SWP_NOZORDER End Select End Function Function PbMain Dialog New 0, "Processes and modules", , , 400, 200, _ %WS_SYSMENU Or %WS_CAPTION Or %WS_THICKFRAME, To hDlg Control Add ListBox, hDlg, 101, , 0, 0, 0, 0, %WS_CHILD Or %WS_VSCROLL, %WS_EX_CLIENTEDGE Dialog Show Modal hDlg Call DlgProc End Function
I thought that this knowledge can be useful, for example, to detect "gifts" in own process.
Somebody knows how to retrieve the DLL's list under 9x ?
(it's not very important and I already solved initial task, but interesting ...)
------------------
[This message has been edited by Semen Matusovski (edited August 01, 2000).]
Comment