Solved

How to check same program and How to use FILEOVERLAPPED

Posted on 2002-03-17
5
174 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now