| 1 | /** |
|---|
| 2 | * Padre Minimal Win32 Executable Launcher |
|---|
| 3 | * @author Ahmad M. Zawawi <ahmad.zawawi@gmail.com> |
|---|
| 4 | * @author Olivier Mengué <dolmen@cpan.org> |
|---|
| 5 | */ |
|---|
| 6 | #define WIN32_LEAN_AND_MEAN |
|---|
| 7 | #define STRICT |
|---|
| 8 | #include <windows.h> |
|---|
| 9 | #include <tchar.h> |
|---|
| 10 | |
|---|
| 11 | #include "padre-rc.h" |
|---|
| 12 | |
|---|
| 13 | static void LocalizedMessageBox(LPCTSTR lpMessage, LPCTSTR lpTitle, DWORD dwFlags) |
|---|
| 14 | { |
|---|
| 15 | HMODULE hModule; |
|---|
| 16 | TCHAR szTitle[256]; |
|---|
| 17 | TCHAR szMessage[256]; |
|---|
| 18 | |
|---|
| 19 | hModule = GetModuleHandle(NULL); |
|---|
| 20 | if (IS_INTRESOURCE(lpMessage)) { |
|---|
| 21 | LoadString(hModule, (UINT)lpMessage, szMessage, sizeof(szMessage)/sizeof(szMessage[0])); |
|---|
| 22 | lpMessage = szMessage; |
|---|
| 23 | } |
|---|
| 24 | if (IS_INTRESOURCE(lpTitle)) { |
|---|
| 25 | LoadString(hModule, (UINT)lpTitle, szTitle, sizeof(szTitle)/sizeof(szTitle[0])); |
|---|
| 26 | lpTitle = szTitle; |
|---|
| 27 | } |
|---|
| 28 | MessageBox(NULL, lpMessage, lpTitle, dwFlags); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | static BOOL FileExists(LPCTSTR lpFileName) |
|---|
| 32 | { |
|---|
| 33 | DWORD att = GetFileAttributes(lpFileName); |
|---|
| 34 | return (att != INVALID_FILE_ATTRIBUTES); //&& (att & (FILE_ATTRIBUTE_DEVICE|FILE_ATTRIBUTE_DIRECTORY) == 0); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | * When called by windows, we simply launch Padre from here |
|---|
| 40 | */ |
|---|
| 41 | #ifdef Mini1 |
|---|
| 42 | VOID WINAPI __main(VOID) |
|---|
| 43 | #elif defined(Mini2) |
|---|
| 44 | VOID WINAPI WinMainCRTStartup(VOID) |
|---|
| 45 | #else |
|---|
| 46 | int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, |
|---|
| 47 | LPSTR lpCmdLineArgs, int nCmdShow) |
|---|
| 48 | #endif |
|---|
| 49 | { |
|---|
| 50 | // Padre.exe path |
|---|
| 51 | TCHAR szExePath[MAX_PATH]; |
|---|
| 52 | // WPerl.exe path |
|---|
| 53 | TCHAR szWPerl[MAX_PATH]; |
|---|
| 54 | // Padre script path |
|---|
| 55 | TCHAR szPadre[MAX_PATH]; |
|---|
| 56 | // WPerl Command line |
|---|
| 57 | TCHAR szCmdLine[1024+1]; |
|---|
| 58 | HMODULE hModule; |
|---|
| 59 | STARTUPINFO si; |
|---|
| 60 | PROCESS_INFORMATION pi; |
|---|
| 61 | BOOL bSuccess; |
|---|
| 62 | DWORD dwLength; |
|---|
| 63 | LPCTSTR lpArgs; |
|---|
| 64 | |
|---|
| 65 | hModule = GetModuleHandle(NULL); |
|---|
| 66 | //Find the the executable's path |
|---|
| 67 | dwLength = GetModuleFileName(hModule, szExePath, sizeof(szExePath)/sizeof(szExePath[0])); |
|---|
| 68 | if (dwLength) { |
|---|
| 69 | while (dwLength && szExePath[ dwLength ] != '\\' && szExePath[ dwLength ] != '/') { |
|---|
| 70 | dwLength--; |
|---|
| 71 | } |
|---|
| 72 | szExePath[ dwLength + 1 ] = '\0'; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | #if 0 |
|---|
| 76 | lstrcpy(szWPerl, _T("C:\\strawberry\\perl\\bin\\wperl.exe")); |
|---|
| 77 | #else |
|---|
| 78 | lstrcpy(szWPerl, szExePath); |
|---|
| 79 | lstrcpy(&szWPerl[dwLength+1], _T("wperl.exe")); |
|---|
| 80 | #endif |
|---|
| 81 | |
|---|
| 82 | //At this point we should check if padre script exists or not |
|---|
| 83 | if (! FileExists(szWPerl)) { |
|---|
| 84 | LocalizedMessageBox(MAKEINTRESOURCE(IDS_ERR_WPERL), MAKEINTRESOURCE(IDS_APP_TITLE), MB_OK|MB_ICONERROR); |
|---|
| 85 | ExitProcess(1); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | // Build the 'padre' script path |
|---|
| 90 | lstrcpy(szPadre, szExePath); |
|---|
| 91 | lstrcpy(&szPadre[dwLength+1], _T("padre")); |
|---|
| 92 | |
|---|
| 93 | //At this point we should check if padre script exists or not |
|---|
| 94 | if (! FileExists(szPadre)) { |
|---|
| 95 | LocalizedMessageBox(MAKEINTRESOURCE(IDS_ERR_SCRIPT), MAKEINTRESOURCE(IDS_APP_TITLE), MB_OK|MB_ICONERROR); |
|---|
| 96 | ExitProcess(1); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | lpArgs = GetCommandLine(); |
|---|
| 100 | //LocalizedMessageBox(lpArgs, MAKEINTRESOURCE(IDS_APP_TITLE), MB_OK|MB_ICONINFORMATION); |
|---|
| 101 | do { |
|---|
| 102 | while (*lpArgs == _T(' ') || *lpArgs == _T('\t')) lpArgs++; |
|---|
| 103 | if (!*lpArgs) break; |
|---|
| 104 | if (*lpArgs == _T('"')) { |
|---|
| 105 | lpArgs++; |
|---|
| 106 | while (*lpArgs && *lpArgs != _T('"')) lpArgs++; |
|---|
| 107 | if (*lpArgs == _T('"')) lpArgs++; |
|---|
| 108 | } else { |
|---|
| 109 | while (*lpArgs && *lpArgs != _T(' ') && *lpArgs != _T('\t')) lpArgs++; |
|---|
| 110 | } |
|---|
| 111 | } while (0); |
|---|
| 112 | // Build the command line |
|---|
| 113 | wsprintf(szCmdLine, "\"%s\" \"%s\"%s", szWPerl, szPadre, lpArgs); |
|---|
| 114 | szCmdLine[(sizeof(szCmdLine)/sizeof(szCmdLine[0]))-1] = '\0'; |
|---|
| 115 | |
|---|
| 116 | ZeroMemory( &pi, sizeof(pi) ); |
|---|
| 117 | GetStartupInfo(&si); |
|---|
| 118 | //LocalizedMessageBox(szCmdLine, MAKEINTRESOURCE(IDS_APP_TITLE), MB_OK|MB_ICONINFORMATION); |
|---|
| 119 | bSuccess = CreateProcess(szWPerl, |
|---|
| 120 | szCmdLine, |
|---|
| 121 | NULL, |
|---|
| 122 | NULL, |
|---|
| 123 | TRUE, |
|---|
| 124 | GetPriorityClass(hModule), |
|---|
| 125 | GetEnvironmentStrings(), |
|---|
| 126 | NULL, |
|---|
| 127 | &si, |
|---|
| 128 | &pi); |
|---|
| 129 | if (bSuccess) { |
|---|
| 130 | //WaitForSingleObject( pi.hProcess, INFINITE ); |
|---|
| 131 | CloseHandle(pi.hProcess); |
|---|
| 132 | CloseHandle(pi.hThread); |
|---|
| 133 | } else { |
|---|
| 134 | LocalizedMessageBox(MAKEINTRESOURCE(IDS_ERR_RUN), MAKEINTRESOURCE(IDS_APP_TITLE), MB_OK|MB_ICONERROR); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | // The application's return value |
|---|
| 138 | ExitProcess(0); |
|---|
| 139 | } |
|---|
| 140 | /** |
|---|
| 141 | # Copyright 2008-2009 The Padre development team as listed in Padre.pm. |
|---|
| 142 | # LICENSE |
|---|
| 143 | # This program is free software; you can redistribute it and/or |
|---|
| 144 | # modify it under the same terms as Perl 5 itself. |
|---|
| 145 | */ |
|---|
| 146 | /* vim:set ts=4 sts=4 sw=4: */ |
|---|