Solved

How to check same program and How to use FILEOVERLAPPED

Posted on 2002-03-17
5
176 Views
Last Modified: 2013-11-20
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
Comment
Question by:can9999
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:shilpa_Jb
ID: 6876063
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
 
LVL 1

Author Comment

by:can9999
ID: 6876093
hmm thanks but.

That is for MFC.

I want to know the method using WinAPI, too.

^^

0
 
LVL 1

Expert Comment

by:pkreddy
ID: 6876179
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
 
LVL 2

Accepted Solution

by:
shilpa_Jb earned 100 total points
ID: 6876199
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
 
LVL 1

Author Comment

by:can9999
ID: 6878579
Thanks.

Would you let me know FILE_OVERLAPPED_FLAG in CreateFile ?

bye .
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question