Click here to Skip to main content
15,920,513 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: CPU Usage Pin
Dudi Avramov16-Mar-03 21:59
Dudi Avramov16-Mar-03 21:59 
Generalmultiple monitors Pin
cmallory16-Mar-03 3:41
cmallory16-Mar-03 3:41 
GeneralLine Counter Pin
cerb-dk16-Mar-03 3:34
cerb-dk16-Mar-03 3:34 
QuestionHow can I program so that the application only respond to mouse events? Pin
DengJW16-Mar-03 3:31
DengJW16-Mar-03 3:31 
AnswerRe: How can I program so that the application only respond to mouse events? Pin
Nish Nishant16-Mar-03 4:47
sitebuilderNish Nishant16-Mar-03 4:47 
QuestionHow can I write a program in VC++6 such that only ONE instance of Pin
DengJW16-Mar-03 3:22
DengJW16-Mar-03 3:22 
AnswerRe: How can I write a program in VC++6 such that only ONE instance of Pin
73Zeppelin16-Mar-03 3:57
73Zeppelin16-Mar-03 3:57 
AnswerRe: How can I write a program in VC++6 such that only ONE instance of Pin
Florin Ochiana16-Mar-03 11:42
Florin Ochiana16-Mar-03 11:42 
Excerpt from MSDN:
You will find this in the file msdn_mfcfaq50.htm (MFC FAQ).

---------------
How do I limit my MFC application to one instance?

Look at the Microsoft C++ sample ONETIME.

In brief:

const char* MyMainWndClassName = "MyMainWndXQW"
BOOL CMyApp::InitApplication()
{
// Call base class. Default version does nothing.
CWinApp::InitApplication();
WNDCLASS wndcls;
// Start with NULL defaults.
memset(&wndcls, 0, sizeof(WNDCLASS));
// Get class information for default window class.
::GetClassInfo(AfxGetInstanceHandle(),"AfxFrameOrView",&wndcls);
// Substitute unique class name for new class.
wndcls.lpszClassName = MyMainWndClassName;
// Register new class and return the result code.
return ::RegisterClass(&wndcls);
}

And:

BOOL CMyApp::FirstInstance()
{
CWnd *PrevCWnd, *ChildCWnd;
// Determine if another window with our class name exists.
PrevCWnd = CWnd::FindWindow(MyMainWndClassName, NULL);
if (PrevCWnd != NULL)
{
// If so, does it have any pop-ups?
ChildCWnd=PrevCWnd->GetLastActivePopup();
// Bring the main window to the top.
PrevCWnd->BringWindowToTop();
// If iconic, restore the main window.
if (PrevCWnd->IsIconic())
PrevCWnd->ShowWindow(SW_RESTORE);
// If there are pop-ups, bring them along too!
if (PrevCWnd != ChildCWnd)
ChildCWnd->BringWindowToTop();
// Return FALSE. This isn't the first instance
// and we are done activating the previous one.
return FALSE;
}
else
// First instance. Proceed as normal.
return TRUE;
}
CMyApp::InitInstance()
{
if (!FirstInstance())
return FALSE;
// ...
}

null@diku.dk, programmer.tools, 6/19/95
See also Win32 SDK Knowledge Base article Q124134 ("Allowing Only One Application Instance on Win32s") and Jeffrey Richter's Advanced Windows NT, chapter 7, "Prohibiting Multiple Instances of an Application from Running: The MultInst Sample Application" (available on the MSDN Library CD).

null@diku.dk, email, 8/8/95
Update—these were posted to mfc-l:

I have each InitApplication() create a semaphore. If GetLastError() returns ERROR_ALREADY_EXISTS then I know that some other application is already running and has gotten that far, so I bail.

Yourapp::InitInstance()
{
hMutexOneInstance =
CreateMutex(NULL,TRUE,_T("PreventSecondInstance"));
if(GetLastError() == ERROR_ALREADY_EXISTS)
bFound = TRUE;
if(hMutexOneInstance)
ReleaseMutex(hMutexOneInstance);
return (bFound == TRUE) ? FALSE : TRUE;
}

