• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 541
  • Last Modified:

GetModuleHandle Windows API in VB5

I'm having a problem getting any Windows API function to work in VB5. My current problem is with GetModuleHandle. I need to determine if an application is running. The code compiles and runs fine but the function always returns a 0. This is the code I'm using, all within a single form:

Option Explicit
Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long

Private Sub cmdOK_Click()
Dim lHandle As Long
Dim sCommand As String
Dim sArgument As String
Dim dRC As Double

sArgument = "testword.doc"
sCommand = "C:\Program Files\Microsoft Office\Office\winword.exe " & sArgument
dRC = Shell(sCommand, vbNormalFocus)
lHandle = GetModuleHandle("winword.exe")
dRC returns the task ID fine but lHandle always returns a 0 even though MSWord is running.

I've used Winapi functions in VB3 but I've never gotten one to work in VB5 - is there a reference or component I need to select? If anyone knows of alternative way to check for an application running that would be a great solution too. Thanks.

1 Solution
I know you're having trouble with API's, but this uses some different ones, and waits until the shelled process is finished.

Const PROCESS_ALL_ACCESS = 2035711 '(&H1F0FFF)
Const STILL_ACTIVE = 259 '(&H103)

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Public Sub RunShell()

    Dim idProg As Long, iExit As Long
    idProg = Shell("exename.exe", 1)
    iExit = WaitOnShell(idProg)
    If iExit Then MsgBox "failed"
End Sub

Public Function WaitOnShell(ByVal idProg As Long) As Long

    Dim iExit As Long, hProg As Long
    hProg = OpenProcess(PROCESS_ALL_ACCESS, False, idProg)
    GetExitCodeProcess hProg, iExit
    Do While iExit = STILL_ACTIVE
        GetExitCodeProcess hProg, iExit
    CloseHandle hProg
    WaitOnProgram = iExit
End Function
check out :How to Determine if a Specific Application is Already Running

joe_sAuthor Commented:
That site is only for 16 bit. I've done this in VB3, I'm only having a problem in 32 bit but thanks for trying.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

'try putting the declare in a bas module
 and then run your code

Public Const MAX_PATH = 206

Public Declare Function GetModuleHandle Lib _
"kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) _
As Long
Public Declare Function GetModuleFileName Lib _
"kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, _
ByVal lpFileName As String, ByVal nSize As Long) As Long
joe_sAuthor Commented:
vbguru - I responded to your answer too fast. I found sample code at that site that works fine. I have a feeling that the function I was using 'getmodulehandle' doesn't work under VB5, the sample code worked great.

Please resubmit your answer for the points and THANKS for the help.


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now