16,551
社区成员
发帖
与我相关
我的任务
分享
#pragma data_seg("Shared")
volatile int g_nInstance = 0;
#pragma data_seg()
#pragma comment(linker, "/SECTION:Shared, RWS")
...
...
BOOL CMy2Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
g_nInstance++;
return TRUE; // return TRUE unless you set the focus to a control
}
清单17-1 AppInst示例应用程序
/******************************************************************************
Module: AppInst.cpp
Notices: Copyright (c) 2000 Jeffrey Richter
******************************************************************************/
#include "..\CmnHdr.h" /* See Appendix A. */
#include <windowsx.h>
#include <tchar.h>
#include "Resource.h"
///////////////////////////////////////////////////////////////////////////////
// The system-wide unique window message
UINT g_uMsgAppInstCountUpdate = INVALID_ATOM;
///////////////////////////////////////////////////////////////////////////////
// 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 pszCmdLine, 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((PLONG) &g_lApplicationInstances, 1);
DialogBox(hinstExe, MAKEINTRESOURCE(IDD_APPINST), NULL, Dlg_Proc);
// This instance of the application is terminating
InterlockedExchangeAdd((PLONG) &g_lApplicationInstances, -1);
// Have all other instances update their display
PostMessage(HWND_BROADCAST, g_uMsgAppInstCountUpdate, 0, 0);
return(0);
}
//////////////////////////////// End of File //////////////////////////////////
#include "Hook.h"
#include <stdio.h>
#pragma data_seg(".myshare")
DWORD g_nInstance = 0;
#pragma data_seg()
#pragma comment(linker,"/SECTION:.myshare,RWS")
void RunningProc()
{
g_nInstance++;
}
DWORD GetCount()
{
return g_nInstance;
}
#ifndef _count_H
#define _count_H
#include <windows.h>
void RunningProc();
DWORD GetCount();
#endif
; count.def : Declares the module parameters for the DLL.
LIBRARY "count"
DESCRIPTION 'count Windows Dynamic Link Library'
EXPORTS
; Explicit exports can go here
RunningProc @1
GetCount @2