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.

224 lines
4.7 KiB

  1. #include "project.hpp"
  2. #include <stdio.h> // for _snwprintf
  3. // * note: debug check/code incomplete.
  4. // ----------------------------------------------------------------------------
  5. //
  6. // Return last Win32 error as an HRESULT.
  7. //
  8. HRESULT
  9. GetLastWin32Error()
  10. {
  11. // Win 95 can return 0, even when there's an error.
  12. DWORD dw = GetLastError();
  13. return dw ? HRESULT_FROM_WIN32(dw) : E_FAIL;
  14. }
  15. // ----------------------------------------------------------------------------
  16. bool
  17. PathAppend(LPWSTR wzDest, LPCWSTR wzSrc)
  18. {
  19. // shlwapi PathAppend-like
  20. bool bRetVal = TRUE;
  21. int iPathLen = 0;
  22. static WCHAR wzWithSeparator[] = L"\\%s";
  23. static WCHAR wzWithoutSeparator[] = L"%s";
  24. if (!wzDest || !wzSrc)
  25. {
  26. bRetVal = FALSE;
  27. goto exit;
  28. }
  29. iPathLen = wcslen(wzDest);
  30. if (_snwprintf(wzDest+iPathLen, MAX_PATH-iPathLen,
  31. (wzDest[iPathLen-1] == L'\\' ? wzWithoutSeparator : wzWithSeparator), wzSrc) < 0)
  32. {
  33. bRetVal = FALSE;
  34. }
  35. exit:
  36. return bRetVal;
  37. }
  38. // ----------------------------------------------------------------------------
  39. /*----------------------------------------------------------
  40. Purpose: Returns an integer value specifying the length of
  41. the substring in psz that consists entirely of
  42. characters in pszSet. If psz begins with a character
  43. not in pszSet, then this function returns 0.
  44. This is a DBCS-safe version of the CRT strspn().
  45. Returns: see above
  46. Cond: --
  47. */
  48. /*int StrSpnW(LPCWSTR psz, LPCWSTR pszSet)
  49. {
  50. LPCWSTR pszT;
  51. LPCWSTR pszSetT;
  52. ASSERT(psz);
  53. ASSERT(pszSet);
  54. // Go thru the string to be inspected
  55. for (pszT = psz; *pszT; pszT++)
  56. {
  57. // Go thru the char set
  58. for (pszSetT = pszSet; *pszSetT != *pszT; pszSetT++)
  59. {
  60. if (0 == *pszSetT)
  61. {
  62. // Reached end of char set without finding a match
  63. return (int)(pszT - psz);
  64. }
  65. }
  66. }
  67. return (int)(pszT - psz);
  68. }*/
  69. // find leading spaces
  70. BOOL AnyNonWhiteSpace(PCWSTR pcwz)
  71. {
  72. ASSERT(! pcwz );
  73. return(pcwz ? wcsspn(pcwz, g_cwzWhiteSpace) < wcslen(pcwz) : FALSE); // use (size_t) StrSpnW as above?
  74. }
  75. // ----------------------------------------------------------------------------
  76. BOOL IsValidPath(PCWSTR pcwzPath)
  77. {
  78. // FEATURE: Beef up path validation.
  79. return(EVAL((UINT)wcslen(pcwzPath) < MAX_PATH));
  80. }
  81. BOOL IsValidPathResult(HRESULT hr, PCWSTR pcwzPath,
  82. UINT ucbPathBufLen)
  83. {
  84. return((hr == S_OK &&
  85. EVAL(IsValidPath(pcwzPath)) &&
  86. EVAL((UINT)wcslen(pcwzPath) < ucbPathBufLen)) ||
  87. (hr != S_OK &&
  88. EVAL(! ucbPathBufLen ||
  89. ! pcwzPath ||
  90. ! *pcwzPath)));
  91. }
  92. BOOL IsValidIconIndex(HRESULT hr, PCWSTR pcwzIconFile,
  93. UINT ucbIconFileBufLen, int niIcon)
  94. {
  95. return(EVAL(IsValidPathResult(hr, pcwzIconFile, ucbIconFileBufLen)) &&
  96. EVAL(hr == S_OK ||
  97. ! niIcon));
  98. }
  99. // ----------------------------------------------------------------------------
  100. BOOL IsValidHWND(HWND hwnd)
  101. {
  102. // Ask User if this is a valid window.
  103. return(IsWindow(hwnd));
  104. }
  105. #ifdef DEBUG
  106. BOOL IsValidHANDLE(HANDLE hnd)
  107. {
  108. return(EVAL(hnd != INVALID_HANDLE_VALUE));
  109. }
  110. BOOL IsValidHEVENT(HANDLE hevent)
  111. {
  112. return(IsValidHANDLE(hevent));
  113. }
  114. BOOL IsValidHFILE(HANDLE hf)
  115. {
  116. return(IsValidHANDLE(hf));
  117. }
  118. BOOL IsValidHGLOBAL(HGLOBAL hg)
  119. {
  120. return(IsValidHANDLE(hg));
  121. }
  122. BOOL IsValidHMENU(HMENU hmenu)
  123. {
  124. return(IsValidHANDLE(hmenu));
  125. }
  126. BOOL IsValidHINSTANCE(HINSTANCE hinst)
  127. {
  128. return(IsValidHANDLE(hinst));
  129. }
  130. BOOL IsValidHICON(HICON hicon)
  131. {
  132. return(IsValidHANDLE(hicon));
  133. }
  134. BOOL IsValidHKEY(HKEY hkey)
  135. {
  136. return(IsValidHANDLE(hkey));
  137. }
  138. BOOL IsValidHMODULE(HMODULE hmod)
  139. {
  140. return(IsValidHANDLE(hmod));
  141. }
  142. BOOL IsValidHPROCESS(HANDLE hprocess)
  143. {
  144. return(IsValidHANDLE(hprocess));
  145. }
  146. BOOL IsValidHTEMPLATEFILE(HANDLE htf)
  147. {
  148. return(IsValidHANDLE(htf));
  149. }
  150. BOOL IsValidShowCmd(int nShow)
  151. {
  152. BOOL bResult;
  153. switch (nShow)
  154. {
  155. case SW_HIDE:
  156. case SW_MINIMIZE:
  157. case SW_MAXIMIZE:
  158. case SW_RESTORE:
  159. case SW_SHOW:
  160. case SW_SHOWNORMAL:
  161. case SW_SHOWDEFAULT:
  162. case SW_SHOWMINIMIZED:
  163. case SW_SHOWMAXIMIZED:
  164. case SW_SHOWNOACTIVATE:
  165. case SW_SHOWMINNOACTIVE:
  166. case SW_SHOWNA:
  167. case SW_FORCEMINIMIZE:
  168. bResult = TRUE;
  169. break;
  170. default:
  171. bResult = FALSE;
  172. ERROR_OUT(("IsValidShowCmd(): Invalid show command %d.",
  173. nShow));
  174. break;
  175. }
  176. return(bResult);
  177. }
  178. #endif