DLLLauncherMain.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2012 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. // This file contains code for a launcher executable for WebKit apps. When compiled into foo.exe, it
  26. // will set PATH so that Apple Application Support DLLs can be found, then will load foo.dll and
  27. // call its dllLauncherEntryPoint function, which should be declared like so:
  28. // extern "C" __declspec(dllexport) int WINAPI dllLauncherEntryPoint(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpstCmdLine, int nCmdShow);
  29. // If USE_CONSOLE_ENTRY_POINT is defined, this function will be called instead:
  30. // extern "C" __declspec(dllexport) int WINAPI dllLauncherEntryPoint(int argc, const char* argv[]);
  31. #include <shlwapi.h>
  32. #include <string>
  33. #include <vector>
  34. #include <windows.h>
  35. using namespace std;
  36. #if defined _M_IX86
  37. #define PROCESSORARCHITECTURE "x86"
  38. #elif defined _M_IA64
  39. #define PROCESSORARCHITECTURE "ia64"
  40. #elif defined _M_X64
  41. #define PROCESSORARCHITECTURE "amd64"
  42. #else
  43. #define PROCESSORARCHITECTURE "*"
  44. #endif
  45. #pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='" PROCESSORARCHITECTURE "' publicKeyToken='6595b64144ccf1df' language='*'\"")
  46. #if defined(_MSC_VER) && (_MSC_VER >= 1600)
  47. #pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.VC80.CRT' version='8.0.50727.6195' processorArchitecture='" PROCESSORARCHITECTURE "' publicKeyToken='1fc8b3b9a1e18e3b' language='*'\"")
  48. #endif
  49. static void enableTerminationOnHeapCorruption()
  50. {
  51. // Enable termination on heap corruption on OSes that support it (Vista and XPSP3).
  52. // http://msdn.microsoft.com/en-us/library/aa366705(VS.85).aspx
  53. HEAP_INFORMATION_CLASS heapEnableTerminationOnCorruption = static_cast<HEAP_INFORMATION_CLASS>(1);
  54. HMODULE module = ::GetModuleHandleW(L"kernel32.dll");
  55. if (!module)
  56. return;
  57. typedef BOOL (WINAPI*HSI)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T);
  58. HSI heapSetInformation = reinterpret_cast<HSI>(::GetProcAddress(module, "HeapSetInformation"));
  59. if (!heapSetInformation)
  60. return;
  61. heapSetInformation(0, heapEnableTerminationOnCorruption, 0, 0);
  62. }
  63. static wstring getStringValue(HKEY key, const wstring& valueName)
  64. {
  65. DWORD type = 0;
  66. DWORD bufferSize = 0;
  67. if (::RegQueryValueExW(key, valueName.c_str(), 0, &type, 0, &bufferSize) != ERROR_SUCCESS || type != REG_SZ)
  68. return wstring();
  69. vector<wchar_t> buffer(bufferSize / sizeof(wchar_t));
  70. if (::RegQueryValueExW(key, valueName.c_str(), 0, &type, reinterpret_cast<LPBYTE>(&buffer[0]), &bufferSize) != ERROR_SUCCESS)
  71. return wstring();
  72. return &buffer[0];
  73. }
  74. static wstring applePathFromRegistry(const wstring& key, const wstring& value)
  75. {
  76. HKEY applePathKey = 0;
  77. if (::RegOpenKeyExW(HKEY_LOCAL_MACHINE, key.c_str(), 0, KEY_READ, &applePathKey) != ERROR_SUCCESS)
  78. return wstring();
  79. wstring path = getStringValue(applePathKey, value);
  80. ::RegCloseKey(applePathKey);
  81. return path;
  82. }
  83. static wstring appleApplicationSupportDirectory()
  84. {
  85. return applePathFromRegistry(L"SOFTWARE\\Apple Inc.\\Apple Application Support", L"InstallDir");
  86. }
  87. static wstring copyEnvironmentVariable(const wstring& variable)
  88. {
  89. DWORD length = ::GetEnvironmentVariableW(variable.c_str(), 0, 0);
  90. if (!length)
  91. return wstring();
  92. vector<wchar_t> buffer(length);
  93. if (!GetEnvironmentVariable(variable.c_str(), &buffer[0], buffer.size()) || !buffer[0])
  94. return wstring();
  95. return &buffer[0];
  96. }
  97. static bool prependPath(const wstring& directoryToPrepend)
  98. {
  99. wstring pathVariable = L"PATH";
  100. wstring oldPath = copyEnvironmentVariable(pathVariable);
  101. wstring newPath = directoryToPrepend + L';' + oldPath;
  102. return ::SetEnvironmentVariableW(pathVariable.c_str(), newPath.c_str());
  103. }
  104. static int fatalError(const wstring& programName, const wstring& message)
  105. {
  106. wstring caption = programName + L" can't open.";
  107. ::MessageBoxW(0, message.c_str(), caption.c_str(), MB_ICONERROR);
  108. return 1;
  109. }
  110. static bool modifyPath(const wstring& programName)
  111. {
  112. struct {
  113. wstring softwareSubKey;
  114. wstring productName;
  115. } products[] = {
  116. { L"Apple Inc.", L"Apple Application Support" },
  117. { L"Apple Computer, Inc.", L"Safari" },
  118. };
  119. wstring pathPrefix;
  120. for (size_t i = 0; i < _countof(products); ++i) {
  121. wstring directory = applePathFromRegistry(L"SOFTWARE\\" + products[i].softwareSubKey + L"\\" + products[i].productName, L"InstallDir");
  122. if (directory.empty()) {
  123. fatalError(programName, L"Failed to determine path to " + products[i].productName + L" directory.");
  124. return false;
  125. }
  126. if (i)
  127. pathPrefix += L';';
  128. pathPrefix += directory;
  129. }
  130. if (prependPath(pathPrefix))
  131. return true;
  132. fatalError(programName, L"Failed to modify PATH environment variable.");
  133. return false;
  134. }
  135. #if USE_CONSOLE_ENTRY_POINT
  136. int main(int argc, const char* argv[])
  137. #else
  138. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpstrCmdLine, int nCmdShow)
  139. #endif
  140. {
  141. enableTerminationOnHeapCorruption();
  142. // Get the path of our executable.
  143. wchar_t exePath[MAX_PATH];
  144. if (!::GetModuleFileNameW(0, exePath, _countof(exePath)))
  145. return fatalError(L"Unknown Program", L"Failed to determine name of executable.");
  146. ::PathRemoveExtensionW(exePath);
  147. wstring programName = ::PathFindFileNameW(exePath);
  148. if (!modifyPath(programName))
  149. return 1;
  150. // Load our corresponding DLL.
  151. wstring dllName = programName + L".dll";
  152. if (!::PathRemoveFileSpecW(exePath))
  153. return fatalError(programName, L"::PathRemoveFileSpecW failed.");
  154. if (!::PathAppendW(exePath, dllName.c_str()))
  155. return fatalError(programName, L"::PathAppendW failed.");
  156. HMODULE module = ::LoadLibraryW(exePath);
  157. if (!module)
  158. return fatalError(programName, L"::LoadLibraryW failed.");
  159. #if USE_CONSOLE_ENTRY_POINT
  160. typedef int (WINAPI*EntryPoint)(int, const char*[]);
  161. #if defined _M_AMD64 || defined _WIN64
  162. const char* entryPointName = "_dllLauncherEntryPoint";
  163. #else
  164. const char* entryPointName = "_dllLauncherEntryPoint@8";
  165. #endif
  166. #else
  167. typedef int (WINAPI*EntryPoint)(HINSTANCE, HINSTANCE, LPWSTR, int);
  168. #if defined _M_AMD64 || defined _WIN64
  169. const char* entryPointName = "_dllLauncherEntryPoint";
  170. #else
  171. const char* entryPointName = "_dllLauncherEntryPoint@16";
  172. #endif
  173. #endif
  174. EntryPoint entryPoint = reinterpret_cast<EntryPoint>(::GetProcAddress(module, entryPointName));
  175. if (!entryPoint)
  176. return fatalError(programName, L"Failed to find dllLauncherEntryPoint function.");
  177. #if USE_CONSOLE_ENTRY_POINT
  178. return entryPoint(argc, argv);
  179. #else
  180. return entryPoint(hInstance, hPrevInstance, lpstrCmdLine, nCmdShow);
  181. #endif
  182. }