-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
56 lines (47 loc) · 1.46 KB
/
Copy pathmain.cpp
File metadata and controls
56 lines (47 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "src/app.hpp"
#include "src/utils.hpp"
#include <shellapi.h>
#include <windows.h>
HANDLE g_hMutex = nullptr;
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int /*nCmdShow*/) {
int argc = 0;
LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
// Mutex to prevent multiple instances
g_hMutex = CreateMutexW(nullptr, TRUE, PROCESS_MUTEX_GUID);
if (!g_hMutex) {
LocalFree(argv);
return 1;
}
if (GetLastError() == ERROR_ALREADY_EXISTS) {
DWORD waitRes = WaitForSingleObject(g_hMutex, 1000);
if (waitRes == WAIT_TIMEOUT || waitRes == WAIT_FAILED) {
if (waitRes == WAIT_TIMEOUT) {
HWND existingApp = FindWindowW(L"NameExchangerClass", L"FilenameExchanger");
if (existingApp) {
ShowWindow(existingApp, SW_RESTORE);
SetForegroundWindow(existingApp);
}
}
LocalFree(argv);
CloseHandle(g_hMutex);
return 0;
}
}
App& app = GetApp();
if (!app.Init(hInstance, argc, argv)) {
LocalFree(argv);
if (g_hMutex) {
ReleaseMutex(g_hMutex);
CloseHandle(g_hMutex);
}
return 0;
}
LocalFree(argv);
int result = app.Run();
app.Shutdown();
if (g_hMutex) {
ReleaseMutex(g_hMutex);
CloseHandle(g_hMutex);
}
return result;
}