Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How find if a Application is running in another Session ?

Status
Not open for further replies.

Kongsjs

Programmer
Jul 20, 2007
17
NO
Hi

Need some help here....

I am running a application (PB 7.0.3) on a Windows 2003 server. This application is starting automatical when the server reboot, and is starting on the Consol session with user "X". Then if I or some other log on to this server with user "X" again, but not to the Console session, then this application starts up for the second time, and this MUST NOT HAPPEND.

I have coded so in the same session you cannot start the program twice, but it dont work over different sessions.

I use this code:
[External functions]
FUNCTION ulong CreateMutexA (ulong lpMutexAttributes, boolean bInitialOwner, REF string lpszName) LIBRARY "kernel32.dll"
FUNCTION long GetLastError() LIBRARY "kernel32.dll"
////////////////////////////////////////////////////
FUNCTION boolean of_IsRunning()
////////////////////////////////////////////////////
constant ulong ERROR_ALREADY_EXISTS = 183
constant ulong SUCCESSFUL_EXECUTION = 0
//
ulong lul_mutex
ulong lpsa
ulong lul_last_error
boolean lb_ret = FALSE
//
IF NOT (Handle(GetApplication()) = 0) THEN
lul_mutex = CreateMutexA(lpsa, FALSE, as_appname)
lul_last_error = GetLastError()
lb_ret = NOT (lul_last_error = SUCCESSFUL_EXECUTION)
END IF
//
RETURN lb_ret

Is there someone that know how to solve this ?

Regards
Kongsjs
 
You could write a value to a table or the application registry which is set when launched and unset when the app is closed. Or perhaps check for a db connection from that user prior to launching?

Matt

"Nature forges everything on the anvil of time
 
Yes, I could do that, but if the application was terminated in a non normal way, the value would still be there and the program would'nt start up again. The same is for a database connection.
This application is reading thousands of file every houre 24/7/365, and must always run, but only one instance of it, else it would start fighting over the input files (have been throug that a lot of times).

Thaks for the reply Matt, but I need to find a more waterproof way.

If anywone have any suggestions I realy would be happy.
 
Not sure if this will work for win2003.
Info is from
Code:
Check if a program is running

OleObject locator,service,props
String ls_query = &
 'select name , description from Win32_Process where name = "textpad.exe"'
int num, ret, i
locator = CREATE OleObject
ret = locator.ConnectToNewObject("WbemScripting.SWbemLocator");
service = locator.ConnectServer();
props = service.ExecQuery(ls_query);
num = props.count()

IF num > 0 THEN
 MessageBox("Process","textpad.exe is running")
ELSE
 Messagebox("Process","textpad.exe is NOT running")
END IF

Before using this technique, you may need to download and install the WMI ActiveX, here the URL.
Check MSDN website for more current version ov WMICore.exe.

Matt

"Nature forges everything on the anvil of time"
 
Hi Matt


This worked perfectly. Thanks a lot :)


Kongsjs
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top