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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find a handle of an running application ??? 1

Status
Not open for further replies.

vulcanjena

Programmer
May 22, 2003
39
0
0
PR
How to find a handle of an running application ???
 
Vulcanjena,

Handle() returns a valid value only when running the app in its compiled format (not from the IDE). From within the app, PowerScript:

Handle( app_name )

From other objects, PowerScript:

Handle( GetApplication())

The return value is a long. Some API functions may need this as an unsigned long so you may have to declare it as an unsigned long.

---
PowerObject!
-----------------------------------------
PowerBuilder / PFC Developers' Group
 
I checked that both....Handle() as well as GetApplication() too. My problem is how do i know one instance of my application is running or not ? e.g if myapp.exe is running which i can see in taskmanager but it is not appearing in windows screen. so when i start is again it should let me know that already one instance is running .
thanks in advance.
 
You can use the Handle() function with its second argument set to TRUE as under:

IF Handle( This, TRUE ) > 0 THEN
//
MessageBox("Application Already Running", &
This.AppName + " is already running." &
+ " You cannot start it again.")
HALT CLOSE
//
ELSE
Open( w_frame )
END IF


However, the above code is for Windows 3.1. If your operating system is later, try the API:

Declare FindWindowA as a global external function:

FUNCTION uint FindWindowA( long classname, string windowname ) LIBRARY "user32.dll"

Then add code like the following to your application's open event:

uint val
//
val = FindWindowA( 0, "MyApp Main Window" )
//
IF val > 0 THEN
MessageBox("Application already running", &
"MyApp is already running. You cannot start it again")
HALT CLOSE
ELSE
open( w_frame)
END IF


---
PowerObject!
-----------------------------------------
PowerBuilder / PFC Developers' Group
 
my dear friend , I have already tried those PB examples and still I can run multiple instances at a time in one PC.
the fact is that the handle returns 0 values in those cases.

by the way will you please tell me how u defined this function ... please send me the script .

FUNCTION uint FindWindowA( long classname, string windowname ) LIBRARY "user32.dll"


Thanks in advance
 
You must be aware that FindWindowA is a Windows' API function. You need to declare it in your app object as a Local External Function and call it in your script just like a PowerBuilder function.

---
PowerObject!


 
I'll appreciate if you can please send me the script .....
 
There is no script for this function as the script for this function is provided by Microsoft in the Windows API - User32.dll that every machine has in its Windows directory. All you need to do to call this API function is to declare it as an external function in your app object from the menu:

Declare->Local External Functions
Declare->Global External Functions

You will see the Declare menu from the script painter. If you want to declare it as a Global External Function, you can declare it from any object from the Declare menu and it is available and visible through out the application. The code to place in the Declare dialog:

FUNCTION uint FindWindowA( long classname, string windowname ) LIBRARY "user32.dll"

Once you save this declaration in the dialog, you can use this function just like any other PowerBuilder function. At runtime, PowerBuilder resolves this function from the User32.dll of the Windows.

As a matter of fact, functions such as GetFileOpenName(), GetFileSaveName()... are all mapped to the standard Windows' API functions by PowerBuilder for us so we don't have to declare them. However, rare functions such as the FindWindowA have to be declared by us.

---
PowerObject!


 
I tried as per you. but still I'm able to run two instances .

Anyway ..thanx a lot
 
Hey if you are not particular about using Handle, Getapplication, api calls etc... i have a very simple solution for you, ofcourse i may be wrong too in giving u this solution as i dont know what ur expectations are anyway here is the solution.

In the application open event open a text file exclusively and if the open of the text file is success then only proceed otherwise give a message saying that already an instance of the application is running. The code would look something as below:

li_FileNum = FileOpen("\\valf01\network\group\copsrpt\adhoc.ini",LineMode!,Write!,LockWrite!,Append!)

// If some other user is accessing the application, a
//message box is displayed and the
// application will not open, else opens the application.

IF li_filenum=-1 THEN
MessageBox("Hold On", "Application is executing in some other machine ... Please logoff that session and try again")
HALT CLOSE
ELSE
OPEN(w_login)
END IF
 
My expectaion is to know whether myapplication.exe is running or not ? if it is running then not to allow to open another one. sometimes let's say ...myapplication.exe is running , u can see from TaskMgr but the application
did not appear due to some reason in windows os.
I tried all the way both API , Handle etc . But still I can open another one . All of them gives me return values 0. Anyway I really appreciate for you efforts .

I'm using WinXP(Prof) & PB9 with SQL 6 & ORACLE 9
 
Opening a file in lock mode can have some side effects. What if the user aborted the app from the Task-Manager? The file still stays locked.

Use the Windows API's Mutex flags, instead (this works):

[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()
////////////////////////////////////////////////////
// USAGE:
//
// IF of_isRunning THEN
// MessageBox("Oops!", "App already running!")
// END IF
////////////////////////////////////////////////////
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


Hope this helps...

---
PowerObject!
-----------------------------------------
PowerBuilder / PFC Developers' Group
 
I faced the same problem..found out problem..

Simple solution... the use of FindWindowA techniques will work only when the Title of the window is same as the one you will be using for this function..

same above ex.

val = FindWindowA( 0, "MyApp Main Window" )

Your window title (through properties you can set that) should be "MyApp Main Window"

This should work.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top