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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

'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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.