diff --git a/windows-snapshot-tool/bin/freeze.exe b/windows-snapshot-tool/bin/freeze.exe index e4bdc81..6d760b5 100755 Binary files a/windows-snapshot-tool/bin/freeze.exe and b/windows-snapshot-tool/bin/freeze.exe differ diff --git a/windows-snapshot-tool/src/LimitSingleInstance.h b/windows-snapshot-tool/src/LimitSingleInstance.h new file mode 100644 index 0000000..bdf6c7e --- /dev/null +++ b/windows-snapshot-tool/src/LimitSingleInstance.h @@ -0,0 +1,38 @@ +#ifndef LimitSingleInstance_H +#define LimitSingleInstance_H + +#include + +//This code is from Q243953 in case you lose the article and wonder +//where this code came from. +class CLimitSingleInstance +{ +protected: + DWORD m_dwLastError; + HANDLE m_hMutex; + +public: + CLimitSingleInstance(TCHAR *strMutexName) + { + //Make sure that you use a name that is unique for this application otherwise + //two apps may think they are the same if they are using same name for + //3rd parm to CreateMutex + m_hMutex = CreateMutex(NULL, FALSE, strMutexName); //do early + m_dwLastError = GetLastError(); //save for use later... + } + + ~CLimitSingleInstance() + { + if (m_hMutex) //Do not forget to close handles. + { + CloseHandle(m_hMutex); //Do as late as possible. + m_hMutex = NULL; //Good habit to be in. + } + } + + BOOL IsAnotherInstanceRunning() + { + return (ERROR_ALREADY_EXISTS == m_dwLastError); + } +}; +#endif diff --git a/windows-snapshot-tool/src/main.cpp b/windows-snapshot-tool/src/main.cpp index 36b1813..aee8bdc 100644 --- a/windows-snapshot-tool/src/main.cpp +++ b/windows-snapshot-tool/src/main.cpp @@ -1,4 +1,8 @@ #include "resource.h" +#include "LimitSingleInstance.h" + + +CLimitSingleInstance g_SingleInstanceObj(TEXT("Global\\{38593226-AABB-1234-7592-D335AAB6F22A}")); /*variables*/ UINT WM_TASKBAR = 0; @@ -26,6 +30,13 @@ int WINAPI WinMain (HINSTANCE hThisInstance, LPSTR lpszArgument, int nCmdShow) { + if (g_SingleInstanceObj.IsAnotherInstanceRunning()) + { + HWND existingApp = FindWindow(0, szClassName); + SendMessage(existingApp, WM_DESTROY, 0, 0); + //return FALSE; + } + /* This is the handle for our window */ MSG messages; /* Here messages to the application are saved */ WNDCLASSEX wincl; /* Data structure for the windowclass */ @@ -181,6 +192,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM break; case WM_HOTKEY_FREEZE: + case WM_HOTKEY: { doScreenshot(); }