Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / Win32

DWinLib - The Guts

Rate me:
Please Sign up or sign in to vote.
4.96/5 (8 votes)
17 Jan 2021CPOL28 min read 19K   374   15  
A little about how things work behind the scenes in DWinLib!
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer www.randommonkeyworks.com
United States United States
I am the author of Laughing at the Devil: One Man’s Religious Discoveries. If you want to understand the astronomic investigations of our priests 3,000 years ago, LATD is the book to turn to. It opens up the thoughts that pushed them away from their earlier polytheism and towards our current definition of God.

Trained as a mechanical engineer, I have been involved with design, supervision, and project management. I taught myself C++ programming in order to play around with binaural beats more easily. I've also created various databases to help with project management and personal tasks.

Databases are cool and extremely useful! Happy coding, everybody!

Comments and Discussions