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.

524 lines
16 KiB

  1. /******************************************************************************
  2. winx.cpp
  3. Windows utility procedures
  4. Copyright (C) Microsoft Corporation, 1997 - 1998
  5. All rights reserved
  6. Notes:
  7. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  8. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  9. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  10. PURPOSE.
  11. ******************************************************************************/
  12. #include "stillvue.h"
  13. #include <math.h> // pow
  14. // WINERROR.H - GetLastError errors
  15. STRINGTABLE StWinerror[] =
  16. {
  17. ERROR_SUCCESS, "ERROR_SUCCESS",0,
  18. ERROR_FILE_NOT_FOUND, "ERROR_FILE_NOT_FOUND",0,
  19. ERROR_PATH_NOT_FOUND, "ERROR_PATH_NOT_FOUND",0,
  20. ERROR_INVALID_FUNCTION, "ERROR_INVALID_FUNCTION",0,
  21. ERROR_ACCESS_DENIED, "ERROR_ACCESS_DENIED",0,
  22. ERROR_INVALID_HANDLE, "ERROR_INVALID_HANDLE",0,
  23. ERROR_INVALID_PARAMETER, "ERROR_INVALID_PARAMETER",0,
  24. ERROR_CALL_NOT_IMPLEMENTED, "ERROR_CALL_NOT_IMPLEMENTED",0,
  25. ERROR_ALREADY_EXISTS, "ERROR_ALREADY_EXISTS",0,
  26. ERROR_INVALID_FLAGS, "ERROR_INVALID_FLAGS",0,
  27. ERROR_INVALID_CATEGORY, "ERROR_INVALID_CATEGORY",0,
  28. RPC_S_SERVER_UNAVAILABLE, "RPC_S_SERVER_UNAVAILABLE",0,
  29. 0, "See WINERROR.H",-1
  30. };
  31. /******************************************************************************
  32. ULONG atox(LPSTR lpHex)
  33. Convert string to hexadecimal value.
  34. ******************************************************************************/
  35. ULONG atox(LPSTR lpHex)
  36. {
  37. char *p;
  38. int x;
  39. double y;
  40. ULONG z,ulHex = 0l;
  41. for (p = lpHex,x = 0;p[x];x++)
  42. ;
  43. for (x--,y = 0.0;lpHex <= (p + x);x--,y++)
  44. {
  45. z = (ULONG) pow(16.0,y);
  46. if ((p[x] >= '0')&&(p[x] <= '9'))
  47. ulHex += ((p[x] - '0') * z);
  48. if ((p[x] >= 'A')&&(p[x] <= 'F'))
  49. ulHex += ((p[x] - 'A' + 10) * z);
  50. if ((p[x] >= 'a')&&(p[x] <= 'f'))
  51. ulHex += ((p[x] - 'a' + 10) * z);
  52. }
  53. return (ulHex);
  54. }
  55. #ifdef _DEBUG
  56. /******************************************************************************
  57. void DisplayDebug(LPSTR sz,...)
  58. Output text to debugger.
  59. ******************************************************************************/
  60. void DisplayDebug(LPSTR sz,...)
  61. {
  62. char Buffer[512];
  63. va_list list;
  64. va_start(list,sz);
  65. vsprintf(Buffer,sz,list);
  66. OutputDebugString(Buffer);
  67. OutputDebugString("\n");
  68. return;
  69. }
  70. #else
  71. /******************************************************************************
  72. void DisplayDebug(LPSTR sz,...)
  73. Output text to debugger - nonfunctional retail version..
  74. ******************************************************************************/
  75. void DisplayDebug(LPSTR sz,...)
  76. {
  77. return;
  78. }
  79. #endif
  80. /******************************************************************************
  81. BOOL ErrorMsg(HWND, LPSTR, LPSTR, BOOL)
  82. Display an error message and send WM_QUIT if error is fatal.
  83. Parameters: handle to current window,
  84. long pointer to string with error message,
  85. long pointer to string with message box caption,
  86. error (Fatal if TRUE)
  87. Shut down app if bFatal is TRUE, continue if FALSE.
  88. ******************************************************************************/
  89. BOOL ErrorMsg(HWND hWnd, LPSTR lpzMsg, LPSTR lpzCaption, BOOL bFatal)
  90. {
  91. MessageBox(hWnd, lpzMsg, lpzCaption, MB_ICONEXCLAMATION | MB_OK);
  92. if (bFatal)
  93. PostMessage (hWnd, WM_QUIT, 0, 0L);
  94. return (bFatal);
  95. }
  96. /******************************************************************************
  97. fDialog(id,hwnd,fpfn)
  98. Description:
  99. This function displays a dialog box and returns the exit code.
  100. the function passed will have a proc instance made for it.
  101. Parameters:
  102. id resource id of dialog to display
  103. hwnd parent window of dialog
  104. fpfn dialog message function
  105. Returns:
  106. exit code of dialog (what was passed to EndDialog)
  107. ******************************************************************************/
  108. BOOL fDialog(int id,HWND hWnd,FARPROC fPfn)
  109. {
  110. BOOL f;
  111. HINSTANCE hInst;
  112. hInst = (HINSTANCE) GetWindowLong(hWnd,GWL_HINSTANCE);
  113. //fPfn = MakeProcInstance(fPfn,hInst);
  114. //f = DialogBox(hInst,MAKEINTRESOURCE(id),hWnd,(DLGPROC)fPfn);
  115. //FreeProcInstance(fPfn);
  116. f = DialogBox(hInst, MAKEINTRESOURCE(id), hWnd, fPfn);
  117. return (f);
  118. }
  119. /******************************************************************************
  120. void FormatHex(unsigned char *szSource, char *szDest)
  121. take first 16 bytes from szSource,
  122. format into a hex dump string,
  123. then copy the string into szDest
  124. szDest must have room for at least 66 bytes
  125. sample code fragment showing use:
  126. char szOut[128], // output string
  127. // print header
  128. sprintf(szOut,
  129. "Offset --------------------- hex --------------------- ---- ascii -----");
  130. puts(szOut);
  131. // dump 512 bytes (32 lines, 16 bytes per line)
  132. for (i = 0; i < 32; i++)
  133. {
  134. // get next 16 bytes
  135. _fmemcpy(szDbgMsg,fpSector + (i*16),16);
  136. // get current offset into data block
  137. sprintf(szOut,"%03xh(%03d) ",i*16,i*16);
  138. // append debug string after data block offset message
  139. FormatHex(szDbgMsg, szOut + strlen(szOut));
  140. puts(szOut);
  141. }
  142. ******************************************************************************/
  143. void FormatHex(unsigned char *szSource, char *szDest)
  144. {
  145. unsigned short j;
  146. sprintf(szDest,
  147. "%02x %02x %02x %02x %02x %02x %02x %02x:"\
  148. "%02x %02x %02x %02x %02x %02x %02x %02x ",
  149. szSource[0],
  150. szSource[1],
  151. szSource[2],
  152. szSource[3],
  153. szSource[4],
  154. szSource[5],
  155. szSource[6],
  156. szSource[7],
  157. szSource[8],
  158. szSource[9],
  159. szSource[10],
  160. szSource[11],
  161. szSource[12],
  162. szSource[13],
  163. szSource[14],
  164. szSource[15]);
  165. // replace bytes with undesirable Sprintf side effects with SPACE
  166. for (j = 0; j < 16; j++)
  167. {
  168. if ((0x00 == szSource[j]) ||
  169. (0x07 == szSource[j]) ||
  170. (0x09 == szSource[j]) ||
  171. (0x0a == szSource[j]) ||
  172. (0x0d == szSource[j]) ||
  173. (0x1a == szSource[j]))
  174. szSource[j] = 0x20;
  175. }
  176. sprintf(szDest + strlen(szDest),
  177. "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",
  178. szSource[0],
  179. szSource[1],
  180. szSource[2],
  181. szSource[3],
  182. szSource[4],
  183. szSource[5],
  184. szSource[6],
  185. szSource[7],
  186. szSource[8],
  187. szSource[9],
  188. szSource[10],
  189. szSource[11],
  190. szSource[12],
  191. szSource[13],
  192. szSource[14],
  193. szSource[15]);
  194. return;
  195. }
  196. /******************************************************************************
  197. BOOL GetFinalWindow(HANDLE, LPRECT, LPSTR, LPSTR)
  198. Retrieve the last window size & location from a private INI
  199. Parameters: handle to current instance,
  200. long pointer to a rectangle with window size/shape,
  201. string with INI filename,
  202. string with Section name
  203. Returns: success/failure (never fails)
  204. Get display size in pixels and private INI saved width and height.
  205. Default width (for no previous INI) is 1/3 display width.
  206. Default height (for no previous INI) is 1/3 display height.
  207. If saved size or postion would put part or all the window
  208. off the desktop, first change the position then the size until
  209. the window is completely on the desktop.
  210. ******************************************************************************/
  211. BOOL GetFinalWindow (HANDLE hInst,
  212. LPRECT lprRect,
  213. LPSTR lpzINI,
  214. LPSTR lpzSection)
  215. {
  216. int x, nDisplayWidth, nDisplayHeight;
  217. RECT rect;
  218. nDisplayWidth = GetSystemMetrics(SM_CXSCREEN);
  219. nDisplayHeight = GetSystemMetrics(SM_CYSCREEN);
  220. rect.left = GetPrivateProfileInt(lpzSection, "Left",
  221. (nDisplayWidth/10) * 7,lpzINI);
  222. rect.top = GetPrivateProfileInt(lpzSection, "Top",
  223. (nDisplayHeight/10) * 8,lpzINI);
  224. rect.right = GetPrivateProfileInt(lpzSection, "Right",
  225. nDisplayWidth,lpzINI);
  226. rect.bottom = GetPrivateProfileInt(lpzSection, "Bottom",
  227. nDisplayHeight,lpzINI);
  228. /*/////////////////////////////////////////////////////////////////////////////
  229. if window hangs off top or left of display, change location to
  230. edge of display and preserve size.
  231. /////////////////////////////////////////////////////////////////////////////*/
  232. if (rect.top < 0)
  233. {
  234. rect.bottom += rect.top * -1;
  235. rect.top = 0;
  236. }
  237. if (rect.left < 0)
  238. {
  239. rect.right += rect.left * -1;
  240. rect.left = 0;
  241. }
  242. /*/////////////////////////////////////////////////////////////////////////////
  243. if window hangs off bottom or right of display, change location
  244. to bring it back onscreen. If window dimension is greater than
  245. display, reduce to size of display.
  246. /////////////////////////////////////////////////////////////////////////////*/
  247. if (rect.bottom > nDisplayHeight)
  248. {
  249. if (rect.bottom > nDisplayHeight * 2)
  250. {
  251. rect.top = 0;
  252. rect.bottom = nDisplayHeight;
  253. }
  254. else
  255. {
  256. x = rect.bottom - nDisplayHeight;
  257. rect.bottom -= x;
  258. rect.top -= x;
  259. }
  260. }
  261. if (rect.right > nDisplayWidth)
  262. {
  263. if (rect.right > nDisplayWidth * 2)
  264. {
  265. rect.left = 0;
  266. rect.right = nDisplayWidth;
  267. }
  268. else
  269. {
  270. x = rect.right - nDisplayWidth;
  271. rect.right -= x;
  272. rect.left -= x;
  273. }
  274. }
  275. /*/////////////////////////////////////////////////////////////////////////////
  276. GetWindowRect() returns a rect where right and bottom are absolute
  277. (measured from 0,0 of display). However, CreateWindow requires
  278. right and bottom to be relative (measured from 0,0 of the window
  279. to be created). SaveFinalWindow saves an absolute rect, and
  280. GetFinalRect converts these to relative measurements.
  281. /////////////////////////////////////////////////////////////////////////////*/
  282. SetRect(lprRect,
  283. rect.left,
  284. rect.top,
  285. rect.right - rect.left,
  286. rect.bottom - rect.top);
  287. return (TRUE);
  288. }
  289. /******************************************************************************
  290. BOOL LastError(BOOL)
  291. Calls GetLastError and displays result in a nice string
  292. Parameters: bNewOnly == TRUE if you only want changed error displayed
  293. Returns: TRUE if it found an error, else FALSE
  294. ******************************************************************************/
  295. BOOL LastError(BOOL bNewOnly)
  296. {
  297. static DWORD dwLast = 0;
  298. DWORD dwError;
  299. if (dwError = GetLastError())
  300. {
  301. // if user asked for only new errors
  302. if (bNewOnly)
  303. {
  304. // not a new error
  305. if (dwLast == dwError)
  306. return FALSE;
  307. // new error, save it
  308. dwLast = dwError;
  309. }
  310. DisplayOutput("*GetLastError %xh %d \"%s\"",
  311. dwError,dwError,StrFromTable(dwError,StWinerror));
  312. return (TRUE);
  313. }
  314. return (FALSE);
  315. }
  316. /******************************************************************************
  317. int NextToken(char *pDest,char *pSrc)
  318. Return next token from a command line string.
  319. ******************************************************************************/
  320. int NextToken(char *pDest,char *pSrc)
  321. {
  322. char *pA,*pB;
  323. int x;
  324. // point pArg to start of token in string pSrc
  325. for (pA = pSrc;*pA && isspace((int) *pA);pA++)
  326. ;
  327. // find end of token in string pSrc
  328. for (pB = pA;((*pB) && (! isspace((int) *pB)));pB++)
  329. ;
  330. // count of chars to next token or end of string
  331. x = (min((pB - pA),(int) strlen(pSrc))) + 1;
  332. // pszDest now contains the arg
  333. lstrcpyn(pDest,pA,x);
  334. // return sizeof token
  335. return (x);
  336. }
  337. /******************************************************************************
  338. BOOL SaveFinalWindow(HANDLE, HWND, LPSTR, LPSTR)
  339. Save the current window size & location to a private INI
  340. Parameters: handle to current instance,
  341. handle to current window,
  342. string with INI filename,
  343. string with Section name
  344. Returns: success/failure (fails if window is MIN or MAX)
  345. ******************************************************************************/
  346. BOOL SaveFinalWindow (HANDLE hInst,
  347. HWND hWnd,
  348. LPSTR lpzINI,
  349. LPSTR lpzSection)
  350. {
  351. PSTR pszValue;
  352. RECT rectWnd, rectINI;
  353. // if the window is minimized OR maximised, don't save anything
  354. if (IsIconic(hWnd) || IsZoomed(hWnd))
  355. return (FALSE);
  356. GetWindowRect (hWnd, &rectWnd);
  357. // get INI data. If there isn't any, we'll get the default and
  358. // save the current Window data.
  359. rectINI.left = GetPrivateProfileInt(lpzSection, "Left", 0, lpzINI);
  360. rectINI.top = GetPrivateProfileInt(lpzSection, "Top", 0, lpzINI);
  361. rectINI.right = GetPrivateProfileInt(lpzSection, "Right", 0, lpzINI);
  362. rectINI.bottom = GetPrivateProfileInt(lpzSection, "Bottom", 0, lpzINI);
  363. // if current window is same as in INI, don't change INI
  364. if ( rectINI.left == rectWnd.left &&
  365. rectINI.top == rectWnd.top &&
  366. rectINI.right == rectWnd.right &&
  367. rectINI.bottom == rectWnd.bottom)
  368. return (TRUE);
  369. // EXIT if we can't local alloc our string stuffer
  370. if ((pszValue = (PSTR) LocalAlloc(LPTR, 80)) == NULL)
  371. return (FALSE);
  372. // it's different, so save
  373. sprintf(pszValue, "%d", rectWnd.left);
  374. WritePrivateProfileString(lpzSection, "Left", pszValue, lpzINI);
  375. sprintf(pszValue, "%d", rectWnd.top);
  376. WritePrivateProfileString(lpzSection, "Top", pszValue, lpzINI);
  377. sprintf(pszValue, "%d", rectWnd.right);
  378. WritePrivateProfileString(lpzSection, "Right", pszValue, lpzINI);
  379. sprintf(pszValue, "%d", rectWnd.bottom);
  380. WritePrivateProfileString(lpzSection, "Bottom", pszValue, lpzINI);
  381. LocalFree((HANDLE) pszValue);
  382. return (TRUE);
  383. }
  384. /******************************************************************************
  385. char * StrFromTable(long number,PSTRINGTABLE pstrTable)
  386. Return string associated with a value from a string table.
  387. ******************************************************************************/
  388. char * StrFromTable(long number,PSTRINGTABLE pstrTable)
  389. {
  390. for (;pstrTable->end != -1;pstrTable++)
  391. {
  392. if (number == pstrTable->number)
  393. break;
  394. }
  395. return (pstrTable->szString);
  396. }
  397. /******************************************************************************
  398. BOOL Wait32(DWORD)
  399. wait DWORD milliseconds, then return
  400. ******************************************************************************/
  401. BOOL Wait32(DWORD dwTime)
  402. {
  403. DWORD dwNewTime,
  404. dwOldTime;
  405. // wait dwTime, then exit
  406. dwOldTime = GetCurrentTime();
  407. while (TRUE)
  408. {
  409. dwNewTime = GetCurrentTime();
  410. if (dwNewTime > dwOldTime + dwTime)
  411. break;
  412. }
  413. return (0);
  414. }