mcontest@universal.com
There is a nice section in Jeffrey Richter's book Advanced Windows NT about this. Essentially, it uses shared data segments between processes.

In your main file, add:
#pragma data_seg(".SharedData")
LONG nUsageCount = -1;
#pragma data_seg()

In your Application's InitInstance(), call:
InterlockedIncrement ( &nUsageCount );

This function returns the incremented value of the variable. If it is nonzero, you know that you are not the first App.

In your Application's ExitInstance(), call:
InterlockedDecrement( &nUsageCount );

In your .DEF file, have the following lines (note that the segment name you give here should match the one in the application's main file):
SEGMENTS
.SharedData shared

abalakri@us.oracle.com
You'd better use one of the built-in synchronization methods. See the Win32 Knowledge Base article Q124134, "Allowing Only One Application Instance on Win32s," for a sample of using a memory-mapped file for synchronization. It doesn't include starting the previous instance, but if you detect that you're not the only one running, it should be pretty simple: If CreateFileMapping fails, try to find the previous instance from the window class name. If it's not found, sleep for a while and start over (with CreateFileMapping). In this way, either you will find the other instance when it gets around to creating its window or CreateFileMapping will eventually succeed. The advantage of using CreateFileMapping instead of CreateObject is that it also works on Win32s.

nuj@kruger.dk
Note There's a sample of this that was contributed by john@jing.com (John Xu) called onetime4.zip that is in the MFC FAQ archive.


-----
We are what we repeatedly do. Excellence, then, is not an act, but a habit.

GeneralThanks to John and Florin. I am reading up the docs. Do you have any more suggestions on Pin
DengJW16-Mar-03 15:41
DengJW16-Mar-03 15:41 
AnswerThanks to John and Florin. I learnt a lots from your posts and got my thing done. Pin
DengJW16-Mar-03 23:16
DengJW16-Mar-03 23:16 
GeneralCoordinate Conversions Pin
georgiek5016-Mar-03 2:56
georgiek5016-Mar-03 2:56 
GeneralRe: Coordinate Conversions Pin
Brian Shifrin16-Mar-03 3:04
Brian Shifrin16-Mar-03 3:04 
GeneralRe: Coordinate Conversions Pin
georgiek5016-Mar-03 3:07
georgiek5016-Mar-03 3:07 
GeneralRe: Coordinate Conversions Pin
73Zeppelin16-Mar-03 3:13
73Zeppelin16-Mar-03 3:13 
GeneralRe: Coordinate Conversions Pin
georgiek5016-Mar-03 6:28
georgiek5016-Mar-03 6:28 
GeneralRe: Coordinate Conversions Pin
73Zeppelin16-Mar-03 7:03
73Zeppelin16-Mar-03 7:03 
GeneralRe: Coordinate Conversions Pin
georgiek5016-Mar-03 7:41
georgiek5016-Mar-03 7:41 
GeneralRe: Coordinate Conversions Pin
georgiek5016-Mar-03 14:38
georgiek5016-Mar-03 14:38 
GeneralRe: Coordinate Conversions Pin
73Zeppelin16-Mar-03 15:51
73Zeppelin16-Mar-03 15:51 
GeneralNeed a windows registry expert's help Pin
Abin16-Mar-03 2:18
Abin16-Mar-03 2:18 
QuestionHow to calculate the running time of each function? Pin
George216-Mar-03 1:24
George216-Mar-03 1:24 
GeneralMessage routing Pin
JamesLaing16-Mar-03 1:01
JamesLaing16-Mar-03 1:01 
GeneralRe: Message routing Pin
Brian Shifrin16-Mar-03 2:33
Brian Shifrin16-Mar-03 2:33 
GeneralRe: Message routing Pin
JamesLaing16-Mar-03 2:38
JamesLaing16-Mar-03 2:38 
GeneralChanging Ownership of child CView Pin
ganon15-Mar-03 17:23
ganon15-Mar-03 17:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.