Prohibit a user from performing concurrent logins for a PowerBuilder App

I am building security into a system that contains 3 different PowerBuilder applications ( 3 different exe's). I would like to know how I can prohibit a user from launching a second instance of the same executable. I understand that there is a way using Microsoft's API to find out if a particular executable is in the computer's memory.

I am currently using Microsoft's API to obtain the computer name and user name of the person logged onto the computer.

I would like the PowerBuilder application to check the computer's memory to ensure the same exe is not currently running prior to displaying the login screen to the application.

Being a novice developer, I would greatly appreciate it if you could provide code samples.
Who is Participating?
The second argument to the findwindow function is the window title,
not the internal PB window name.

For the PB 6.5 online help :

On Windows 95 and Windows NT, the Handle function does not return a useful value when the previous flag is TRUE. You can use the FindWindowA Windows function to determine whether a Windows application is already running.

* 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")
Easiest way to make a section in your INI file [Application] and when application starts,  put the value to 1
App_A=1 , in close event of application to 0 ( App_A=0 ) in your INI file and so on.

MOST IMPORTANT*** If your program crashed INI file remain unchange. INI file changing can be develpoed in error Event.

You can recognise which appl. is running and which Appl. not.

check it also.
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

frank_sAuthor Commented:

Does FindWindow work in 5.0?

I put the code in, but it did not work.
Can you see anything I did wrong?

Here is what I put in:
Global External Function
FUNCTION uint FindWindowA(long classname, string windowname) LIBRARY "user32.DLL"

In the Application Open Event:
uint val

val = FindWindowA(0,"w_cdr_frame")

if val > 0 then
     MessageBox("Application already running", "Security is already running")
     gs_ini_file = "cdr.ini"
     OpenWithParm(w_login,"cdr_rept/Administration Report")
     if Message.DoubleParm =1 then
          gw_frame.ia_app = this
     end if
end if

Thanks for all your help.
frank_sAuthor Commented:
Question for PowerBuilder 5.0.04

I need to get the IP Address of a local computer.
I know how to get to the IP Address in the Registry.
The problem is the IP Address is in a hexadecimal form.
How do I get the hexadecimal into a variable?
How do I convert the hexadecimal back to a standard IP Address?


If you don't like the FindWindow approach, because you don't have a fix title, you can use the CreateMutex function. This function creates a NEW mutex process if doesn't exists, or just opens a handle to the existing one otherwise:

//[external function declaration]
 FUNCTION ulong CreateMutexA(ulong lpMutexAttributes, boolean bInitialOwner, REF string lpszName) &
    LIBRARY "kernel32.dll"
 FUNCTION long GetLastError() LIBRARY "kernel32.dll"
 FUNCTION long CloseHandle(ulong w_handle) LIBRARY "Kernel32.dll"

//[var declaration]
 ulong ziul_mutex

//on the app::open event

 constant ulong ERROR_ALREADY_EXISTS = 183
 constant ulong SUCCESSFUL_EXECUTION = 0

 ulong lpsa
 ulong lul_last_error
 boolean lb_ret = FALSE

 IF NOT (Handle(GetApplication()) = 0) THEN
     ziul_mutex = CreateMutexA(lpsa, FALSE, as_appname)
     lul_last_error = GetLastError()
     IF lul_last_error = ERROR_ALREADY_EXISTS THEN
         //the mutex already exists so give a message and close the app
         messagebox("", "App alrady running")
         HALT CLOSE
     END IF

//on the app::close event
//destroy the created handle

Suggest you to start a new session for the new question.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.