forked from snowie2000/mactype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.cpp
280 lines (237 loc) · 6.52 KB
/
run.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// dll injection
#define _CRT_SECURE_NO_DEPRECATE 1
#define WINVER 0x500
#define _WIN32_WINNT 0x500
#define _WIN32_IE 0x601
#define WIN32_LEAN_AND_MEAN 1
#define UNICODE 1
#define _UNICODE 1
#include <Windows.h>
#include <ShellApi.h>
#include <ComDef.h>
#include <ShlObj.h>
#include <ShLwApi.h>
#include <tchar.h>
#include "array.h"
#include <strsafe.h>
#include "gdiexe.rc"
// _vsnwprintf用
#include <wchar.h>
#include <stdarg.h>
#define _CRTDBG_MAP_ALLOC
#include <cstdlib>
#include <malloc.h>
#include <crtdbg.h>
#define for if(0);else for
#ifndef _countof
#define _countof(array) (sizeof(array) / sizeof((array)[0]))
#endif
#pragma comment(linker, "/subsystem:windows,5.0")
#pragma comment(lib, "Kernel32.lib")
#pragma comment(lib, "User32.lib")
#pragma comment(lib, "Shell32.lib")
#pragma comment(lib, "ShLwApi.lib")
#pragma comment(lib, "Ole32.lib")
static void errmsg(UINT id, DWORD code)
{
char buffer [512];
char format [128];
LoadStringA(GetModuleHandleA(NULL), id, format, 128);
wnsprintfA(buffer, 512, format, code);
MessageBoxA(NULL, buffer, "MacType ERROR", MB_OK|MB_ICONSTOP);
}
inline HRESULT HresultFromLastError()
{
DWORD dwErr = GetLastError();
return HRESULT_FROM_WIN32(dwErr);
}
//#include <detours.h>
HINSTANCE hinstDLL;
#include <stddef.h>
#define GetDLLInstance() (hinstDLL)
#define _GDIPP_EXE
#define _GDIPP_RUN_CPP
#include "supinfo.h"
static BOOL (WINAPI * ORIG_CreateProcessW)(LPCWSTR lpApp, LPWSTR lpCmd, LPSECURITY_ATTRIBUTES pa, LPSECURITY_ATTRIBUTES ta, BOOL bInherit, DWORD dwFlags, LPVOID lpEnv, LPCWSTR lpDir, LPSTARTUPINFOW psi, LPPROCESS_INFORMATION ppi)
= CreateProcessW;
static BOOL WINAPI IMPL_CreateProcessW(LPCWSTR lpApp, LPWSTR lpCmd, LPSECURITY_ATTRIBUTES pa, LPSECURITY_ATTRIBUTES ta, BOOL bInherit, DWORD dwFlags, LPVOID lpEnv, LPCWSTR lpDir, LPSTARTUPINFOW psi, LPPROCESS_INFORMATION ppi)
{
return _CreateProcessAorW(lpApp, lpCmd, pa, ta, bInherit, dwFlags, lpEnv, lpDir, psi, ppi, ORIG_CreateProcessW);
}
//#define OLD_PSDK
#ifdef OLD_PSDK
extern "C" {
HRESULT WINAPI _SHILCreateFromPath(LPCWSTR pszPath, LPITEMIDLIST *ppidl, DWORD *rgflnOut)
{
if (!pszPath || !ppidl) {
return E_INVALIDARG;
}
LPSHELLFOLDER psf;
HRESULT hr = ::SHGetDesktopFolder(&psf);
if (hr != NOERROR) {
return hr;
}
ULONG chEaten;
LPOLESTR lpszDisplayName = ::StrDupW(pszPath);
hr = psf->ParseDisplayName(NULL, NULL, lpszDisplayName, &chEaten, ppidl, rgflnOut);
::LocalFree(lpszDisplayName);
psf->Release();
return hr;
}
void WINAPI _SHFree(void *pv)
{
if (!pv) {
return;
}
LPMALLOC pMalloc = NULL;
if (::SHGetMalloc(&pMalloc) == NOERROR) {
pMalloc->Free(pv);
pMalloc->Release();
}
}
}
#else
#define _SHILCreateFromPath SHILCreateFromPath
#define _SHFree SHFree
#endif
// 1つ目の引数だけファイルとして扱い、実行する。
//
// コマンドは こんな感じで連結されます。
// exe linkpath linkarg cmdarg2 cmdarg3 cmdarg4 ...
//
static HRESULT HookAndExecute(int show)
{
int argc = 0;
LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if(!argv) {
return HresultFromLastError();
}
if(argc <= 1) {
char buffer [256];
LoadStringA(GetModuleHandleA(NULL), IDS_USAGE, buffer, 256);
MessageBoxA(NULL,
buffer
,"MacType", MB_OK|MB_ICONINFORMATION);
LocalFree(argv);
return S_OK;
}
int i;
size_t length = 1;
for(i=2; i<argc; i++) {
length += wcslen(argv[i]) + 3;
}
LPWSTR cmdline = (WCHAR*)calloc(sizeof(WCHAR), length);
if(!cmdline) {
LocalFree(argv);
return E_OUTOFMEMORY;
}
LPWSTR p = cmdline;
*p = L'\0';
for(i=2; i<argc; i++) {
const bool dq = !!wcschr(argv[i], L' ');
if (dq) {
*p++ = '"';
length--;
}
StringCchCopyExW(p, length, argv[i], &p, &length, STRSAFE_NO_TRUNCATION);
if (dq) {
*p++ = '"';
length--;
}
*p++ = L' ';
length--;
}
*CharPrevW(cmdline, p) = L'\0';
WCHAR file[MAX_PATH], dir[MAX_PATH];
GetCurrentDirectoryW(_countof(dir), dir);
StringCchCopyW(file, _countof(file), argv[1]);
if(PathIsRelativeW(file)) {
PathCombineW(file, dir, file);
} else {
WCHAR gdippDir[MAX_PATH];
GetModuleFileNameW(NULL, gdippDir, _countof(gdippDir));
PathRemoveFileSpec(gdippDir);
// カレントディレクトリがgdi++.exeの置かれているディレクトリと同じだったら、
// 起動しようとしているEXEのフルパスから抜き出したディレクトリ名をカレント
// ディレクトリとして起動する。(カレントディレクトリがEXEと同じ場所である
// 前提で作られているアプリ対策)
if (wcscmp(dir, gdippDir) == 0) {
StringCchCopyW(dir, _countof(dir), argv[1]);
PathRemoveFileSpec(dir);
}
}
LocalFree(argv);
argv = NULL;
#ifdef _DEBUG
if((GetAsyncKeyState(VK_CONTROL) & 0x8000)
&& MessageBoxW(NULL, cmdline, NULL, MB_YESNO) != IDYES) {
free(cmdline);
return NOERROR;
}
#endif
LPITEMIDLIST pidl = NULL;
HRESULT hr;
//fileのアイテムIDリストを取得
hr = _SHILCreateFromPath(file, &pidl, NULL);
if(SUCCEEDED(hr) && pidl) {
//SEE_MASK_INVOKEIDLISTを使うと
//explorerでクリックして起動したのと同じ動作になる
SHELLEXECUTEINFOW sei = { sizeof(SHELLEXECUTEINFOW) };
sei.fMask = SEE_MASK_INVOKEIDLIST
| SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
| SEE_MASK_DOENVSUBST
| SEE_MASK_WAITFORINPUTIDLE;
sei.hwnd = GetDesktopWindow();
sei.lpParameters = cmdline;
sei.lpDirectory = dir;
sei.nShow = show;
sei.lpIDList = pidl;
//ShellExecuteExWが内部で呼び出すCreateProcessWをフックして
//HookChildProcesses相当の処理を行う
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)ORIG_CreateProcessW, IMPL_CreateProcessW);
hr = DetourTransactionCommit();
if(hr == NOERROR) {
if(ShellExecuteExW(&sei)) {
hr = S_OK;
} else {
hr = HresultFromLastError();
}
}
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)ORIG_CreateProcessW, IMPL_CreateProcessW);
DetourTransactionCommit();
}
if(pidl)
_SHFree(pidl);
free(cmdline);
return hr;
}
int WINAPI wWinMain(HINSTANCE ins, HINSTANCE prev, LPWSTR cmd, int show)
{
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
OleInitialize(NULL);
WCHAR path [MAX_PATH];
if(GetModuleFileNameW(NULL, path, _countof(path))) {
PathRenameExtensionW(path, L".dll");
//DONT_RESOLVE_DLL_REFERENCESを指定すると依存関係の解決や
//DllMainの呼び出しが行われない
hinstDLL = LoadLibraryExW(path, NULL, DONT_RESOLVE_DLL_REFERENCES);
}
if(!hinstDLL) {
errmsg(IDS_DLL, HresultFromLastError());
} else {
PathRemoveFileSpecW(path);
SetCurrentDirectoryW(path);
HRESULT hr = HookAndExecute(show);
if(hr != S_OK) {
errmsg(IDC_EXEC, hr);
}
}
OleUninitialize();
return 0;
}
//EOF