?
Solved

How to check same program and How to use FILEOVERLAPPED

Posted on 2002-03-17
5
Medium Priority
?
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses

752 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