Tasp
Full Member
Posts: 215
|
Post by Tasp on Jul 1, 2021 13:54:19 GMT -5
Hi all,
I'm looking to test to see if my program is already running and to prevent it from running a 2nd instance.
I'm sure I have done this before but I'm unsure how or where the code went!
I assume I need to check if the process is running. But I'm not sure where to even start looking for code to do that.
I've used the usual search terms I would associate with this but have drawn a blank.
Any ideas?
|
|
|
Post by Walt Decker on Jul 1, 2021 14:05:12 GMT -5
User32.dll function GetWindowLong(WinHndl, GWL_HINSTANCE) GWL_HINSTANCE = -6
PS: On startup you could test for a simple text file. If it is not present create it and continue; delete it at end of app. If it is present, abort.
|
|
|
Post by Rod on Jul 2, 2021 6:52:07 GMT -5
|
|
|
Post by Walt Decker on Jul 2, 2021 8:31:45 GMT -5
Another possibility is User32.dll function EnumWindows(CallbackFunction Pointer, lParam)
The function enumerates all top-level windows on the screen by passing the handle of each window, in turn, to an application-defined callback function. EnumWindows continues until the last top-level window is enumerated or the callback function returns FALSE.
The callback function looks like:
FUNCTION EnumWindProc(hwnd, lParam) WinName$ = SPACE$(180)
CALLDLL #USER, "GetWindowText", hwnd AS ULONG, WinName$ AS PTR, 180 AS LONG, _ NumChrs AS LONG WinName$ = LEFT$(NumChrs)
IF WinName$ = ThisProcName$ THEN EXIT FUNCTION
EnumWindProc = 1 END FUNCTION
|
|
Tasp
Full Member
Posts: 215
|
Post by Tasp on Jul 3, 2021 4:40:06 GMT -5
Thanks guys, I shall investigate both methods.
I didn't think of the txt file route and it does follow the KISS regime. However if the program crashes for any reason it could leave a text file there even after a restart.
|
|