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.

377 lines
9.4 KiB

  1. /****************************************************************************
  2. WINEX.CPP
  3. Owner: cslim
  4. Copyright (c) 1997-1999 Microsoft Corporation
  5. Windows API extension functions
  6. History:
  7. 19-JUL-1999 cslim Created
  8. *****************************************************************************/
  9. #include "precomp.h"
  10. #include "winex.h"
  11. static LPSTR GetModuleName(BOOL fFullPath = fFalse);
  12. BOOL vf16BitApps = fFalse;
  13. POSVERSIONINFO GetVersionInfo()
  14. {
  15. static BOOL fFirstCallVerInfo = fTrue;
  16. static OSVERSIONINFO os;
  17. if (fFirstCallVerInfo)
  18. {
  19. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  20. if ( GetVersionEx( &os ) )
  21. fFirstCallVerInfo = fFalse;
  22. }
  23. return &os;
  24. }
  25. BOOL WINAPI IsWinNT()
  26. {
  27. static BOOL fFirstCallNT4 = fTrue;
  28. static BOOL fNT4 = fFalse;
  29. if (fFirstCallNT4 == fFalse)
  30. return fNT4;
  31. fFirstCallNT4 = fFalse;
  32. fNT4 = (GetVersionInfo()->dwPlatformId == VER_PLATFORM_WIN32_NT) &&
  33. (GetVersionInfo()->dwMajorVersion >= 4);
  34. return fNT4;
  35. }
  36. BOOL WINAPI IsWinNT5orUpper()
  37. {
  38. static BOOL fFirstCallNT5 = fTrue;
  39. static BOOL fNT5 = fFalse;
  40. if (fFirstCallNT5 == fFalse)
  41. return fNT5;
  42. fFirstCallNT5 = fFalse;
  43. fNT5 = (GetVersionInfo()->dwPlatformId == VER_PLATFORM_WIN32_NT) &&
  44. (GetVersionInfo()->dwMajorVersion >= 5);
  45. return fNT5;
  46. }
  47. BOOL WINAPI IsMemphis()
  48. {
  49. static BOOL fFirstCallMemphis = fTrue;
  50. static BOOL fMemphis = fFalse;
  51. if (fFirstCallMemphis == fFalse)
  52. return fMemphis;
  53. fFirstCallMemphis = fFalse;
  54. fMemphis = (GetVersionInfo()->dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) &&
  55. (GetVersionInfo()->dwMajorVersion >= 4) &&
  56. (GetVersionInfo()->dwMinorVersion >= 10);
  57. return fMemphis;
  58. }
  59. BOOL WINAPI IsWin95()
  60. {
  61. static BOOL fFirstCallIsWin95 = fTrue;
  62. static BOOL fIsWin95 = fFalse;
  63. if (fFirstCallIsWin95 == fFalse)
  64. return fIsWin95;
  65. fFirstCallIsWin95 = fFalse;
  66. fIsWin95 = !IsWinNT() && !IsMemphis();
  67. return fIsWin95;
  68. }
  69. #if !defined(_WIN64)
  70. BOOL WINAPI IsWin64()
  71. {
  72. static BOOL fFristCallIsWin64 = fTrue;
  73. static BOOL fIsWin64 = fFalse;
  74. SYSTEM_INFO sys;
  75. if (fFristCallIsWin64 == fFalse)
  76. return fIsWin64;
  77. GetNativeSystemInfo(&sys);
  78. fIsWin64 = (sys.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
  79. ||(sys.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64);
  80. fFristCallIsWin64 = fFalse;
  81. return fIsWin64;
  82. }
  83. #endif
  84. LPSTR OurGetModuleFileName(BOOL fFullPath)
  85. {
  86. LPSTR pch;
  87. LPSTR pchFilename;
  88. static CHAR szFile[MAX_PATH] = {0};
  89. if (szFile[0] == '\0')
  90. GetModuleFileName(NULL, szFile, MAX_PATH);
  91. if (fFullPath)
  92. return szFile;
  93. pch = szFile;
  94. pchFilename = NULL;
  95. while (*pch++)
  96. {
  97. if (*pch == '\\')
  98. pchFilename = pch + 1;
  99. }
  100. return pchFilename;
  101. }
  102. inline Min(INT a, INT b)
  103. {
  104. return ((a)<(b)?(a):(b)) ;
  105. }
  106. //////////////////////////////////////////////////////////////////
  107. // Function : OurLoadStringW
  108. // Type : INT
  109. // Purpose : Wrapper of LoadStrinW() API.
  110. // Load Unicode string with specified Language
  111. // in any platform.
  112. // Args :
  113. // : LANGID lgid
  114. // : HINSTANCE hInst
  115. // : UINT uID
  116. // : LPWSTR lpBuffer
  117. // : INT nBufferMax
  118. // Return :
  119. // DATE : 971028
  120. //////////////////////////////////////////////////////////////////
  121. INT WINAPI OurLoadStringW(HINSTANCE hInst, UINT uID, LPWSTR lpBuffer, INT nBufferMax)
  122. {
  123. INT cchwstr = 0;
  124. UINT block, num;
  125. HRSRC hres;
  126. HGLOBAL hgbl;
  127. LPWSTR lpwstr;
  128. if (!hInst || !lpBuffer)
  129. return 0;
  130. block = (uID >>4)+1;
  131. num = uID & 0xf;
  132. hres = FindResourceEx(hInst,
  133. RT_STRING,
  134. MAKEINTRESOURCE(block),
  135. GetSystemDefaultLangID());
  136. if (hres == NULL)
  137. hres = FindResourceEx(hInst,
  138. RT_STRING,
  139. MAKEINTRESOURCE(block),
  140. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
  141. if (!hres)
  142. goto Error;
  143. if ((hgbl = LoadResource(hInst, hres)) == NULL)
  144. goto Error;
  145. if ((lpwstr = (LPWSTR)LockResource(hgbl)) == NULL)
  146. goto Error;
  147. for(UINT i = 0; i < num; i++)
  148. lpwstr += *lpwstr + 1;
  149. cchwstr = *lpwstr;
  150. CopyMemory(lpBuffer, lpwstr+1, Min(cchwstr, nBufferMax-1) * sizeof(WCHAR));
  151. Error:
  152. lpBuffer[Min(cchwstr, nBufferMax-1)]= (WCHAR)0x0000;
  153. return cchwstr;
  154. }
  155. //////////////////////////////////////////////////////////////////
  156. // Function : ExLoadStringA
  157. // Type : INT
  158. // Purpose : Wrapper of LoadStringA().
  159. // Args :
  160. // : LANGID lgid
  161. // : HINSTANCE hInst
  162. // : INT uID
  163. // : LPSTR lpBuffer
  164. // : INT nBufferMax
  165. // Return :
  166. // DATE :
  167. //////////////////////////////////////////////////////////////////
  168. INT WINAPI OurLoadStringA(HINSTANCE hInst, INT uID, LPSTR lpBuffer, INT nBufferMax)
  169. {
  170. INT cchstr;
  171. LPWSTR lpwstr;
  172. if (!hInst || !lpBuffer)
  173. return 0;
  174. if ((lpwstr = (LPWSTR)GlobalAllocPtr(GHND, nBufferMax*sizeof(WCHAR))) == NULL)
  175. return 0;
  176. // Call wide version
  177. OurLoadStringW(hInst, uID, lpwstr, nBufferMax/2);
  178. // W to A
  179. cchstr = WideCharToMultiByte(CP_ACP,
  180. 0,
  181. lpwstr, -1,
  182. lpBuffer, nBufferMax,
  183. NULL, NULL);
  184. if (cchstr)
  185. cchstr--; // remove NULL char
  186. GlobalFreePtr(lpwstr);
  187. return cchstr;
  188. }
  189. //////////////////////////////////////////////////////////////////
  190. // Function : ExLoadMenuTemplate
  191. // Type : MENUTEMPLATE *
  192. // Purpose :
  193. // Args :
  194. // : LANGID lgid
  195. // : HINSTANCE hInstance
  196. // : LPCSTR pchTemplate
  197. // Return :
  198. // DATE :
  199. //////////////////////////////////////////////////////////////////
  200. static MENUTEMPLATE* ExLoadMenuTemplate(LANGID lgid,
  201. HINSTANCE hInstance,
  202. LPCSTR pchTemplate)
  203. {
  204. HRSRC hResMenu;
  205. HANDLE hMenuTmpl;
  206. hResMenu = FindResourceEx(hInstance, RT_MENU, pchTemplate, lgid);
  207. if((hResMenu == NULL) && (lgid != MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL)))
  208. {
  209. hResMenu = FindResourceEx(hInstance,
  210. RT_MENU,
  211. pchTemplate,
  212. MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
  213. }
  214. if (hResMenu == NULL)
  215. return NULL;
  216. hMenuTmpl = LoadResource( hInstance, hResMenu );
  217. if(hMenuTmpl == NULL)
  218. return NULL; /* failed */
  219. return (MENUTEMPLATE *)LockResource( hMenuTmpl );
  220. }
  221. HMENU WINAPI OurLoadMenu(HINSTANCE hInstance, LPCSTR lpMenuName)
  222. {
  223. MENUTEMPLATE* pMenuTmpl;
  224. pMenuTmpl = ExLoadMenuTemplate(GetSystemDefaultLangID(), hInstance, lpMenuName);
  225. if (pMenuTmpl)
  226. return LoadMenuIndirect(pMenuTmpl);
  227. else
  228. return (HMENU)NULL;
  229. }
  230. DLGTEMPLATE* WINAPI ExLoadDialogTemplate(LANGID lgid, HINSTANCE hInstance, LPCSTR pchTemplate)
  231. {
  232. HRSRC hResDlg;
  233. HANDLE hDlgTmpl;
  234. hResDlg = FindResourceExA(hInstance, RT_DIALOG, pchTemplate, lgid);
  235. if ((hResDlg == NULL) && (lgid != MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL)))
  236. hResDlg = FindResourceExA(hInstance,
  237. RT_DIALOG,
  238. pchTemplate,
  239. MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
  240. if (hResDlg == NULL)
  241. return NULL;
  242. hDlgTmpl = LoadResource(hInstance, hResDlg);
  243. if(hDlgTmpl == NULL)
  244. return NULL; /* failed */
  245. return (DLGTEMPLATE *)LockResource(hDlgTmpl);
  246. }
  247. BOOL WINAPI OurGetMessage(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax)
  248. {
  249. if (IsWinNT())
  250. return ::GetMessageW(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax);
  251. else
  252. return ::GetMessageA(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax);
  253. }
  254. LPSTR GetModuleName(BOOL fFullPath)
  255. {
  256. static CHAR szFile[MAX_PATH] = {0};
  257. if (szFile[0] == '\0' )
  258. GetModuleFileName( NULL, szFile, MAX_PATH );
  259. if (fFullPath)
  260. return szFile;
  261. LPSTR pch = szFile;
  262. LPSTR pchFilename = NULL;
  263. while(*pch++)
  264. {
  265. if (*pch == '\\')
  266. pchFilename = pch + 1;
  267. }
  268. return pchFilename;
  269. }
  270. BOOL IsExplorerProcess()
  271. {
  272. static BOOL fExplorer = FALSE;
  273. static BOOL fFirst = TRUE;
  274. if (fFirst == fFalse)
  275. return fExplorer;
  276. fFirst = fFalse;
  277. LPSTR pchFilename = GetModuleName();
  278. if (pchFilename && lstrcmpi(pchFilename, "EXPLORER.EXE") == 0)
  279. { // if this process is MSIME9xM.EXE
  280. fExplorer = fTrue;
  281. }
  282. return fExplorer;
  283. }
  284. BOOL IsExplorer()
  285. {
  286. if (FindWindow("Progman", NULL) != (HWND)0)
  287. return fTrue;
  288. else
  289. return fFalse;
  290. }