Leaked source code of windows server 2003
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.

286 lines
8.1 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright � Microsoft Corporation. All rights reserved.
  4. //
  5. // MultiPlat.CPP
  6. //
  7. // Purpose: Support routines for multiplatform support
  8. //
  9. //***************************************************************************
  10. #include "precomp.h"
  11. #include "multiplat.h"
  12. #include "ImpersonateRevert.h"
  13. #include <cnvmacros.h>
  14. // smart pointers
  15. #include <autoptr.h>
  16. HMODULE FRGetModuleHandle(LPCWSTR wszModule)
  17. {
  18. if (CWbemProviderGlue::GetPlatform() == VER_PLATFORM_WIN32_NT)
  19. {
  20. return GetModuleHandleW(wszModule);
  21. }
  22. else
  23. {
  24. bool t_ConversionFailure = false ;
  25. char *szModule = NULL ;
  26. WCSTOANSISTRING(wszModule, szModule , t_ConversionFailure );
  27. if ( ! t_ConversionFailure )
  28. {
  29. if (szModule != NULL)
  30. {
  31. return GetModuleHandleA(szModule);
  32. }
  33. else
  34. {
  35. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  36. }
  37. }
  38. else
  39. {
  40. SetLastError(ERROR_NO_UNICODE_TRANSLATION);
  41. return 0;
  42. }
  43. }
  44. return 0; // To get rid of 64-bit compilation warning
  45. }
  46. DWORD FRGetModuleFileName(HMODULE hModule, LPWSTR lpwcsFileName, DWORD dwSize)
  47. {
  48. if (CWbemProviderGlue::GetPlatform() == VER_PLATFORM_WIN32_NT)
  49. {
  50. return GetModuleFileNameW(hModule, lpwcsFileName, dwSize);
  51. }
  52. else
  53. {
  54. char lpFileName[_MAX_PATH];
  55. DWORD dwRet = GetModuleFileNameA(hModule, lpFileName, dwSize);
  56. // If the call worked, convert the output string
  57. if (dwRet != 0)
  58. {
  59. bool t_ConversionFailure = false ;
  60. WCHAR *pName = NULL;
  61. ANSISTRINGTOWCS(lpFileName, pName, t_ConversionFailure );
  62. if ( ! t_ConversionFailure )
  63. {
  64. if ( pName )
  65. {
  66. StringCchCopyW( lpwcsFileName, dwSize, pName );
  67. }
  68. else
  69. {
  70. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  71. }
  72. }
  73. else
  74. {
  75. SetLastError(ERROR_NO_UNICODE_TRANSLATION);
  76. return 0;
  77. }
  78. }
  79. return dwRet;
  80. }
  81. }
  82. HINSTANCE FRLoadLibrary(LPCWSTR lpwcsLibFileName)
  83. {
  84. if (CWbemProviderGlue::GetPlatform() == VER_PLATFORM_WIN32_NT)
  85. {
  86. return LoadLibraryW(lpwcsLibFileName);
  87. }
  88. else
  89. {
  90. bool t_ConversionFailure = false ;
  91. char *lpLibFileName = NULL ;
  92. WCSTOANSISTRING(lpwcsLibFileName, lpLibFileName, t_ConversionFailure );
  93. if ( ! t_ConversionFailure )
  94. {
  95. if (lpLibFileName != NULL)
  96. {
  97. return LoadLibraryA(lpLibFileName);
  98. }
  99. else
  100. {
  101. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  102. }
  103. }
  104. else
  105. {
  106. SetLastError(ERROR_NO_UNICODE_TRANSLATION);
  107. return 0;
  108. }
  109. }
  110. return 0; // To get rid of compilation warning
  111. }
  112. BOOL FRGetComputerName(LPWSTR lpwcsBuffer, LPDWORD nSize)
  113. {
  114. if (CWbemProviderGlue::GetPlatform() == VER_PLATFORM_WIN32_NT)
  115. {
  116. BOOL bResult = FALSE;
  117. if ( ( bResult = GetComputerNameW(lpwcsBuffer, nSize) ) == FALSE )
  118. {
  119. DWORD dwError = ::GetLastError ();
  120. if ( ERROR_ACCESS_DENIED == dwError )
  121. {
  122. // The GetComputer will need to be called in the process's context.
  123. ProviderImpersonationRevert ir;
  124. if ( ir.Reverted () )
  125. {
  126. bResult = GetComputerNameW(lpwcsBuffer, nSize);
  127. }
  128. else
  129. {
  130. // I was not impersonated or revert failed
  131. // that means call GetComputerName failed with process credentials already
  132. // or will fail as I'm not reverted
  133. ::SetLastError ( dwError );
  134. }
  135. }
  136. }
  137. return bResult;
  138. }
  139. else
  140. {
  141. char lpBuffer[_MAX_PATH];
  142. DWORD dwSize = *nSize;
  143. BOOL bRet = GetComputerNameA(lpBuffer, nSize);
  144. // If the call worked
  145. if (bRet)
  146. {
  147. bool t_ConversionFailure = false ;
  148. WCHAR *pName = NULL ;
  149. ANSISTRINGTOWCS(lpBuffer, pName , t_ConversionFailure );
  150. if ( ! t_ConversionFailure )
  151. {
  152. if ( pName )
  153. {
  154. StringCchCopyW( lpwcsBuffer, dwSize, pName );
  155. }
  156. else
  157. {
  158. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  159. }
  160. }
  161. else
  162. {
  163. SetLastError(ERROR_NO_UNICODE_TRANSLATION);
  164. return FALSE ;
  165. }
  166. }
  167. return bRet;
  168. }
  169. }
  170. HANDLE FRCreateMutex(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitOwner, LPCWSTR lpwstrName)
  171. {
  172. if (CWbemProviderGlue::GetPlatform() == VER_PLATFORM_WIN32_NT)
  173. {
  174. return CreateMutexW(lpMutexAttributes, bInitOwner, lpwstrName);
  175. }
  176. else
  177. {
  178. bool t_ConversionFailure = false ;
  179. char *lpName = NULL ;
  180. WCSTOANSISTRING(lpwstrName, lpName, t_ConversionFailure );
  181. if ( ! t_ConversionFailure )
  182. {
  183. if (lpName != NULL)
  184. {
  185. return CreateMutexA(lpMutexAttributes, bInitOwner, lpName);
  186. }
  187. else
  188. {
  189. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  190. }
  191. }
  192. else
  193. {
  194. SetLastError(ERROR_NO_UNICODE_TRANSLATION);
  195. return 0;
  196. }
  197. }
  198. return NULL; // To get rid of compilation warning
  199. }
  200. DWORD FRExpandEnvironmentStrings(LPCWSTR wszSource, WCHAR *wszDest, DWORD dwSize)
  201. {
  202. if (CWbemProviderGlue::GetPlatform() == VER_PLATFORM_WIN32_NT)
  203. {
  204. return ExpandEnvironmentStringsW(wszSource, wszDest, dwSize);
  205. }
  206. else
  207. {
  208. bool t_ConversionFailure = false ;
  209. char *szSource = NULL ;
  210. WCSTOANSISTRING(wszSource, szSource, t_ConversionFailure );
  211. if ( ! t_ConversionFailure )
  212. {
  213. if (szSource != NULL)
  214. {
  215. char *szDest = new char[dwSize];
  216. if (szDest != NULL)
  217. {
  218. wmilib::auto_buffer < char > smartszDest ( szDest ) ;
  219. DWORD dwRet;
  220. dwRet = ExpandEnvironmentStringsA(szSource, szDest, dwSize);
  221. if ((dwRet <= dwSize) && (dwRet != 0))
  222. {
  223. bool t_ConversionFailure = false ;
  224. WCHAR *pName = NULL;
  225. ANSISTRINGTOWCS(szDest, pName, t_ConversionFailure );
  226. if ( ! t_ConversionFailure )
  227. {
  228. if ( pName )
  229. {
  230. StringCchCopyW( wszDest, dwSize, pName );
  231. }
  232. else
  233. {
  234. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  235. }
  236. }
  237. else
  238. {
  239. SetLastError(ERROR_NO_UNICODE_TRANSLATION);
  240. return 0;
  241. }
  242. }
  243. return dwRet;
  244. }
  245. else
  246. {
  247. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  248. }
  249. }
  250. else
  251. {
  252. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  253. }
  254. }
  255. else
  256. {
  257. SetLastError(ERROR_NO_UNICODE_TRANSLATION);
  258. return 0;
  259. }
  260. }
  261. return NULL; // To get rid of compilation warning
  262. }