Created
May 19, 2013 08:38
-
-
Save vinjn/5607085 to your computer and use it in GitHub Desktop.
count the number of executed applications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************** | |
Module: AppInst.cpp | |
Notices: Copyright (c) 2008 Jeffrey Richter & Christophe Nasarre | |
******************************************************************************/ | |
#include "..\CommonFiles\CmnHdr.h" /* See Appendix A. */ | |
#include <windowsx.h> | |
#include <tchar.h> | |
#include "Resource.h" | |
/////////////////////////////////////////////////////////////////////////////// | |
// The system-wide window message, unique to the application | |
UINT g_uMsgAppInstCountUpdate = WM_APP+123; | |
/////////////////////////////////////////////////////////////////////////////// | |
// Tell the compiler to put this initialized variable in its own Shared | |
// section so it is shared by all instances of this application. | |
#pragma data_seg("Shared") | |
volatile LONG g_lApplicationInstances = 0; | |
#pragma data_seg() | |
// Tell the linker to make the Shared section readable, writable, and shared. | |
#pragma comment(linker, "/Section:Shared,RWS") | |
/////////////////////////////////////////////////////////////////////////////// | |
BOOL Dlg_OnInitDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam) { | |
chSETDLGICONS(hWnd, IDI_APPINST); | |
// Force the static control to be initialized correctly. | |
PostMessage(HWND_BROADCAST, g_uMsgAppInstCountUpdate, 0, 0); | |
return(TRUE); | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
void Dlg_OnCommand(HWND hWnd, int id, HWND hWndCtl, UINT codeNotify) { | |
switch (id) { | |
case IDCANCEL: | |
EndDialog(hWnd, id); | |
break; | |
} | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
INT_PTR WINAPI Dlg_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { | |
if (uMsg == g_uMsgAppInstCountUpdate) { | |
SetDlgItemInt(hWnd, IDC_COUNT, g_lApplicationInstances, FALSE); | |
} | |
switch (uMsg) { | |
chHANDLE_DLGMSG(hWnd, WM_INITDIALOG, Dlg_OnInitDialog); | |
chHANDLE_DLGMSG(hWnd, WM_COMMAND, Dlg_OnCommand); | |
} | |
return(FALSE); | |
} | |
/////////////////////////////////////////////////////////////////////////////// | |
int WINAPI _tWinMain(HINSTANCE hInstExe, HINSTANCE, PTSTR, int) { | |
// Get the numeric value of the systemwide window message used to notify | |
// all top-level windows when the module's usage count has changed. | |
g_uMsgAppInstCountUpdate = | |
RegisterWindowMessage(TEXT("MsgAppInstCountUpdate")); | |
// There is another instance of this application running | |
InterlockedExchangeAdd(&g_lApplicationInstances, 1); | |
DialogBox(hInstExe, MAKEINTRESOURCE(IDD_APPINST), NULL, Dlg_Proc); | |
// This instance of the application is terminating | |
InterlockedExchangeAdd(&g_lApplicationInstances, -1); | |
// Have all other instances update their display | |
PostMessage(HWND_BROADCAST, g_uMsgAppInstCountUpdate, 0, 0); | |
return(0); | |
} | |
//////////////////////////////// End of File ////////////////////////////////// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment