• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

How to check same program and How to use FILEOVERLAPPED

1. I want to know how to check whether same program is already executed and how to avoid execution of same program...

2. I want to know how to use FILEOVERLAPPED
when to use.
0
can9999
Asked:
can9999
  • 2
  • 2
1 Solution
 
shilpa_JbCommented:
Hi,
Make use of mutex to check more than one instance of the application.
Just try to use the below code, hope that helps u.

// app.h
class CYourApp : public CWinApp
{  
private:
 HANDLE hMutex;//Handle to mutex...
};
// app.cpp
BOOL CYourApp::InitInstance()
{
// Create mutex here
 hMutex = ::CreateMutex(NULL, TRUE, "GlobalMutex");  
switch(::GetLastError())
{
 case ERROR_SUCCESS:  
 AfxMessageBox("Mutex created There is no other instance");
 break;  
 case ERROR_ALREADY_EXISTS:    
 AfxMessageBox(" Mutex already exists so there is a  running instance of our app");
 return FALSE;    
 default:
    AfxMessageBox("Failed to create mutex");
   return FALSE;  
}
}
Hope that helps u...
-Shilpa



0
 
can9999Author Commented:
hmm thanks but.

That is for MFC.

I want to know the method using WinAPI, too.

^^

0
 
pkreddyCommented:
Hi,
 Put the same piece of code in the WinMain() of your application. Some thing like(code lifted form Shilpa),

1. If you want to check at startup do so before you write the Run loop (While(GetMessage(){//Some code;}). remember to return 0 if you want to cancel the execution of app before calling the Run loop.
2. In case you want to exit in between the program definitely make use of PostQuitMessage().

Sample Code:

int WinMain(HINSTANCE hInstance,HINSTANCE PrevInstance,
  LPSTR lpCmdLine,int nCmdShow)
{
 HANDLE hMutex;//Handle to mutex...
// Create mutex here
hMutex = ::CreateMutex(NULL, TRUE, "GlobalMutex");  
switch(::GetLastError())
{
case ERROR_SUCCESS:  
TRACE0("Mutex created: There is no other instance");
break;  
case ERROR_ALREADY_EXISTS:    
TRACE0("Mutex exists: App already running\n");
return 0;    
default:
TRACE0("Failed to create mutex\n");
return 0;  
}

// Create WNDCLASS
// Registration
// etc....

//MSG LOOP
//Procedure

//etc...

}

If you want to display messages that "Instance already running" using a message box, try to use this this piece of code after you create window but before you show it and return 0 if instance is already running.
bye
PK




0
 
shilpa_JbCommented:
Hi can9999,
CreateMutex() is a winAPi Itself, And u can use it in either MFC or in any console application.
If u use it in  any application use preprocessor
#include<winbase.h>
#include <windows.h>
Hope that helps u.
-Shilpa
0
 
can9999Author Commented:
Thanks.

Would you let me know FILE_OVERLAPPED_FLAG in CreateFile ?

bye .
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now