July 7th, 1999, 04:51 AM
I've got the following code on "planet source" but it doesn't work with VB 5.
Can anybody help me ?
'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 :
If GetModuleUsage(GetModuleHandle("notepad.exe")) then
MsgBox "Windows Notepad is already running!", 56
July 7th, 1999, 05:05 AM
The code you've got there is designed for VB3/4 - it's a 16bit application (GetModuleUsage doesn't exist for 32 bit programs).
CodeGuru - the website for developers
July 7th, 1999, 05:12 AM
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"
July 7th, 1999, 05:38 AM
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"
July 7th, 1999, 05:50 AM
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"
This one works. I#ve tested it.
July 7th, 1999, 07:00 AM
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 ?
July 7th, 1999, 07:06 AM
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.
July 7th, 1999, 07:20 AM
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 ?
July 7th, 1999, 08:08 AM
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
July 7th, 1999, 08:24 AM
I've no help on API, can you give to me the declaration of this API and a sample of its using ?
July 7th, 1999, 08:28 AM
' 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
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
h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If h = 0 then
Dim f as Long
Dim pe as PROCESSENTRY32
pe.dwSize = LenB(pe)
f = Process32First(h, pe)
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)
col.Add Key:=CStr(pe.th32ProcessID), Item:=strExe
f = Process32Next(h, pe)
private Sub RaiseSystemError(byval lErr as Long)
' löse Laufzeitfehler mit Err.Description = Systemfehlertext aus
If lErr = 0 then
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)
July 8th, 1999, 03:33 AM
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 !
Private Sub Command1_Click()
If GetModuleHandle("c:\windows\calc.exe") <> 0 Then
MsgBox "Calculatrice open !"
July 8th, 1999, 04:06 AM
Dim col as new collection
for i = 1 to col.count
if strcomp( col.Item(i), "c:\windows\calc.exe",0) = 0 then
msgbox "calc is running"
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.
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.