| 1 |
#include <windows.h> |
| 2 |
#include <Strsafe.h> |
| 3 |
|
| 4 |
int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, PWSTR lpCmdLine, int nCmdShow) { |
| 5 |
|
| 6 |
wchar_t wszAppPath[MAX_PATH]; |
| 7 |
wchar_t wszCmdPath[MAX_PATH]; |
| 8 |
wchar_t wszCmdArgs[8192]; |
| 9 |
wchar_t *pwszTail; |
| 10 |
PROCESS_INFORMATION pi; |
| 11 |
STARTUPINFO si = { 0 }; |
| 12 |
DWORD dwRet; |
| 13 |
|
| 14 |
si.cb = sizeof(STARTUPINFO); |
| 15 |
si.dwFlags = STARTF_USESHOWWINDOW; |
| 16 |
si.wShowWindow = SW_HIDE; |
| 17 |
|
| 18 |
|
| 19 |
dwRet = GetSystemDirectory(wszAppPath, MAX_PATH); |
| 20 |
if (dwRet == 0 || dwRet >= MAX_PATH) { |
| 21 |
MessageBox(NULL, L"Path too long - could not determine system directory.", |
| 22 |
NULL, MB_OK | MB_ICONEXCLAMATION); |
| 23 |
return 1; |
| 24 |
} |
| 25 |
dwRet = StringCchPrintf(wszCmdPath, MAX_PATH, L"%s\\cmd.exe", wszAppPath); |
| 26 |
if (FAILED(dwRet)) { |
| 27 |
MessageBox(NULL, L"Path too long - could not get path to command line interpreter (cmd.exe).", |
| 28 |
NULL, MB_OK | MB_ICONEXCLAMATION); |
| 29 |
return 1; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
dwRet = GetModuleFileName(NULL, wszAppPath, MAX_PATH); |
| 34 |
if (dwRet == 0 || dwRet >= MAX_PATH) { |
| 35 |
MessageBox(NULL, L"Path too long - place FontForge in another directory.", |
| 36 |
NULL, MB_OK | MB_ICONEXCLAMATION); |
| 37 |
return 1; |
| 38 |
} else if ((pwszTail = wcsrchr(wszAppPath, L'\\')) == NULL) { |
| 39 |
MessageBox(NULL, L"Could not determine executable location.", |
| 40 |
NULL, MB_OK | MB_ICONEXCLAMATION); |
| 41 |
return 1; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
*pwszTail = L'\0'; |
| 46 |
dwRet = StringCchPrintf(wszCmdArgs, sizeof(wszCmdArgs)/sizeof(wchar_t), L"/c \"\"%s\\fontforge.bat\" %s\"", |
| 47 |
wszAppPath, lpCmdLine); |
| 48 |
if (FAILED(dwRet)) { |
| 49 |
MessageBox(NULL, L"Command parameter is too long.", |
| 50 |
NULL, MB_OK | MB_ICONEXCLAMATION); |
| 51 |
return 1; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
dwRet = CreateProcess(wszCmdPath, wszCmdArgs, NULL, NULL, FALSE, |
| 56 |
CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi); |
| 57 |
if (dwRet == 0) { |
| 58 |
MessageBox(NULL, L"Could not launch FontForge.", NULL, MB_OK | MB_ICONEXCLAMATION); |
| 59 |
return 1; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
dwRet = WaitForSingleObject(pi.hProcess, 10000); |
| 64 |
if (dwRet == WAIT_OBJECT_0) { |
| 65 |
GetExitCodeProcess(pi.hProcess, &dwRet); |
| 66 |
if (dwRet != 0) { |
| 67 |
wchar_t buf[100]; |
| 68 |
StringCchPrintf(buf, sizeof(buf)/sizeof(wchar_t), L"Could not launch FontForge (exit code %d).", (int)dwRet); |
| 69 |
MessageBox(NULL, buf, NULL, MB_OK | MB_ICONEXCLAMATION); |
| 70 |
} |
| 71 |
} else { |
| 72 |
dwRet = 0; |
| 73 |
} |
| 74 |
|
| 75 |
CloseHandle(pi.hProcess); |
| 76 |
CloseHandle(pi.hThread); |
| 77 |
return dwRet; |
| 78 |
} |