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.

1378 lines
30 KiB

  1. #include "std.hxx"
  2. void xGetOverlappedResult(
  3. HANDLE hFile,
  4. LPOVERLAPPED lpOverlapped,
  5. LPDWORD lpNumberOfBytesTransferred,
  6. BOOL bWait)
  7. {
  8. BOOL bResult = ::GetOverlappedResult(
  9. hFile,
  10. lpOverlapped,
  11. lpNumberOfBytesTransferred,
  12. bWait);
  13. if (!bResult)
  14. {
  15. throw CWin32ApiExcept(
  16. GetLastError(),
  17. _T("GetOverlappedResult"));
  18. }
  19. }
  20. HANDLE xCreateFile(
  21. PCTSTR pcszFileName, // file name
  22. DWORD dwDesiredAccess, // access mode
  23. DWORD dwShareMode, // share mode
  24. PSECURITY_ATTRIBUTES pSecurityAttributes, // SD
  25. DWORD dwCreationDisposition, // how to create
  26. DWORD dwFlagsAndAttributes, // file attributes
  27. HANDLE hTemplateFile) // handle to template file
  28. {
  29. HANDLE hTemp = ::CreateFile(
  30. pcszFileName,
  31. dwDesiredAccess,
  32. dwShareMode,
  33. pSecurityAttributes,
  34. dwCreationDisposition,
  35. dwFlagsAndAttributes,
  36. hTemplateFile);
  37. if (INVALID_HANDLE_VALUE==hTemp)
  38. {
  39. throw CWin32ApiExcept(
  40. GetLastError(),
  41. _T("CreateFile"));
  42. }
  43. return hTemp;
  44. }
  45. void xCloseHandle(
  46. HANDLE hObject) // handle to object
  47. {
  48. BOOL bResult = ::CloseHandle(
  49. hObject);
  50. if (!bResult)
  51. {
  52. throw CWin32ApiExcept(
  53. GetLastError(),
  54. _T("CloseHandle"));
  55. }
  56. }
  57. BOOL xReadFile(
  58. HANDLE hFile, // handle to file
  59. PVOID pBuffer, // data buffer
  60. DWORD dwNumberOfBytesToRead, // number of bytes to read
  61. PDWORD pdwNumberOfBytesRead, // number of bytes read
  62. LPOVERLAPPED pOverlapped) // overlapped buffer
  63. {
  64. BOOL bResult = ::ReadFile(
  65. hFile,
  66. pBuffer,
  67. dwNumberOfBytesToRead,
  68. pdwNumberOfBytesRead,
  69. pOverlapped);
  70. if (!bResult && (GetLastError()!=ERROR_IO_PENDING))
  71. {
  72. throw CWin32ApiExcept(
  73. GetLastError(),
  74. _T("ReadFile"));
  75. }
  76. return bResult;
  77. }
  78. void xReadFileWithAbort(
  79. HANDLE hFile, // handle to file
  80. PVOID pBuffer, // data buffer
  81. DWORD dwNumberOfBytesToRead, // number of bytes to read
  82. PDWORD pdwNumberOfBytesRead, // number of bytes read
  83. LPOVERLAPPED pOverlapped, // overlapped buffer
  84. HANDLE hAbort) // Handle to manual reset abort event
  85. {
  86. HANDLE hWait[2];
  87. hWait[0] = hAbort;
  88. hWait[1] = pOverlapped->hEvent;
  89. BOOL bResult = xReadFile(
  90. hFile, // handle to file
  91. pBuffer, // data buffer
  92. dwNumberOfBytesToRead, // number of bytes to read
  93. pdwNumberOfBytesRead, // number of bytes read
  94. pOverlapped); // overlapped buffer
  95. if (!bResult)
  96. {
  97. DWORD dwResult = xWaitForMultipleObjects(
  98. 2, // There are two to wait on
  99. hWait, // Our two event handles
  100. FALSE, // Only one event need to be pinged
  101. INFINITE); // Wait for ever
  102. switch (dwResult)
  103. {
  104. case (WAIT_OBJECT_0):
  105. CancelIo(
  106. hFile);
  107. throw CAbortExcept();
  108. break;
  109. case (WAIT_OBJECT_0 + 1):
  110. xGetOverlappedResult(
  111. hFile,
  112. pOverlapped,
  113. pdwNumberOfBytesRead,
  114. TRUE);
  115. break;
  116. }
  117. }
  118. }
  119. BOOL xWriteFile(
  120. HANDLE hFile, // handle to file
  121. LPCVOID pBuffer, // data buffer
  122. DWORD dwNumberOfBytesToWrite, // number of bytes to write
  123. PDWORD pdwNumberOfBytesWritten, // number of bytes written
  124. LPOVERLAPPED pOverlapped) // overlapped buffer
  125. {
  126. BOOL bResult = ::WriteFile(
  127. hFile,
  128. pBuffer,
  129. dwNumberOfBytesToWrite,
  130. pdwNumberOfBytesWritten,
  131. pOverlapped);
  132. if (!bResult && (GetLastError()!=ERROR_IO_PENDING))
  133. {
  134. throw CWin32ApiExcept(
  135. GetLastError(),
  136. _T("WriteFile"));
  137. }
  138. return bResult;
  139. }
  140. void xWriteFileWithAbort(
  141. HANDLE hFile, // handle to file
  142. LPCVOID pBuffer, // data buffer
  143. DWORD dwNumberOfBytesToWrite, // number of bytes to write
  144. PDWORD pdwNumberOfBytesWritten, // number of bytes written
  145. LPOVERLAPPED pOverlapped, // overlapped buffer
  146. HANDLE hAbort) // Handle to manual reset abort event
  147. {
  148. HANDLE hWait[2];
  149. hWait[0] = hAbort;
  150. hWait[1] = pOverlapped->hEvent;
  151. BOOL bResult = xWriteFile(
  152. hFile, // handle to file
  153. pBuffer, // data buffer
  154. dwNumberOfBytesToWrite, // number of bytes to read
  155. pdwNumberOfBytesWritten, // number of bytes read
  156. pOverlapped); // overlapped buffer
  157. if (!bResult)
  158. {
  159. DWORD dwResult = xWaitForMultipleObjects(
  160. 2, // There are two to wait on
  161. hWait, // Our two event handles
  162. FALSE, // Only one event need to be pinged
  163. INFINITE); // Wait for ever
  164. switch (dwResult)
  165. {
  166. case (WAIT_OBJECT_0):
  167. CancelIo(
  168. hFile);
  169. throw CAbortExcept();
  170. break;
  171. case (WAIT_OBJECT_0 + 1):
  172. xGetOverlappedResult(
  173. hFile,
  174. pOverlapped,
  175. pdwNumberOfBytesWritten,
  176. TRUE);
  177. break;
  178. }
  179. }
  180. }
  181. void xGetCommState(
  182. HANDLE hFile, // handle to communications device
  183. LPDCB pDCB) // device-control block
  184. {
  185. BOOL bResult = ::GetCommState(
  186. hFile,
  187. pDCB);
  188. if (!bResult)
  189. {
  190. throw CWin32ApiExcept(
  191. GetLastError(),
  192. _T("GetCommState"));
  193. }
  194. }
  195. void xSetCommState(
  196. HANDLE hFile, // handle to communications device
  197. LPDCB pDCB) // device-control block
  198. {
  199. BOOL bResult = ::SetCommState(
  200. hFile,
  201. pDCB);
  202. if (!bResult)
  203. {
  204. throw CWin32ApiExcept(
  205. GetLastError(),
  206. _T("SetCommState"));
  207. }
  208. }
  209. void xGetCommTimeouts(
  210. HANDLE hFile, // handle to comm device
  211. LPCOMMTIMEOUTS pCommTimeouts) // time-out values
  212. {
  213. BOOL bResult = ::GetCommTimeouts(
  214. hFile,
  215. pCommTimeouts);
  216. if (!bResult)
  217. {
  218. throw CWin32ApiExcept(
  219. GetLastError(),
  220. _T("GetCommTimeouts"));
  221. }
  222. }
  223. void xSetCommTimeouts(
  224. HANDLE hFile, // handle to comm device
  225. LPCOMMTIMEOUTS pCommTimeouts) // time-out values
  226. {
  227. BOOL bResult = ::SetCommTimeouts(
  228. hFile,
  229. pCommTimeouts);
  230. if (!bResult)
  231. {
  232. throw CWin32ApiExcept(
  233. GetLastError(),
  234. _T("SetCommTimeouts"));
  235. }
  236. }
  237. void xSetCommMask(
  238. HANDLE hFile, // handle to communications device
  239. DWORD dwEvtMask) // mask that identifies enabled events
  240. {
  241. BOOL bResult = ::SetCommMask(
  242. hFile,
  243. dwEvtMask);
  244. if (!bResult)
  245. {
  246. throw CWin32ApiExcept(
  247. GetLastError(),
  248. _T("SetCommMask"));
  249. }
  250. }
  251. void xGetCommMask(
  252. HANDLE hFile, // handle to communications device
  253. PDWORD pdwEvtMask) // mask that identifies enabled events
  254. {
  255. BOOL bResult = ::GetCommMask(
  256. hFile,
  257. pdwEvtMask);
  258. if (!bResult)
  259. {
  260. throw CWin32ApiExcept(
  261. GetLastError(),
  262. _T("GetCommMask"));
  263. }
  264. }
  265. BOOL xWaitCommEvent(
  266. HANDLE hFile, // handle to comm device
  267. PDWORD pdwEvtMask, // event type
  268. LPOVERLAPPED pOverlapped) // overlapped structure
  269. {
  270. BOOL bResult = ::WaitCommEvent(
  271. hFile,
  272. pdwEvtMask,
  273. pOverlapped);
  274. if (!bResult && (GetLastError()!=ERROR_IO_PENDING))
  275. {
  276. throw CWin32ApiExcept(
  277. GetLastError(),
  278. _T("WaitCommEvent"));
  279. }
  280. return bResult;
  281. }
  282. void xWaitCommEventWithAbort(
  283. HANDLE hFile, // handle to comm device
  284. PDWORD pdwEvtMask, // event type
  285. LPOVERLAPPED pOverlapped, // overlapped structure
  286. HANDLE hAbort) // Manual reset abort event
  287. {
  288. HANDLE hWait[2];
  289. hWait[0] = hAbort;
  290. hWait[1] = pOverlapped->hEvent;
  291. DWORD dwBytesRead;
  292. BOOL bResult = xWaitCommEvent(
  293. hFile,
  294. pdwEvtMask,
  295. pOverlapped);
  296. if (!bResult)
  297. {
  298. DWORD dwResult = xWaitForMultipleObjects(
  299. 2, // There are two to wait on
  300. hWait, // Our two event handles
  301. FALSE, // Only one event need to be pinged
  302. INFINITE); // Wait for ever
  303. switch (dwResult)
  304. {
  305. case (WAIT_OBJECT_0):
  306. CancelIo(
  307. hFile);
  308. throw CAbortExcept();
  309. break;
  310. case (WAIT_OBJECT_0 + 1):
  311. xGetOverlappedResult(
  312. hFile,
  313. pOverlapped,
  314. &dwBytesRead,
  315. TRUE);
  316. break;
  317. }
  318. }
  319. }
  320. void xEscapeCommFunction(
  321. HANDLE hFile, // handle to communications device
  322. DWORD dwFunc) // extended function to perform
  323. {
  324. BOOL bResult = ::EscapeCommFunction(
  325. hFile,
  326. dwFunc);
  327. if (!bResult)
  328. {
  329. throw CWin32ApiExcept(
  330. GetLastError(),
  331. _T("EscapeCommFunction"));
  332. }
  333. }
  334. void xClearCommBreak(
  335. HANDLE hFile) // handle to communications device
  336. {
  337. BOOL bResult = ::ClearCommBreak(
  338. hFile);
  339. if (!bResult)
  340. {
  341. throw CWin32ApiExcept(
  342. GetLastError(),
  343. _T("ClearCommBreak"));
  344. }
  345. }
  346. void xClearCommError(
  347. HANDLE hFile, // handle to communications device
  348. PDWORD pdwErrors, // error codes
  349. LPCOMSTAT pStat) // communications status
  350. {
  351. BOOL bResult = ::ClearCommError(
  352. hFile,
  353. pdwErrors,
  354. pStat);
  355. if (!bResult)
  356. {
  357. throw CWin32ApiExcept(
  358. GetLastError(),
  359. _T("ClearCommError"));
  360. }
  361. }
  362. void xBuildCommDCB(
  363. PCTSTR pcszDef, // device-control string
  364. LPDCB lpDCB) // device-control block
  365. {
  366. BOOL bResult = ::BuildCommDCB(
  367. pcszDef,
  368. lpDCB);
  369. if (!bResult)
  370. {
  371. throw CWin32ApiExcept(
  372. GetLastError(),
  373. _T("BuildCommDCB"));
  374. }
  375. }
  376. void xTransmitCommChar(
  377. HANDLE hFile,
  378. char cChar)
  379. {
  380. BOOL bResult = ::TransmitCommChar(
  381. hFile,
  382. cChar);
  383. if (!bResult)
  384. {
  385. throw CWin32ApiExcept(
  386. GetLastError(),
  387. _T("TransmitCommChar"));
  388. }
  389. }
  390. HANDLE xCreateThread(
  391. LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD
  392. DWORD dwStackSize, // initial stack size
  393. LPTHREAD_START_ROUTINE lpStartAddress, // thread function
  394. LPVOID lpParameter, // thread argument
  395. DWORD dwCreationFlags, // creation option
  396. LPDWORD lpThreadId) // thread identifier
  397. {
  398. HANDLE hResult = ::CreateThread(
  399. lpThreadAttributes, // SD
  400. dwStackSize, // initial stack size
  401. lpStartAddress, // thread function
  402. lpParameter, // thread argument
  403. dwCreationFlags, // creation option
  404. lpThreadId); // thread identifier
  405. if (NULL==hResult)
  406. {
  407. throw CWin32ApiExcept(
  408. GetLastError(),
  409. _T("CreateThread"));
  410. }
  411. return hResult;
  412. }
  413. void xTerminateThread(
  414. HANDLE hThread, // handle to thread
  415. DWORD dwExitCode) // exit code
  416. {
  417. throw CWin32ApiExcept(
  418. GetLastError(),
  419. _T("TerminateThread"));
  420. }
  421. UINT_PTR xSetTimer(
  422. HWND hWnd, // handle to window
  423. UINT_PTR nIDEvent, // timer identifier
  424. UINT uElapse, // time-out value
  425. TIMERPROC lpTimerFunc) // timer procedure
  426. {
  427. UINT_PTR pResult = ::SetTimer(
  428. hWnd,
  429. nIDEvent,
  430. uElapse,
  431. lpTimerFunc);
  432. if (0 == pResult)
  433. {
  434. throw CWin32ApiExcept(
  435. GetLastError(),
  436. _T("SetTimer"));
  437. }
  438. return pResult;
  439. }
  440. void xKillTimer(
  441. HWND hWnd, // handle to window
  442. UINT_PTR uIDEvent) // timer identifier
  443. {
  444. BOOL bResult = ::KillTimer(
  445. hWnd,
  446. uIDEvent);
  447. if (!bResult)
  448. {
  449. throw CWin32ApiExcept(
  450. GetLastError(),
  451. _T("KillTimer"));
  452. }
  453. }
  454. void xFillConsoleOutputAttribute(
  455. HANDLE hConsoleOutput,
  456. WORD wAttribute,
  457. DWORD nLength,
  458. COORD dwWriteCoord,
  459. LPDWORD lpNumberOfAttrsWritten)
  460. {
  461. BOOL bResult = ::FillConsoleOutputAttribute(
  462. hConsoleOutput,
  463. wAttribute,
  464. nLength,
  465. dwWriteCoord,
  466. lpNumberOfAttrsWritten);
  467. if (!bResult)
  468. {
  469. throw CWin32ApiExcept(
  470. GetLastError(),
  471. _T("FillConsoleOutputAttribute"));
  472. }
  473. }
  474. void xFillConsoleOutputCharacter(
  475. HANDLE hConsoleOutput,
  476. TCHAR cCharacter,
  477. DWORD nLength,
  478. COORD dwWriteCoord,
  479. LPDWORD lpNumberOfCharsWritten)
  480. {
  481. BOOL bResult = ::FillConsoleOutputCharacter(
  482. hConsoleOutput,
  483. cCharacter,
  484. nLength,
  485. dwWriteCoord,
  486. lpNumberOfCharsWritten);
  487. if (!bResult)
  488. {
  489. throw CWin32ApiExcept(
  490. GetLastError(),
  491. _T("FillConsoleOutputCharacter"));
  492. }
  493. }
  494. void xGetConsoleScreenBufferInfo(
  495. HANDLE hConsoleOutput,
  496. PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo)
  497. {
  498. BOOL bResult = ::GetConsoleScreenBufferInfo(
  499. hConsoleOutput,
  500. lpConsoleScreenBufferInfo);
  501. if (!bResult)
  502. {
  503. throw CWin32ApiExcept(
  504. GetLastError(),
  505. _T("GetConsoleScreenBufferInfo"));
  506. }
  507. }
  508. DWORD xGetConsoleTitle(
  509. LPTSTR lpConsoleTitle,
  510. DWORD nSize)
  511. {
  512. DWORD dwResult = ::GetConsoleTitle(
  513. lpConsoleTitle,
  514. nSize);
  515. if (!dwResult)
  516. {
  517. throw CWin32ApiExcept(
  518. GetLastError(),
  519. _T("GetConsoleTitle"));
  520. }
  521. return dwResult;
  522. }
  523. COORD xGetLargestConsoleWindowSize(
  524. HANDLE hConsoleOutput)
  525. {
  526. COORD Result = ::GetLargestConsoleWindowSize(
  527. hConsoleOutput);
  528. if (0==Result.X && 0==Result.Y)
  529. {
  530. throw CWin32ApiExcept(
  531. GetLastError(),
  532. _T("GetLargestConsoleWindowSize"));
  533. }
  534. return Result;
  535. }
  536. HANDLE xGetStdHandle(
  537. DWORD nStdHandle)
  538. {
  539. HANDLE hResult = ::GetStdHandle(
  540. nStdHandle);
  541. if (INVALID_HANDLE_VALUE==hResult)
  542. {
  543. throw CWin32ApiExcept(
  544. GetLastError(),
  545. _T("GetStdHandle"));
  546. }
  547. return hResult;
  548. }
  549. void xReadConsoleOutputAttribute(
  550. HANDLE hConsoleOutput,
  551. LPWORD lpAttribute,
  552. DWORD nLength,
  553. COORD dwReadCoord,
  554. LPDWORD lpNumberOfAttrsRead)
  555. {
  556. BOOL bResult = ::ReadConsoleOutputAttribute(
  557. hConsoleOutput,
  558. lpAttribute,
  559. nLength,
  560. dwReadCoord,
  561. lpNumberOfAttrsRead);
  562. if (!bResult)
  563. {
  564. throw CWin32ApiExcept(
  565. GetLastError(),
  566. _T("ReadConsoleOutputAttribute"));
  567. }
  568. }
  569. void xReadConsoleOutputCharacter(
  570. HANDLE hConsoleOutput,
  571. LPTSTR lpCharacter,
  572. DWORD nLength,
  573. COORD dwReadCoord,
  574. LPDWORD lpNumberOfCharsRead)
  575. {
  576. BOOL bResult = ::ReadConsoleOutputCharacter(
  577. hConsoleOutput,
  578. lpCharacter,
  579. nLength,
  580. dwReadCoord,
  581. lpNumberOfCharsRead);
  582. if (!bResult)
  583. {
  584. throw CWin32ApiExcept(
  585. GetLastError(),
  586. _T("ReadConsoleOutputCharacter"));
  587. }
  588. }
  589. void xSetConsoleScreenBufferSize(
  590. HANDLE hConsoleOutput,
  591. COORD dwSize)
  592. {
  593. BOOL bResult = ::SetConsoleScreenBufferSize(
  594. hConsoleOutput,
  595. dwSize);
  596. if (!bResult)
  597. {
  598. throw CWin32ApiExcept(
  599. GetLastError(),
  600. _T("SetConsoleScreenBufferSize"));
  601. }
  602. }
  603. void xSetConsoleTextAttribute(
  604. HANDLE hConsoleOutput,
  605. WORD wAttributes)
  606. {
  607. BOOL bResult = ::SetConsoleTextAttribute(
  608. hConsoleOutput,
  609. wAttributes);
  610. if (!bResult)
  611. {
  612. throw CWin32ApiExcept(
  613. GetLastError(),
  614. _T("SetConsoleTextAttribute"));
  615. }
  616. }
  617. void xSetConsoleTitle(
  618. LPCTSTR lpConsoleTitle)
  619. {
  620. BOOL bResult = ::SetConsoleTitle(
  621. lpConsoleTitle);
  622. if (!bResult)
  623. {
  624. throw CWin32ApiExcept(
  625. GetLastError(),
  626. _T("SetConsoleTitle"));
  627. }
  628. }
  629. void xSetConsoleWindowInfo(
  630. HANDLE hConsoleOutput,
  631. BOOL bAbsolute,
  632. CONST SMALL_RECT *lpConsoleWindow)
  633. {
  634. BOOL bResult = ::SetConsoleWindowInfo(
  635. hConsoleOutput,
  636. bAbsolute,
  637. lpConsoleWindow);
  638. if (!bResult)
  639. {
  640. throw CWin32ApiExcept(
  641. GetLastError(),
  642. _T("SetConsoleWindowInfo"));
  643. }
  644. }
  645. void xWriteConsole(
  646. HANDLE hConsoleOutput,
  647. CONST VOID *lpBuffer,
  648. DWORD nNumberOfCharsToWrite,
  649. LPDWORD lpNumberOfCharsWritten,
  650. LPVOID lpReserved)
  651. {
  652. BOOL bResult = ::WriteConsole(
  653. hConsoleOutput,
  654. lpBuffer,
  655. nNumberOfCharsToWrite,
  656. lpNumberOfCharsWritten,
  657. lpReserved);
  658. if (!bResult)
  659. {
  660. throw CWin32ApiExcept(
  661. GetLastError(),
  662. _T("WriteConsole"));
  663. }
  664. }
  665. void xWriteConsoleOutputAttribute(
  666. HANDLE hConsoleOutput,
  667. CONST WORD *lpAttribute,
  668. DWORD nLength,
  669. COORD dwWriteCoord,
  670. LPDWORD lpNumberOfAttrsWritten)
  671. {
  672. BOOL bResult = ::WriteConsoleOutputAttribute(
  673. hConsoleOutput,
  674. lpAttribute,
  675. nLength,
  676. dwWriteCoord,
  677. lpNumberOfAttrsWritten);
  678. if (!bResult)
  679. {
  680. throw CWin32ApiExcept(
  681. GetLastError(),
  682. _T("WriteConsoleOutputAttribute"));
  683. }
  684. }
  685. void xWriteConsoleOutputCharacter(
  686. HANDLE hConsoleOutput,
  687. LPCTSTR lpCharacter,
  688. DWORD nLength,
  689. COORD dwWriteCoord,
  690. LPDWORD lpNumberOfCharsWritten)
  691. {
  692. BOOL bResult = ::WriteConsoleOutputCharacter(
  693. hConsoleOutput,
  694. lpCharacter,
  695. nLength,
  696. dwWriteCoord,
  697. lpNumberOfCharsWritten);
  698. if (!bResult)
  699. {
  700. throw CWin32ApiExcept(
  701. GetLastError(),
  702. _T("WriteConsoleOutputCharacter"));
  703. }
  704. }
  705. HANDLE xCreateEvent(
  706. LPSECURITY_ATTRIBUTES lpEventAttributes, // SD
  707. BOOL bManualReset, // reset type
  708. BOOL bInitialState, // initial state
  709. LPCTSTR lpName) // object name
  710. {
  711. HANDLE hResult = ::CreateEvent(
  712. lpEventAttributes,
  713. bManualReset,
  714. bInitialState,
  715. lpName);
  716. if (INVALID_HANDLE_VALUE==hResult)
  717. {
  718. throw CWin32ApiExcept(
  719. GetLastError(),
  720. _T("CreateEvent"));
  721. }
  722. return hResult;
  723. }
  724. void xSetEvent(
  725. HANDLE hEvent)
  726. {
  727. BOOL bResult = ::SetEvent(
  728. hEvent);
  729. if (!bResult)
  730. {
  731. throw CWin32ApiExcept(
  732. GetLastError(),
  733. _T("SetEvent"));
  734. }
  735. }
  736. void xResetEvent(
  737. HANDLE hEvent)
  738. {
  739. BOOL bResult = ::ResetEvent(
  740. hEvent);
  741. if (!bResult)
  742. {
  743. throw CWin32ApiExcept(
  744. GetLastError(),
  745. _T("ResetEvent"));
  746. }
  747. }
  748. DWORD xWaitForMultipleObjects(
  749. DWORD nCount, // number of handles in array
  750. CONST HANDLE *lpHandles, // object-handle array
  751. BOOL fWaitAll, // wait option
  752. DWORD dwMilliseconds) // time-out interval
  753. {
  754. DWORD dwResult = ::WaitForMultipleObjects(
  755. nCount,
  756. lpHandles,
  757. fWaitAll,
  758. dwMilliseconds);
  759. if (WAIT_FAILED==dwResult)
  760. {
  761. throw CWin32ApiExcept(
  762. GetLastError(),
  763. _T("WaitForMultipleObjects"));
  764. }
  765. return dwResult;
  766. }
  767. DWORD xWaitForSingleObject(
  768. HANDLE hHandle, // handle to object
  769. DWORD dwMilliseconds) // time-out interval
  770. {
  771. DWORD dwResult = ::WaitForSingleObject(
  772. hHandle, // handle to object
  773. dwMilliseconds); // time-out interval
  774. if (WAIT_FAILED==dwResult)
  775. {
  776. throw CWin32ApiExcept(
  777. GetLastError(),
  778. _T("WaitForSingleObject"));
  779. }
  780. return dwResult;
  781. }
  782. PWSTR xMultiByteToWideChar(
  783. UINT CodePage, // code page
  784. DWORD dwFlags, // performance and mapping flags
  785. LPCSTR lpMultiByteStr) // wide-character string
  786. {
  787. int dResult = ::MultiByteToWideChar(
  788. CodePage,
  789. dwFlags,
  790. lpMultiByteStr,
  791. -1,
  792. NULL,
  793. 0);
  794. if (dResult==0)
  795. {
  796. throw CWin32ApiExcept(
  797. GetLastError(),
  798. _T("MultiByteToWideChar"));
  799. }
  800. auto_ptr<WCHAR> pszOutput(
  801. new WCHAR[dResult]);
  802. dResult = ::MultiByteToWideChar(
  803. CodePage,
  804. dwFlags,
  805. lpMultiByteStr,
  806. -1,
  807. pszOutput.get(),
  808. dResult);
  809. if (dResult==0)
  810. {
  811. throw CWin32ApiExcept(
  812. GetLastError(),
  813. _T("MultiByteToWideChar"));
  814. }
  815. return pszOutput.release();
  816. }
  817. PSTR xWideCharToMultiByte(
  818. UINT CodePage, // code page
  819. DWORD dwFlags, // performance and mapping flags
  820. LPCWSTR lpWideCharStr) // wide-character string
  821. {
  822. int dResult = ::WideCharToMultiByte(
  823. CodePage,
  824. dwFlags,
  825. lpWideCharStr,
  826. -1,
  827. NULL,
  828. 0,
  829. NULL,
  830. NULL);
  831. if (dResult==0)
  832. {
  833. throw CWin32ApiExcept(
  834. GetLastError(),
  835. _T("WideCharToMultiByte"));
  836. }
  837. auto_ptr<CHAR> pszOutput(
  838. new CHAR[dResult]);
  839. dResult = ::WideCharToMultiByte(
  840. CodePage,
  841. dwFlags,
  842. lpWideCharStr,
  843. -1,
  844. pszOutput.get(),
  845. dResult,
  846. NULL,
  847. NULL);
  848. if (dResult==0)
  849. {
  850. throw CWin32ApiExcept(
  851. GetLastError(),
  852. _T("WideCharToMultiByte"));
  853. }
  854. return pszOutput.release();
  855. }
  856. PWSTR xMakeWideChar(
  857. PTSTR pcszInput)
  858. {
  859. #ifdef _UNICODE
  860. auto_ptr<WCHAR> pszOutput(
  861. new WCHAR[wcslen(pcszInput)+1]);
  862. wcscpy(
  863. pszOutput.get(),
  864. pcszInput);
  865. #else
  866. auto_ptr<WCHAR> pszOutput(
  867. xMultiByteToWideChar(
  868. CP_ACP,
  869. 0,
  870. pcszInput));
  871. #endif
  872. return pszOutput.release();
  873. }
  874. PSTR xMakeMultiByte(
  875. PTSTR pcszInput)
  876. {
  877. #ifdef _UNICODE
  878. auto_ptr<CHAR> pszOutput(
  879. xWideCharToMultiByte(
  880. CP_ACP,
  881. 0,
  882. pcszInput));
  883. #else
  884. auto_ptr<CHAR> pszOuptut(
  885. new CHAR[strlen(pcszInput)+1]);
  886. strcpy(
  887. pszOutput.get(),
  888. pcszInput);
  889. #endif
  890. return pszOutput.release();
  891. }
  892. PTSTR xMakeDefaultChar(
  893. PWSTR pcszInput)
  894. {
  895. #ifdef _UNICODE
  896. auto_ptr<WCHAR> pszOutput(
  897. new WCHAR[wcslen(pcszInput)+1]);
  898. wcscpy(
  899. pszOutput.get(),
  900. pcszInput);
  901. #else
  902. auto_ptr<WCHAR> pszOuptut(
  903. xWideCharToMultiByte(
  904. CP_ACP,
  905. 0,
  906. pcszInput));
  907. #endif
  908. return pszOutput.release();
  909. }
  910. PTSTR xMakeDefaultChar(
  911. PSTR pcszInput)
  912. {
  913. #ifdef _UNICODE
  914. auto_ptr<WCHAR> pszOutput(
  915. xMultiByteToWideChar(
  916. CP_ACP,
  917. 0,
  918. pcszInput));
  919. #else
  920. auto_ptr<CHAR> pszOuptut(
  921. new CHAR[strlen(pcszInput)+1]);
  922. strcpy(
  923. pszOutput.get(),
  924. pcszInput);
  925. #endif
  926. return pszOutput.release();
  927. }
  928. DWORD xGetModuleFileName(
  929. HMODULE hModule, // handle to module
  930. LPTSTR lpFilename, // file name of module
  931. DWORD nSize) // size of buffer
  932. {
  933. DWORD dwResult = ::GetModuleFileName(
  934. hModule,
  935. lpFilename,
  936. nSize);
  937. if (0==dwResult)
  938. {
  939. throw CWin32ApiExcept(
  940. GetLastError(),
  941. _T("GetModuleFileName"));
  942. }
  943. if (dwResult == nSize) // This will happen if
  944. {
  945. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  946. return 0;
  947. }
  948. else
  949. {
  950. return dwResult;
  951. }
  952. }
  953. HANDLE xFindFirstFile(
  954. LPCTSTR lpFileName, // file name
  955. LPWIN32_FIND_DATA lpFindFileData) // data buffer
  956. {
  957. HANDLE hResult = ::FindFirstFile(
  958. lpFileName,
  959. lpFindFileData);
  960. if (INVALID_HANDLE_VALUE==hResult && GetLastError()!=ERROR_NO_MORE_FILES)
  961. {
  962. throw CWin32ApiExcept(
  963. GetLastError(),
  964. _T("FindFirstFile"));
  965. }
  966. return hResult;
  967. }
  968. BOOL xFindNextFile(
  969. HANDLE hFindFile, // search handle
  970. LPWIN32_FIND_DATA lpFindFileData) // data buffer
  971. {
  972. BOOL fResult = ::FindNextFile(
  973. hFindFile,
  974. lpFindFileData);
  975. if (!fResult && GetLastError()!=ERROR_NO_MORE_FILES)
  976. {
  977. throw CWin32ApiExcept(
  978. GetLastError(),
  979. _T("FindNextFile"));
  980. }
  981. return fResult;
  982. }
  983. HMODULE xLoadLibrary(
  984. LPCTSTR lpFileName) // file name of module
  985. {
  986. HMODULE hResult = ::LoadLibrary(
  987. lpFileName);
  988. if (NULL==hResult)
  989. {
  990. throw CWin32ApiExcept(
  991. GetLastError(),
  992. _T("LoadLibrary"));
  993. }
  994. return hResult;
  995. }
  996. BOOL xFreeLibrary(
  997. HMODULE hModule) // handle to DLL module
  998. {
  999. BOOL bResult = ::FreeLibrary(
  1000. hModule);
  1001. if (!bResult)
  1002. {
  1003. throw CWin32ApiExcept(
  1004. GetLastError(),
  1005. _T("FreeLibrary"));
  1006. }
  1007. return bResult;
  1008. }
  1009. FARPROC xGetProcAddress(
  1010. HMODULE hModule,
  1011. LPCSTR lpProcName)
  1012. {
  1013. FARPROC pResult = ::GetProcAddress(
  1014. hModule,
  1015. lpProcName);
  1016. if (NULL==pResult)
  1017. {
  1018. throw CWin32ApiExcept(
  1019. GetLastError(),
  1020. _T("GetProcAddress"));
  1021. }
  1022. return pResult;
  1023. }
  1024. CFindFile::~CFindFile()
  1025. {
  1026. if (m_hFind!=INVALID_HANDLE_VALUE)
  1027. {
  1028. FindClose(m_hFind);
  1029. }
  1030. }
  1031. BOOL CFindFile::First(PCTSTR pcszFileName)
  1032. {
  1033. if (m_hFind!=INVALID_HANDLE_VALUE)
  1034. {
  1035. FindClose(m_hFind);
  1036. }
  1037. m_hFind = xFindFirstFile(
  1038. pcszFileName,
  1039. &m_FindData);
  1040. return (m_hFind!=INVALID_HANDLE_VALUE);
  1041. }
  1042. BOOL CFindFile::Next()
  1043. {
  1044. return xFindNextFile(
  1045. m_hFind,
  1046. &m_FindData);
  1047. }
  1048. PWIN32_FIND_DATA CFindFile::Found()
  1049. {
  1050. return &m_FindData;
  1051. }
  1052. HANDLE xCreateMutex(
  1053. LPSECURITY_ATTRIBUTES lpMutexAttributes, // SD
  1054. BOOL bInitialOwner, // initial owner
  1055. LPCTSTR lpName) // object name
  1056. {
  1057. HANDLE hResult = ::CreateMutex(
  1058. lpMutexAttributes, // SD
  1059. bInitialOwner, // initial owner
  1060. lpName); // object name
  1061. if (NULL==hResult)
  1062. {
  1063. throw CWin32ApiExcept(
  1064. GetLastError(),
  1065. _T("CreateMutex"));
  1066. }
  1067. return hResult;
  1068. }
  1069. void xReleaseMutex(
  1070. HANDLE hMutex)
  1071. {
  1072. BOOL bResult = ::ReleaseMutex(
  1073. hMutex); // mutex object handle
  1074. if (!bResult)
  1075. {
  1076. throw CWin32ApiExcept(
  1077. GetLastError(),
  1078. _T("ReleaseMutex"));
  1079. }
  1080. }
  1081. PTSTR xFormatMessage(
  1082. DWORD dwFlags, // source and processing options
  1083. LPCVOID lpSource, // message source
  1084. DWORD dwMessageId, // message identifier
  1085. DWORD dwLanguageId, // language identifier
  1086. LPTSTR lpBuffer, // message buffer
  1087. DWORD nSize, // maximum size of message buffer
  1088. va_list *Arguments) // array of message inserts
  1089. {
  1090. auto_ptr<TCHAR> pszMessage;
  1091. PTSTR pszTempBuffer;
  1092. DWORD dwResult = ::FormatMessage(
  1093. dwFlags,
  1094. lpSource,
  1095. dwMessageId,
  1096. dwLanguageId,
  1097. (LPTSTR)&pszTempBuffer,
  1098. nSize,
  1099. Arguments);
  1100. if (dwResult==0)
  1101. {
  1102. throw CWin32ApiExcept(
  1103. GetLastError(),
  1104. _T("FormatMessage"));
  1105. }
  1106. try
  1107. {
  1108. auto_ptr<TCHAR> pszMessage(
  1109. new TCHAR[_tcslen(pszTempBuffer)+1]);
  1110. _tcscpy(
  1111. pszMessage.get(),
  1112. pszTempBuffer);
  1113. if (lpBuffer!=NULL)
  1114. {
  1115. *((PTSTR*)lpBuffer) = pszMessage.get();
  1116. }
  1117. LocalFree(
  1118. pszTempBuffer);
  1119. return pszMessage.release();
  1120. }
  1121. catch (...)
  1122. {
  1123. //
  1124. // This needs to be freed regardless.
  1125. //
  1126. LocalFree(
  1127. pszTempBuffer);
  1128. throw;
  1129. }
  1130. }