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 can I see in my program if there is not yet an instance running

Status
Not open for further replies.

wimvanherp

Programmer
Mar 3, 2001
149
0
0
BE
Hello,

does anybody know how to see if there is not yet an instance of the program running, so I can avoid the program being started twice.

regards


Wim Vanherp
Wim.Vanherp@myself.com
 
I supposed, that the program name is "Test", then next code will not allow to execute second instance of the program "Test".
//----------------------------------------------------------static int runing = 0;
//----------------------------------------------------------bool __stdcall EnumProc(HWND hWnd,long)
{
unsigned long* pPid;
unsigned long result;
void *hg;
unsigned long id;

if(hWnd==NULL)
return false;

hg = GlobalAlloc(GMEM_SHARE,sizeof(unsigned long));
pPid = (unsigned long *)GlobalLock(hg);

result = GetWindowThreadProcessId(hWnd,pPid);

if(result)
{
char className[95];
char title[256];
GetClassName(hWnd,className,95);
GetWindowText(hWnd,title,110);
if(!strcmp(className,"TApplication") &&
!strcmp(title,"Test"))
runing++;
}

GlobalUnlock(hg);
GlobalFree(hg);
return true;
}
//----------------------------------------------------------void __fastcall TFormTest::FormActivate(TObject *Sender)
{
EnumChildWindows(GetDesktopWindow(),(WNDENUMPROC) EnumProc,0);

if(runing == 2)
{
MessageBox(0,"Already runing!","Waring!",MB_OK);
Close();
}
}
//----------------------------------------------------------
 
The code is case sensitive, so "Test" != "test".
To avoid this, use strcmpi(title,"Test");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top