multinst.c
来自「一本已经绝版的好书」· C语言 代码 · 共 68 行
C
68 行
/************************************************************
Module name: MultInst.C
Notices: Copyright (c) 1995-1997 Jeffrey Richter
************************************************************/
#include "..\CmnHdr.H" /* See Appendix C. */
#include <windows.h>
#include <tchar.h>
#include "Resource.H"
/////////////////////////////////////////////////////////////
// We instruct the compiler to put the g_lUsageCount data
// variable in its own data section called Shared. We
// then instruct the linker that we want the data in this
// section to be shared by all instances of this application.
#pragma data_seg("Shared")
LONG g_lUsageCount = -1;
#pragma data_seg()
// Instruct the linker to make the Shared section
// readable, writable, and shared.
#pragma comment(linker, "/section:Shared,rws")
/////////////////////////////////////////////////////////////
int WINAPI _tWinMain (HINSTANCE hinstExe,
HINSTANCE hinstPrev, LPTSTR pszCmdLine, int nCmdShow) {
// An instance is running; increment the counter.
BOOL fFirstInstance =
(InterlockedIncrement(&g_lUsageCount) == 0);
chWARNIFUNICODEUNDERWIN95();
// If more than one instance is running, tell
// the user and terminate the program.
if (!fFirstInstance) {
MessageBox(NULL,
__TEXT("Application is already running - ")
__TEXT("Terminating this instance."),
__TEXT("Multiple Instance"),
MB_OK | MB_ICONINFORMATION);
} else {
// We are the only instance running;
// wait for the user to terminate us.
MessageBox(NULL,
__TEXT("Running first instance of application.\n")
__TEXT("Select OK to terminate."),
__TEXT("Multiple Instance"),
MB_OK | MB_ICONINFORMATION);
}
// We are no longer running; decrement the usage counter.
InterlockedDecrement(&g_lUsageCount);
return(0);
}
//////////////////////// End Of File ////////////////////////
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?