-
Notifications
You must be signed in to change notification settings - Fork 1k
/
StubExecutable.cpp
120 lines (93 loc) · 2.67 KB
/
StubExecutable.cpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// StubExecutable.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "StubExecutable.h"
#include "semver200.h"
using namespace std;
wchar_t* FindRootAppDir()
{
wchar_t* ourDirectory = new wchar_t[MAX_PATH];
GetModuleFileName(GetModuleHandle(NULL), ourDirectory, MAX_PATH);
wchar_t* lastSlash = wcsrchr(ourDirectory, L'\\');
if (!lastSlash) {
delete[] ourDirectory;
return NULL;
}
// Null-terminate the string at the slash so now it's a directory
*lastSlash = 0x0;
return ourDirectory;
}
wchar_t* FindOwnExecutableName()
{
wchar_t* ourDirectory = new wchar_t[MAX_PATH];
GetModuleFileName(GetModuleHandle(NULL), ourDirectory, MAX_PATH);
wchar_t* lastSlash = wcsrchr(ourDirectory, L'\\');
if (!lastSlash) {
delete[] ourDirectory;
return NULL;
}
wchar_t* ret = _wcsdup(lastSlash + 1);
delete[] ourDirectory;
return ret;
}
std::wstring FindLatestAppDir()
{
std::wstring ourDir;
ourDir.assign(FindRootAppDir());
ourDir += L"\\app-*";
WIN32_FIND_DATA fileInfo = { 0 };
HANDLE hFile = FindFirstFile(ourDir.c_str(), &fileInfo);
if (hFile == INVALID_HANDLE_VALUE) {
return NULL;
}
version::Semver200_version acc("0.0.0");
std::wstring acc_s;
do {
std::wstring appVer = fileInfo.cFileName;
appVer = appVer.substr(4); // Skip 'app-'
if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
continue;
}
std::string s(appVer.begin(), appVer.end());
version::Semver200_version thisVer(s);
if (thisVer > acc) {
acc = thisVer;
acc_s = appVer;
}
} while (FindNextFile(hFile, &fileInfo));
if (acc == version::Semver200_version("0.0.0")) {
return NULL;
}
ourDir.assign(FindRootAppDir());
std::wstringstream ret;
ret << ourDir << L"\\app-" << acc_s;
FindClose(hFile);
return ret.str();
}
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
std::wstring appName;
appName.assign(FindOwnExecutableName());
std::wstring workingDir(FindLatestAppDir());
std::wstring fullPath(workingDir + L"\\" + appName);
STARTUPINFO si = { 0 };
PROCESS_INFORMATION pi = { 0 };
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = nCmdShow;
std::wstring cmdLine(L"\"");
cmdLine += fullPath;
cmdLine += L"\" ";
cmdLine += lpCmdLine;
wchar_t* lpCommandLine = wcsdup(cmdLine.c_str());
wchar_t* lpCurrentDirectory = wcsdup(workingDir.c_str());
if (!CreateProcess(NULL, lpCommandLine, NULL, NULL, true, 0, NULL, lpCurrentDirectory, &si, &pi)) {
return -1;
}
AllowSetForegroundWindow(pi.dwProcessId);
WaitForInputIdle(pi.hProcess, 5 * 1000);
return 0;
}