Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
6.0 KiB

  1. /*****************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 2000
  4. *
  5. * TITLE: fusutils.cpp
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: LazarI
  10. *
  11. * DATE: 14-Feb-2001
  12. *
  13. * DESCRIPTION: Fusion utilities
  14. *
  15. *****************************************************************************/
  16. #include "precomp.h"
  17. #pragma hdrstop
  18. #include "fusutils.h"
  19. #include "coredefs.h"
  20. // open C code brace
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. //
  25. // Win32Error2HRESULT: converts Win32 error to HRESULT
  26. //
  27. inline HRESULT Win32Error2HRESULT(DWORD dwError = GetLastError())
  28. {
  29. return (ERROR_SUCCESS == dwError) ? E_FAIL : HRESULT_FROM_WIN32(dwError);
  30. }
  31. //
  32. // SearchExecutableWrap: HRESULT wrapper around SearchPath
  33. //
  34. // searches for an executable and returns its full path in lpBuffer.
  35. // returns E_INVALIDARG if the executable cannot be found and
  36. // Win32Error2HRESULT(ERROR_INSUFFICIENT_BUFFER) if the
  37. // passed in buffer is too small to hold the full path.
  38. //
  39. inline HRESULT SearchExecutableWrap(LPCTSTR lpszExecutableName, UINT nBufferLength, LPTSTR lpBuffer, LPTSTR *lppFilePart)
  40. {
  41. DWORD cch = SearchPath(NULL, lpszExecutableName, NULL, nBufferLength, lpBuffer, lppFilePart);
  42. return (0 == cch) ? Win32Error2HRESULT() :
  43. (cch >= nBufferLength) ? Win32Error2HRESULT(ERROR_INSUFFICIENT_BUFFER) : S_OK;
  44. }
  45. //
  46. // GetExecutableNameWrap: HRESULT wrapper around GetModuleFileName
  47. //
  48. //
  49. // returns the full path of the current process executable (EXE)
  50. // or Win32Error2HRESULT(ERROR_INSUFFICIENT_BUFFER) if the
  51. // passed in buffer is too small to hold the full path.
  52. //
  53. inline HRESULT GetExecutableNameWrap(HMODULE hModule, UINT nBufferLength, LPTSTR lpBuffer)
  54. {
  55. DWORD cch = GetModuleFileName(hModule, lpBuffer, nBufferLength);
  56. return (0 == cch) ? Win32Error2HRESULT() :
  57. (cch >= nBufferLength) ? Win32Error2HRESULT(ERROR_INSUFFICIENT_BUFFER) : S_OK;
  58. }
  59. //
  60. // FileExists: checks if the passed in file name exists.
  61. //
  62. inline HRESULT FileExists(LPCTSTR pszFileName, BOOL *pbExists)
  63. {
  64. HRESULT hr = E_INVALIDARG;
  65. if (pszFileName && pbExists)
  66. {
  67. hr = S_OK;
  68. *pbExists = FALSE;
  69. WIN32_FIND_DATA findFileData;
  70. HANDLE hFind = FindFirstFile(pszFileName, &findFileData);
  71. if (hFind != INVALID_HANDLE_VALUE)
  72. {
  73. *pbExists = TRUE;
  74. FindClose(hFind);
  75. }
  76. }
  77. return hr;
  78. }
  79. static TCHAR g_szManifestExt[] = TEXT(".manifest");
  80. //
  81. // CreateActivationContextFromExecutableEx:
  82. //
  83. // check the passed in executable name for a manifest (if any)
  84. // and creates an activation context from it.
  85. //
  86. HRESULT CreateActivationContextFromExecutableEx(LPCTSTR lpszExecutableName, UINT uResourceID, BOOL bMakeProcessDefault, HANDLE *phActCtx)
  87. {
  88. HRESULT hr = E_INVALIDARG;
  89. if (phActCtx)
  90. {
  91. TCHAR szModule[MAX_PATH];
  92. TCHAR szManifest[MAX_PATH];
  93. BOOL bManifestFileFound = FALSE;
  94. // let's try to figure out whether this executable has a manifest file or not
  95. if (lpszExecutableName)
  96. {
  97. // search the passed in name in the path
  98. hr = SearchExecutableWrap(lpszExecutableName, ARRAYSIZE(szModule), szModule, NULL);
  99. }
  100. else
  101. {
  102. // if lpszExecutableName is NULL we assume the current module name
  103. hr = GetExecutableNameWrap(GetModuleHandle(NULL), ARRAYSIZE(szModule), szModule);
  104. }
  105. if (SUCCEEDED(hr))
  106. {
  107. if ((lstrlen(szModule) + lstrlen(g_szManifestExt)) < ARRAYSIZE(szManifest))
  108. {
  109. // create the manifest file name by appending ".manifest" to the executable name
  110. lstrcpy(szManifest, szModule);
  111. lstrcat(szManifest, g_szManifestExt);
  112. }
  113. else
  114. {
  115. // buffer is too small to hold the manifest file name
  116. hr = Win32Error2HRESULT(ERROR_BUFFER_OVERFLOW);
  117. }
  118. if (SUCCEEDED(hr))
  119. {
  120. BOOL bFileExists = FALSE;
  121. hr = FileExists(szManifest, &bFileExists);
  122. if (SUCCEEDED(hr) && bFileExists)
  123. {
  124. // an external manifest file found!
  125. bManifestFileFound = TRUE;
  126. }
  127. }
  128. }
  129. // now let's try to create an activation context
  130. ACTCTX act;
  131. ::ZeroMemory(&act, sizeof(act));
  132. act.cbSize = sizeof(act);
  133. if (bManifestFileFound)
  134. {
  135. // the executable has an external manifest file
  136. act.lpSource = szManifest;
  137. }
  138. else
  139. {
  140. // if the executable doesn't have an external manifest file,
  141. // so we assume that the it may have a manifest in its resources.
  142. act.dwFlags |= ACTCTX_FLAG_RESOURCE_NAME_VALID;
  143. act.lpResourceName = MAKEINTRESOURCE(uResourceID);
  144. act.lpSource = szModule;
  145. }
  146. if (bMakeProcessDefault)
  147. {
  148. // the caller has requested to set this activation context as
  149. // sefault for the current process. watch out!
  150. act.dwFlags |= ACTCTX_FLAG_SET_PROCESS_DEFAULT;
  151. }
  152. // now let's ask kernel32 to create an activation context
  153. HANDLE hActCtx = CreateActCtx(&act);
  154. if (INVALID_HANDLE_VALUE == hActCtx)
  155. {
  156. // something failed. create proper HRESULT to return.
  157. hr = Win32Error2HRESULT();
  158. }
  159. else
  160. {
  161. // wow, success!
  162. *phActCtx = hActCtx;
  163. hr = S_OK;
  164. }
  165. }
  166. return hr;
  167. }
  168. //
  169. // CreateActivationContextFromExecutable:
  170. //
  171. // check the passed in executable name for a manifest (if any)
  172. // and creates an activation context from it using the defaults
  173. // (i.e. bMakeProcessDefault=FALSE & uResourceID=123)
  174. //
  175. HRESULT CreateActivationContextFromExecutable(LPCTSTR lpszExecutableName, HANDLE *phActCtx)
  176. {
  177. return CreateActivationContextFromExecutableEx(lpszExecutableName, 123, FALSE, phActCtx);
  178. }
  179. // close C code brace
  180. #ifdef __cplusplus
  181. }
  182. #endif