File Name:Singleton.cpp
#include "stdafx.h"
#include "Singleton.h"
Singleton::singleton ()
{
strGUID = _t ("{d727bdb5-f86a-405b-910d-11ad2a4ca76e}");
Strmapfileguid = strGUID + _t ("_mapfile");
M_hsingleton = NULL;
}
Singleton::~singleton ()
{
if (M_hsingleton)
{
CloseHandle (M_hsingleton);
M_hsingleton = NULL;
}
}
void Singleton::appstart (const HWND & HWND) const
{
HANDLE M_hsingleton = NULL;
M_hsingleton = CreateMutex (NULL, TRUE, strGUID);
RECT RECT;
ZeroMemory (&rect, sizeof (rect));
:: GetWindowRect (HWnd, &rect);
if (GetLastError () = error_already_exists)
{
//Open shared file
HANDLE hhandle =:: OpenFileMapping ( File_map_all_access, FALSE, Strmapfileguid);
if (hhandle)
{
LPTSTR lpmapaddr = (LPTSTR):: MapViewOfFile ( Hhandle, file_map_all_access, 0, 0, 0);
CString Strbuffer (LPMAPADDR);
HWND holdwnd = (HWND) _tcstoul (strbuffer , NULL, 10);
:: SetForegroundWindow (Holdwnd);
To remove a kernel object mapping
UnmapViewOfFile (Hhandle);
CloseHandle (Hhandle);
Exit (0);
}
}
else//First Run program
{
int nnum = sizeof (HANDLE);
Create a file Map kernel object
HANDLE Hhandle =:: CreateFileMapping (Invalid_handle_value, NULL, page_readwrite, 0, sizeof (HANDLE), strmapfileguid);
if (Hhandle)
{
Copy data to a shared file
LPTSTR lpmapaddr = (LPTSTR):: MapViewOfFile (Hhandle, file_map_all_access, 0, 0, 0);
CString Strbuffer;
Strbuffer.format (_t ("%d"), hWnd);
_tcscpy (LPMAPADDR, Strbuffer);
All data written to the file mapping buffer is flushed to disk;
:: Flushviewoffile (Lpmapaddr, Strbuffer.getlength () +1);
}
}
}