Solved

How to check same program and How to use FILEOVERLAPPED

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mixString challenge 36 98
Detect CR LF to each line 12 137
Annoying "thing" blocks my view 4 51
wordappend challenge 8 85
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…
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

17 Experts available now in Live!

Get 1:1 Help Now