In this article, you will get an overview of the internal workings of DWinLib, a semi-simple wrapper for the Windows API.
|
// --------------------------------------------------------------------------------------- //
// This is the entry unit into the BareApp program. It checks for multiple instances and //
// sets up the DWinLib application. //
// --------------------------------------------------------------------------------------- //
#include "PrecompiledHeaders.h"
#pragma hdrstop
#include "DwlApp.h"
#include "CharArrayWrappers.h"
//The method used to determine if there is another instance of this application running is
//http://www.codeproject.com/cpp/avoidmultinstance.asp. Note that this gives a single
//instance per machine, not a single instance per user. Newcomer's article shows how to
//extend this for the second requirement.
uint UWM_YOU_ARE_ME;
BOOL CALLBACK searcher(HWND hwnd, LPARAM lParam) {
DWORD result;
LRESULT ok = SendMessageTimeout(hwnd, UWM_YOU_ARE_ME, 0, 0, SMTO_BLOCK |
SMTO_ABORTIFHUNG, 200, &result);
if (!ok) return TRUE;
if (result == UWM_YOU_ARE_ME) {
HWND * target = (HWND *)lParam;
*target = hwnd;
return FALSE;
}
return TRUE;
}
int WINAPI WinMain(HINSTANCE inst1, HINSTANCE inst2, LPSTR str, int show) {
//For your own app, change the text accordingly using GUIDGEN.
UWM_YOU_ARE_ME = RegisterWindowMessage(_T("MyApp-AE383CB7-EA7D-4f75-BAE5-0B46E2453DF3"));
//Check to see if it is already running. ERROR_ACCESS_DENIED is returned when
//two or more user sessions are running on a computer, and it is running in another
//session than the one the user is currently logged onto. Of course, I don't believe
//Windows allows you to 'Show' an app another user is running in your current session,
//so you will not be able to run it in a second session. Hmmmm....
HANDLE mutex = CreateMutex(NULL, FALSE,
_T("MyAppA-AE383CB7-EA7D-4f75-BAE5-0B46E2453DF3"));
bool alreadyRunning = (GetLastError() == ERROR_ALREADY_EXISTS ||
GetLastError() == ERROR_ACCESS_DENIED);
if (alreadyRunning) { // kill this one
Sleep(10); //Give other window time to open up if necessary
HWND window = NULL;
EnumWindows(searcher, (LPARAM)&window);
if (window != NULL) {
SetForegroundWindow(window);
if (IsIconic(window)) ShowWindow(window, SW_RESTORE);
if (*str != '\0') {
COPYDATASTRUCT cds;
cds.dwData = UWM_YOU_ARE_ME;
cds.cbData = s_cast<int>(strlen(str)+1);
cds.lpData = str;
ShowWindow(window, SW_SHOWNORMAL);
SendMessage(window, WM_COPYDATA, 0, (LPARAM)&cds);
}
}
return FALSE;
}
FlexiCharWrap winTitle(_T("Stupid Squares"));
DwlMdiApp app(inst1, inst2, str, &winTitle, show);
WPARAM ret = app.run();
CloseHandle(mutex); //Technically, you don't have to do this. Windows will close it
//automatically upon process termination.
return s_cast<int>(ret);
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.