GetModuleHandle
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: GetModuleHandle

  1. #1
    Join Date
    Jun 1999
    Posts
    34

    GetModuleHandle

    Hi
    I've got the following code on "planet source" but it doesn't work with VB 5.
    Can anybody help me ?
    Thank's


    '************************************************

    'Windows API/Global Declarations for etermine 'if a Specific Application is active

    '*********************************************
    Declare Function GetModuleUsage Lib "Kernel" (ByVal hModule As Integer) As Integer


    Declare Function GetModuleHandle Lib "Kernel" (ByVal ApplicationFileName As String) As Integer

    'In the form :

    Sub Command1_Click()


    If GetModuleUsage(GetModuleHandle("notepad.exe")) then
    MsgBox "Windows Notepad is already running!", 56
    End
    End If

    End Sub



    ///\\
    /////\\)
    /////(
    ////////\Tony Shape

  2. #2
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: GetModuleHandle

    Hi

    The code you've got there is designed for VB3/4 - it's a 16bit application (GetModuleUsage doesn't exist for 32 bit programs).

    Chris Eastwood

    CodeGuru - the website for developers
    http://www.codeguru.com/vb

  3. #3
    Join Date
    May 1999
    Posts
    3,332

    Re: GetModuleHandle

    since this code only checks if Notepad is running, you don't need GetModuleUsage.
    If GetModulehandle returns a non-null value you know that it's running.

    Thus, you could write:

    if getmodulehandle("notepad.exe") <> 0 then
    msgbox "notepad is running"
    end if





  4. #4
    Join Date
    Jun 1999
    Posts
    34

    Re: GetModuleHandle

    Here is my new code but the GetModuleHandle function returns always 0 ???
    Have you got an idea?

    Declare Function GetModuleHandle Lib "Kernel32" Alias "GetModuleHandleA" (ByVal ApplicationFileName As String) As Integer


    Private Sub Command1_Click()

    If GetModuleHandle("notepad.exe") <> 0 Then
    MsgBox "notepad is running"
    End If


    End Sub


    ///\\
    /////\\)
    /////(
    ////////\Tony Shape

  5. #5
    Join Date
    May 1999
    Posts
    3,332

    Re: GetModuleHandle

    the return value is 0 because my code is wrong!
    I just checked the Platform SDK:
    "The name is compared (case independently) to the names of modules currently mapped into the address space of the calling process. "

    Of course Notepad is not "mapped into the address space of your program".
    Sorry about that.
    Use FindWindow instead.


    If FindWindow("Notepad", vbNullString) <> 0 then
    MsgBox "notepad running"
    End If




    This one works. I#ve tested it.


  6. #6
    Join Date
    Jun 1999
    Posts
    34

    Re: GetModuleHandle

    I've declared the function with the following line, but the error number 453 occured. The message is : Enter point FindWindow of a dll unfindable in Kernel32.
    Declare Function FindWindow Lib "Kernel32" (ByVal ApplicationFileName As String, ByVal WindowName As String) As Integer

    Moreover, does it work if i use the .exe name instead of the handle ?

    ///\\
    /////\\)
    /////(
    ////////\Tony Shape

  7. #7
    Join Date
    May 1999
    Posts
    3,332

    Re: GetModuleHandle

    this is the declaration as pasted from the APIViewer

    public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (byval lpClassName as string, byval lpWindowName as string) as Long




    tested under NT 4.

    not sure what your second question means.
    You HAVE to specify the app name, not the handle.


  8. #8
    Join Date
    Jun 1999
    Posts
    34

    Re: GetModuleHandle

    It works under Win 95 to.
    In fact my original question was : i want to test "c:\windows\Notepad.exe" and not "Notepad".
    How can i do ?


    ///\\
    /////\\)
    /////(
    ////////\Tony Shape

  9. #9
    Join Date
    May 1999
    Posts
    3,332

    Re: GetModuleHandle

    if you want to find out whether a process which is based on an exefile with a certain path is loaded you could call CreateToolhelp32snapshot in Win95 and Win2000


  10. #10
    Join Date
    Jun 1999
    Posts
    34

    Re: GetModuleHandle

    I've no help on API, can you give to me the declaration of this API and a sample of its using ?

    ///\\
    /////\\)
    /////(
    ////////\Tony Shape

  11. #11
    Join Date
    May 1999
    Posts
    3,332

    Re: GetModuleHandle


    ' für Windows95
    private Const MAX_PATH = 260
    private Type PROCESSENTRY32
    dwSize as Long
    cntUsage as Long
    th32ProcessID as Long ' this process
    th32DefaultHeapID as Long
    th32ModuleID as Long ' associated exe
    cntThreads as Long
    th32ParentProcessID as Long ' this process's parent process
    pcPriClassBase as Long ' Base priority of process's threads
    dwFlags as Long
    szExeFile as string * MAX_PATH
    End Type
    private Const TH32CS_SNAPPROCESS = &H2
    private Declare Function CreateToolhelp32Snapshot Lib "kernel32.dll" (byval h as Long, byval nix as Long) as Long
    private Declare Function Process32First Lib "kernel32.dll" (byval h as Long, pe as PROCESSENTRY32) as Long
    private Declare Function Process32Next Lib "kernel32.dll" (byval h as Long, pe as PROCESSENTRY32) as Long




    excerpt from my vb program; I add all entries to a collection for easier enumeration.


    private Sub GetProcesses()
    Dim h as Long
    Dim i as Integer
    for i = col.Count to 1 step -1
    col.Remove 1
    next i
    h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    If h = 0 then
    RaiseSystemError (Err.LastDllError)
    Exit Sub
    End If
    Dim f as Long
    Dim pe as PROCESSENTRY32
    pe.dwSize = LenB(pe)
    f = Process32First(h, pe)
    RaiseSystemError Err.LastDllError
    Dim strExe as string
    Dim iPos as Integer
    on error resume next
    Do While f <> 0
    strExe = pe.szExeFile
    iPos = InStr(strExe, Chr(0))
    If iPos > 0 then
    strExe = Left$(strExe, iPos - 1)
    End If
    col.Add Key:=CStr(pe.th32ProcessID), Item:=strExe
    f = Process32Next(h, pe)
    Loop
    CloseHandle h
    End Sub

    private Sub RaiseSystemError(byval lErr as Long)
    ' löse Laufzeitfehler mit Err.Description = Systemfehlertext aus
    If lErr = 0 then
    Exit Sub
    End If
    Dim lResult as Long
    Dim strBuffer as string * 256
    lResult = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, lErr, 0, strBuffer, len(strBuffer), 0)
    If lResult > 0 then
    Err.Raise Number:=vbObjectError + 1001, Description:=Left(strBuffer, lResult)
    End If
    End Sub






  12. #12
    Join Date
    Jun 1999
    Posts
    34

    Re: GetModuleHandle

    Hello !
    If i understand your code, i must replace &H2 by my program path (like "c:\windows\calc.exe" for example), and before calling GetModuleHandle("c:\windows\calc.exe"), i must call GetProcesses() !
    I'm not sure i've understand all your code !
    How must i declare the variable "col" ?
    My sub would look like this !

    <vbcode>
    Private Sub Command1_Click()

    GetProcesses
    If GetModuleHandle("c:\windows\calc.exe") <> 0 Then
    MsgBox "Calculatrice open !"
    End If

    End Sub

    <\vbcode>

    ///\\
    /////\\)
    /////(
    ////////\Tony Shape

  13. #13
    Join Date
    May 1999
    Posts
    3,332

    Re: GetModuleHandle


    Dim col as new collection


    Call Getprocesses
    for i = 1 to col.count
    if strcomp( col.Item(i), "c:\windows\calc.exe",0) = 0 then
    msgbox "calc is running"
    exit for
    end if
    next i



    you iterate of the items in the collection (which contains all exenames of the running processes) and check if you find an entry that matches your search criteria.

    You could change the way GetProcesses works and make the Exename the key of the collection.
    That way you could write:
    if col("c:\windows\calc.exe") then
    calc is running

    I hope the above code works. I can't test it, because I don't have Win95 and the CreateTool... functions are not available in NT.


Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center