CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2000
    Posts
    26

    Enum Loaded Modules

    Hello,
    I would like to know the version of all the dll modules loaded by my application.I am able to find the version by passing the file path . But i am finding it difficult to Enumerate the Loaded Modules.Can someone help me in this regard.
    Thanks
    Regards,
    Thiyagarajan M


  2. #2
    Join Date
    Apr 2000
    Posts
    737

    Re: Enum Loaded Modules

    the following API might help

    Module32First,Module32Next, CreateToolhelp32Snapshot

    I got the following code to get all module using by a process, but you need to know the process ID, or simply pass in 0 if you want the current process. This code probably wouldn't work on your pc, but just follow the step & refer MSDN to get it work. This is because it use vbLib (http://vblib.virtualave.net)




    Declare Function Module32First Lib "kernel32" (byval hSnapshot as Long, lpme as MODULEENTRY32) as Long
    Declare Function Module32Next Lib "kernel32" (byval hSnapshot as Long, lpme as MODULEENTRY32) as Long

    Declare Function CreateToolhelp32Snapshot Lib "kernel32" (byval dwFlags as Long, byval the32ProcessID as Long) as Long

    private Const GET_CURRENT_PROCESS as Long = 0
    private Const MAX_MODULE_INFO as Long = 1024

    public Type MODULEENTRY32
    dwSize as Long
    th32ModuleID as Long
    th32ProcessID as Long
    GlblcntUsage as Long
    ProccntUsage as Long
    modBaseAddr as Long
    modBaseSize as Long
    hModule as Long
    szModule as string * MAX_MODULE_NAME32
    szExePath as string * MAX_PATH
    End Type


    public Type vbSystem_MODULE_INFO
    ModuleID as Long
    hModule as Long
    BaseAddress as Long
    GlobalUsage as Long
    ProcessUsage as Long
    Size as Long
    ModuleName as string
    ModuleFileName as string
    End Type

    Global Const MAX_MODULE_NAME32 as Long = 256
    Global Const TH32CS_SNAPMODULE as Long = 8


    'get all module info
    public Function GetAllModuleID(ModuleInfo() as vbSystem_MODULE_INFO, optional byval ProcessID as Long = GET_CURRENT_PROCESS) as Long
    Dim result as Long
    Dim tM as Long
    Dim hSnap as Long
    Dim lpme as MODULEENTRY32
    Dim errorNo as Long

    hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ProcessID)
    vbLibErrorModule.vbDllErrorCode = GetLastError

    If hSnap <> INVALID_HANDLE_VALUE then

    lpme.dwSize = LenB(lpme)
    ReDim ModuleInfo(MAX_MODULE_INFO) 'allocate buffer
    tM = 0

    errorNo = VB_OK
    vbLibErrorModule.vbLibFuncName = "kernel32.dll - Module32First"
    result = Module32First(hSnap, lpme)
    vbLibErrorModule.vbDllErrorCode = GetLastError

    Do While result <> VB_FALSE

    If tM = 0 then
    vbLibErrorModule.vbLibFuncName = "kernel32.dll - Module32next"
    End If

    'assign data
    ModuleInfo(tM).GlobalUsage = lpme.GlblcntUsage
    ModuleInfo(tM).hModule = lpme.hModule
    ModuleInfo(tM).ModuleID = lpme.th32ModuleID
    ModuleInfo(tM).ProcessUsage = lpme.ProccntUsage
    ModuleInfo(tM).Size = lpme.modBaseSize
    ModuleInfo(tM).BaseAddress = lpme.modBaseAddr

    vbStrings.PurseString lpme.szExePath, ModuleInfo(tM).ModuleFileName, Chr(0) 'no resource for error
    vbStrings.PurseString lpme.szModule, ModuleInfo(tM).ModuleName, Chr(0) 'no resource for error

    tM = tM + 1

    If tM <= MAX_MODULE_INFO then

    result = Module32Next(hSnap, lpme)
    vbLibErrorModule.vbDllErrorCode = GetLastError

    else

    errorNo = VB_SYSTEM_BUFFER_TOO_SMALL

    End If

    Loop

    If errorNo <> VB_OK then

    GetAllModuleID = errorNo

    ElseIf vbLibErrorModule.vbDllErrorCode <> ERROR_NO_MORE_FILES then

    GetAllModuleID = VB_DLL_ERROR

    else

    result = CloseHandle(hSnap)
    vbLibErrorModule.vbDllErrorCode = GetLastError

    If result <> VB_FALSE then

    If tM > 0 then
    ReDim Preserve ModuleInfo(tM - 1)
    else
    Erase ModuleInfo
    End If

    GetAllModuleID = tM

    else

    vbLibErrorModule.vbLibFuncName = "kernel32.dll - CloseHandle"
    GetAllModuleID = VB_DLL_ERROR

    End If

    End If

    else

    vbLibErrorModule.vbLibFuncName = "kernel32.dll - CreateToolhelp32Snapshot"
    GetAllModuleID = VB_DLL_ERROR

    End If

    End Function





    HTH


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured