WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: erezpaz on August 20, 2019, 02:30:50 AM

Title: Running from studio single process in debug mode
Post by: erezpaz on August 20, 2019, 02:30:50 AM
Hi,

I am using an example from here how to run only one process instate of my program.
DupThread = 0
kernel32=strcat(dirwindows(1),"kernel32.dll")
ERROR_ALREADY_EXISTS=183
mutexname="*mywinbatchappnamev1.x*"
hmutex=dllcall(kernel32,long:"CreateMutexA",lpnull,long:0,lpstr:mutexname)
if DllLastError()==ERROR_ALREADY_EXISTS ;mutex exist an hinstance is running
display(5,NameVer,"Only 1 instance of %tool% is allowed.")
exit
end if
It works fine when i run the exe directly. If i run it from the studio in regular mode or debug mode i get the message and the program exit.
Can you help me understand how to prevent that?

Thanks
Title: Re: Running from studio single process in debug mode
Post by: kdmoyers on August 20, 2019, 05:17:47 AM
Just a data point: it's working fine for me on my Win7 and Win10 machines.
(I had to define NameVer)
Thanks for the cool example!
$0.02
-Kirby
Title: Re: Running from studio single process in debug mode
Post by: td on August 20, 2019, 07:45:40 AM
When you call your function from the WinBatch Studio debugger,  the mutex remains in existence even after the debugging session ends because the WinBatch Studio process is still running.

Also, is there some reason why you are not using the AppExist function instead of fiddling with a DllCall?
Title: Re: Running from studio single process in debug mode
Post by: td on August 21, 2019, 07:07:48 AM
If there is a good reason to be using a DllCall instead of AppExist, you can always simply close the mutex handle using the Win32 CloseHandle function just before the script terminates.