Source code of Windows XP (NT5)
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.

1309 lines
27 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. BOOL bResult = ::TerminateThread(
  418. hThread,
  419. dwExitCode);
  420. if (!bResult)
  421. {
  422. throw CWin32ApiExcept(
  423. GetLastError(),
  424. _T("TerminateThread"));
  425. }
  426. }
  427. UINT_PTR xSetTimer(
  428. HWND hWnd, // handle to window
  429. UINT_PTR nIDEvent, // timer identifier
  430. UINT uElapse, // time-out value
  431. TIMERPROC lpTimerFunc) // timer procedure
  432. {
  433. UINT_PTR pResult = ::SetTimer(
  434. hWnd,
  435. nIDEvent,
  436. uElapse,
  437. lpTimerFunc);
  438. if (0 == pResult)
  439. {
  440. throw CWin32ApiExcept(
  441. GetLastError(),
  442. _T("SetTimer"));
  443. }
  444. return pResult;
  445. }
  446. void xKillTimer(
  447. HWND hWnd, // handle to window
  448. UINT_PTR uIDEvent) // timer identifier
  449. {
  450. BOOL bResult = ::KillTimer(
  451. hWnd,
  452. uIDEvent);
  453. if (!bResult)
  454. {
  455. throw CWin32ApiExcept(
  456. GetLastError(),
  457. _T("KillTimer"));
  458. }
  459. }
  460. void xFillConsoleOutputAttribute(
  461. HANDLE hConsoleOutput,
  462. WORD wAttribute,
  463. DWORD nLength,
  464. COORD dwWriteCoord,
  465. LPDWORD lpNumberOfAttrsWritten)
  466. {
  467. BOOL bResult = ::FillConsoleOutputAttribute(
  468. hConsoleOutput,
  469. wAttribute,
  470. nLength,
  471. dwWriteCoord,
  472. lpNumberOfAttrsWritten);
  473. if (!bResult)
  474. {
  475. throw CWin32ApiExcept(
  476. GetLastError(),
  477. _T("FillConsoleOutputAttribute"));
  478. }
  479. }
  480. void xFillConsoleOutputCharacter(
  481. HANDLE hConsoleOutput,
  482. TCHAR cCharacter,
  483. DWORD nLength,
  484. COORD dwWriteCoord,
  485. LPDWORD lpNumberOfCharsWritten)
  486. {
  487. BOOL bResult = ::FillConsoleOutputCharacter(
  488. hConsoleOutput,
  489. cCharacter,
  490. nLength,
  491. dwWriteCoord,
  492. lpNumberOfCharsWritten);
  493. if (!bResult)
  494. {
  495. throw CWin32ApiExcept(
  496. GetLastError(),
  497. _T("FillConsoleOutputCharacter"));
  498. }
  499. }
  500. void xGetConsoleScreenBufferInfo(
  501. HANDLE hConsoleOutput,
  502. PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo)
  503. {
  504. BOOL bResult = ::GetConsoleScreenBufferInfo(
  505. hConsoleOutput,
  506. lpConsoleScreenBufferInfo);
  507. if (!bResult)
  508. {
  509. throw CWin32ApiExcept(
  510. GetLastError(),
  511. _T("GetConsoleScreenBufferInfo"));
  512. }
  513. }
  514. DWORD xGetConsoleTitle(
  515. LPTSTR lpConsoleTitle,
  516. DWORD nSize)
  517. {
  518. DWORD dwResult = ::GetConsoleTitle(
  519. lpConsoleTitle,
  520. nSize);
  521. if (!dwResult)
  522. {
  523. throw CWin32ApiExcept(
  524. GetLastError(),
  525. _T("GetConsoleTitle"));
  526. }
  527. return dwResult;
  528. }
  529. COORD xGetLargestConsoleWindowSize(
  530. HANDLE hConsoleOutput)
  531. {
  532. COORD Result = ::GetLargestConsoleWindowSize(
  533. hConsoleOutput);
  534. if (0==Result.X && 0==Result.Y)
  535. {
  536. throw CWin32ApiExcept(
  537. GetLastError(),
  538. _T("GetLargestConsoleWindowSize"));
  539. }
  540. return Result;
  541. }
  542. HANDLE xGetStdHandle(
  543. DWORD nStdHandle)
  544. {
  545. HANDLE hResult = ::GetStdHandle(
  546. nStdHandle);
  547. if (INVALID_HANDLE_VALUE==hResult)
  548. {
  549. throw CWin32ApiExcept(
  550. GetLastError(),
  551. _T("GetStdHandle"));
  552. }
  553. return hResult;
  554. }
  555. void xReadConsoleOutputAttribute(
  556. HANDLE hConsoleOutput,
  557. LPWORD lpAttribute,
  558. DWORD nLength,
  559. COORD dwReadCoord,
  560. LPDWORD lpNumberOfAttrsRead)
  561. {
  562. BOOL bResult = ::ReadConsoleOutputAttribute(
  563. hConsoleOutput,
  564. lpAttribute,
  565. nLength,
  566. dwReadCoord,
  567. lpNumberOfAttrsRead);
  568. if (!bResult)
  569. {
  570. throw CWin32ApiExcept(
  571. GetLastError(),
  572. _T("ReadConsoleOutputAttribute"));
  573. }
  574. }
  575. void xReadConsoleOutputCharacter(
  576. HANDLE hConsoleOutput,
  577. LPTSTR lpCharacter,
  578. DWORD nLength,
  579. COORD dwReadCoord,
  580. LPDWORD lpNumberOfCharsRead)
  581. {
  582. BOOL bResult = ::ReadConsoleOutputCharacter(
  583. hConsoleOutput,
  584. lpCharacter,
  585. nLength,
  586. dwReadCoord,
  587. lpNumberOfCharsRead);
  588. if (!bResult)
  589. {
  590. throw CWin32ApiExcept(
  591. GetLastError(),
  592. _T("ReadConsoleOutputCharacter"));
  593. }
  594. }
  595. void xSetConsoleScreenBufferSize(
  596. HANDLE hConsoleOutput,
  597. COORD dwSize)
  598. {
  599. BOOL bResult = ::SetConsoleScreenBufferSize(
  600. hConsoleOutput,
  601. dwSize);
  602. if (!bResult)
  603. {
  604. throw CWin32ApiExcept(
  605. GetLastError(),
  606. _T("SetConsoleScreenBufferSize"));
  607. }
  608. }
  609. void xSetConsoleTextAttribute(
  610. HANDLE hConsoleOutput,
  611. WORD wAttributes)
  612. {
  613. BOOL bResult = ::SetConsoleTextAttribute(
  614. hConsoleOutput,
  615. wAttributes);
  616. if (!bResult)
  617. {
  618. throw CWin32ApiExcept(
  619. GetLastError(),
  620. _T("SetConsoleTextAttribute"));
  621. }
  622. }
  623. void xSetConsoleTitle(
  624. LPCTSTR lpConsoleTitle)
  625. {
  626. BOOL bResult = ::SetConsoleTitle(
  627. lpConsoleTitle);
  628. if (!bResult)
  629. {
  630. throw CWin32ApiExcept(
  631. GetLastError(),
  632. _T("SetConsoleTitle"));
  633. }
  634. }
  635. void xSetConsoleWindowInfo(
  636. HANDLE hConsoleOutput,
  637. BOOL bAbsolute,
  638. CONST SMALL_RECT *lpConsoleWindow)
  639. {
  640. BOOL bResult = ::SetConsoleWindowInfo(
  641. hConsoleOutput,
  642. bAbsolute,
  643. lpConsoleWindow);
  644. if (!bResult)
  645. {
  646. throw CWin32ApiExcept(
  647. GetLastError(),
  648. _T("SetConsoleWindowInfo"));
  649. }
  650. }
  651. void xWriteConsole(
  652. HANDLE hConsoleOutput,
  653. CONST VOID *lpBuffer,
  654. DWORD nNumberOfCharsToWrite,
  655. LPDWORD lpNumberOfCharsWritten,
  656. LPVOID lpReserved)
  657. {
  658. BOOL bResult = ::WriteConsole(
  659. hConsoleOutput,
  660. lpBuffer,
  661. nNumberOfCharsToWrite,
  662. lpNumberOfCharsWritten,
  663. lpReserved);
  664. if (!bResult)
  665. {
  666. throw CWin32ApiExcept(
  667. GetLastError(),
  668. _T("WriteConsole"));
  669. }
  670. }
  671. void xWriteConsoleOutputAttribute(
  672. HANDLE hConsoleOutput,
  673. CONST WORD *lpAttribute,
  674. DWORD nLength,
  675. COORD dwWriteCoord,
  676. LPDWORD lpNumberOfAttrsWritten)
  677. {
  678. BOOL bResult = ::WriteConsoleOutputAttribute(
  679. hConsoleOutput,
  680. lpAttribute,
  681. nLength,
  682. dwWriteCoord,
  683. lpNumberOfAttrsWritten);
  684. if (!bResult)
  685. {
  686. throw CWin32ApiExcept(
  687. GetLastError(),
  688. _T("WriteConsoleOutputAttribute"));
  689. }
  690. }
  691. void xWriteConsoleOutputCharacter(
  692. HANDLE hConsoleOutput,
  693. LPCTSTR lpCharacter,
  694. DWORD nLength,
  695. COORD dwWriteCoord,
  696. LPDWORD lpNumberOfCharsWritten)
  697. {
  698. BOOL bResult = ::WriteConsoleOutputCharacter(
  699. hConsoleOutput,
  700. lpCharacter,
  701. nLength,
  702. dwWriteCoord,
  703. lpNumberOfCharsWritten);
  704. if (!bResult)
  705. {
  706. throw CWin32ApiExcept(
  707. GetLastError(),
  708. _T("WriteConsoleOutputCharacter"));
  709. }
  710. }
  711. HANDLE xCreateEvent(
  712. LPSECURITY_ATTRIBUTES lpEventAttributes, // SD
  713. BOOL bManualReset, // reset type
  714. BOOL bInitialState, // initial state
  715. LPCTSTR lpName) // object name
  716. {
  717. HANDLE hResult = ::CreateEvent(
  718. lpEventAttributes,
  719. bManualReset,
  720. bInitialState,
  721. lpName);
  722. if (INVALID_HANDLE_VALUE==hResult)
  723. {
  724. throw CWin32ApiExcept(
  725. GetLastError(),
  726. _T("CreateEvent"));
  727. }
  728. return hResult;
  729. }
  730. void xSetEvent(
  731. HANDLE hEvent)
  732. {
  733. BOOL bResult = ::SetEvent(
  734. hEvent);
  735. if (!bResult)
  736. {
  737. throw CWin32ApiExcept(
  738. GetLastError(),
  739. _T("SetEvent"));
  740. }
  741. }
  742. void xResetEvent(
  743. HANDLE hEvent)
  744. {
  745. BOOL bResult = ::ResetEvent(
  746. hEvent);
  747. if (!bResult)
  748. {
  749. throw CWin32ApiExcept(
  750. GetLastError(),
  751. _T("ResetEvent"));
  752. }
  753. }
  754. DWORD xWaitForMultipleObjects(
  755. DWORD nCount, // number of handles in array
  756. CONST HANDLE *lpHandles, // object-handle array
  757. BOOL fWaitAll, // wait option
  758. DWORD dwMilliseconds) // time-out interval
  759. {
  760. DWORD dwResult = ::WaitForMultipleObjects(
  761. nCount,
  762. lpHandles,
  763. fWaitAll,
  764. dwMilliseconds);
  765. if (WAIT_FAILED==dwResult)
  766. {
  767. throw CWin32ApiExcept(
  768. GetLastError(),
  769. _T("WaitForMultipleObjects"));
  770. }
  771. return dwResult;
  772. }
  773. DWORD xWaitForSingleObject(
  774. HANDLE hHandle, // handle to object
  775. DWORD dwMilliseconds) // time-out interval
  776. {
  777. DWORD dwResult = ::WaitForSingleObject(
  778. hHandle, // handle to object
  779. dwMilliseconds); // time-out interval
  780. if (WAIT_FAILED==dwResult)
  781. {
  782. throw CWin32ApiExcept(
  783. GetLastError(),
  784. _T("WaitForSingleObject"));
  785. }
  786. return dwResult;
  787. }
  788. PWSTR xMultiByteToWideChar(
  789. UINT CodePage, // code page
  790. DWORD dwFlags, // performance and mapping flags
  791. LPCSTR lpMultiByteStr) // wide-character string
  792. {
  793. int dResult = ::MultiByteToWideChar(
  794. CodePage,
  795. dwFlags,
  796. lpMultiByteStr,
  797. -1,
  798. NULL,
  799. 0);
  800. if (dResult==0)
  801. {
  802. throw CWin32ApiExcept(
  803. GetLastError(),
  804. _T("MultiByteToWideChar"));
  805. }
  806. auto_ptr<WCHAR> pszOutput(
  807. new WCHAR[dResult]);
  808. dResult = ::MultiByteToWideChar(
  809. CodePage,
  810. dwFlags,
  811. lpMultiByteStr,
  812. -1,
  813. pszOutput.get(),
  814. dResult);
  815. if (dResult==0)
  816. {
  817. throw CWin32ApiExcept(
  818. GetLastError(),
  819. _T("MultiByteToWideChar"));
  820. }
  821. return pszOutput.release();
  822. }
  823. PSTR xWideCharToMultiByte(
  824. UINT CodePage, // code page
  825. DWORD dwFlags, // performance and mapping flags
  826. LPCWSTR lpWideCharStr) // wide-character string
  827. {
  828. int dResult = ::WideCharToMultiByte(
  829. CodePage,
  830. dwFlags,
  831. lpWideCharStr,
  832. -1,
  833. NULL,
  834. 0,
  835. NULL,
  836. NULL);
  837. if (dResult==0)
  838. {
  839. throw CWin32ApiExcept(
  840. GetLastError(),
  841. _T("WideCharToMultiByte"));
  842. }
  843. auto_ptr<CHAR> pszOutput(
  844. new CHAR[dResult]);
  845. dResult = ::WideCharToMultiByte(
  846. CodePage,
  847. dwFlags,
  848. lpWideCharStr,
  849. -1,
  850. pszOutput.get(),
  851. dResult,
  852. NULL,
  853. NULL);
  854. if (dResult==0)
  855. {
  856. throw CWin32ApiExcept(
  857. GetLastError(),
  858. _T("WideCharToMultiByte"));
  859. }
  860. return pszOutput.release();
  861. }
  862. PWSTR xMakeWideChar(
  863. PTSTR pcszInput)
  864. {
  865. #ifdef _UNICODE
  866. auto_ptr<WCHAR> pszOutput(
  867. new WCHAR[wcslen(pcszInput)+1]);
  868. wcscpy(
  869. pszOutput.get(),
  870. pcszInput);
  871. #else
  872. auto_ptr<WCHAR> pszOutput(
  873. xMultiByteToWideChar(
  874. CP_ACP,
  875. 0,
  876. pcszInput));
  877. #endif
  878. return pszOutput.release();
  879. }
  880. PSTR xMakeMultiByte(
  881. PTSTR pcszInput)
  882. {
  883. #ifdef _UNICODE
  884. auto_ptr<CHAR> pszOutput(
  885. xWideCharToMultiByte(
  886. CP_ACP,
  887. 0,
  888. pcszInput));
  889. #else
  890. auto_ptr<CHAR> pszOuptut(
  891. new CHAR[strlen(pcszInput)+1]);
  892. strcpy(
  893. pszOutput.get(),
  894. pcszInput);
  895. #endif
  896. return pszOutput.release();
  897. }
  898. PTSTR xMakeDefaultChar(
  899. PWSTR pcszInput)
  900. {
  901. #ifdef _UNICODE
  902. auto_ptr<WCHAR> pszOutput(
  903. new WCHAR[wcslen(pcszInput)+1]);
  904. wcscpy(
  905. pszOutput.get(),
  906. pcszInput);
  907. #else
  908. auto_ptr<WCHAR> pszOuptut(
  909. xWideCharToMultiByte(
  910. CP_ACP,
  911. 0,
  912. pcszInput));
  913. #endif
  914. return pszOutput.release();
  915. }
  916. PTSTR xMakeDefaultChar(
  917. PSTR pcszInput)
  918. {
  919. #ifdef _UNICODE
  920. auto_ptr<WCHAR> pszOutput(
  921. xMultiByteToWideChar(
  922. CP_ACP,
  923. 0,
  924. pcszInput));
  925. #else
  926. auto_ptr<CHAR> pszOuptut(
  927. new CHAR[strlen(pcszInput)+1]);
  928. strcpy(
  929. pszOutput.get(),
  930. pcszInput);
  931. #endif
  932. return pszOutput.release();
  933. }
  934. DWORD xGetModuleFileName(
  935. HMODULE hModule, // handle to module
  936. LPTSTR lpFilename, // file name of module
  937. DWORD nSize) // size of buffer
  938. {
  939. DWORD dwResult = ::GetModuleFileName(
  940. hModule,
  941. lpFilename,
  942. nSize);
  943. if (0==dwResult)
  944. {
  945. throw CWin32ApiExcept(
  946. GetLastError(),
  947. _T("GetModuleFileName"));
  948. }
  949. if (dwResult == nSize) // This will happen if
  950. {
  951. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  952. return 0;
  953. }
  954. else
  955. {
  956. return dwResult;
  957. }
  958. }
  959. HANDLE xFindFirstFile(
  960. LPCTSTR lpFileName, // file name
  961. LPWIN32_FIND_DATA lpFindFileData) // data buffer
  962. {
  963. HANDLE hResult = ::FindFirstFile(
  964. lpFileName,
  965. lpFindFileData);
  966. if (INVALID_HANDLE_VALUE==hResult && GetLastError()!=ERROR_NO_MORE_FILES)
  967. {
  968. throw CWin32ApiExcept(
  969. GetLastError(),
  970. _T("FindFirstFile"));
  971. }
  972. return hResult;
  973. }
  974. BOOL xFindNextFile(
  975. HANDLE hFindFile, // search handle
  976. LPWIN32_FIND_DATA lpFindFileData) // data buffer
  977. {
  978. BOOL fResult = ::FindNextFile(
  979. hFindFile,
  980. lpFindFileData);
  981. if (!fResult && GetLastError()!=ERROR_NO_MORE_FILES)
  982. {
  983. throw CWin32ApiExcept(
  984. GetLastError(),
  985. _T("FindNextFile"));
  986. }
  987. return fResult;
  988. }
  989. HMODULE xLoadLibrary(
  990. LPCTSTR lpFileName) // file name of module
  991. {
  992. HMODULE hResult = ::LoadLibrary(
  993. lpFileName);
  994. if (NULL==hResult)
  995. {
  996. throw CWin32ApiExcept(
  997. GetLastError(),
  998. _T("LoadLibrary"));
  999. }
  1000. return hResult;
  1001. }
  1002. FARPROC xGetProcAddress(
  1003. HMODULE hModule,
  1004. LPCSTR lpProcName)
  1005. {
  1006. FARPROC pResult = ::GetProcAddress(
  1007. hModule,
  1008. lpProcName);
  1009. if (NULL==pResult)
  1010. {
  1011. throw CWin32ApiExcept(
  1012. GetLastError(),
  1013. _T("GetProcAddress"));
  1014. }
  1015. return pResult;
  1016. }
  1017. CFindFile::~CFindFile()
  1018. {
  1019. if (m_hFind!=INVALID_HANDLE_VALUE)
  1020. {
  1021. FindClose(m_hFind);
  1022. }
  1023. }
  1024. BOOL CFindFile::First(PCTSTR pcszFileName)
  1025. {
  1026. if (m_hFind!=INVALID_HANDLE_VALUE)
  1027. {
  1028. FindClose(m_hFind);
  1029. }
  1030. m_hFind = xFindFirstFile(
  1031. pcszFileName,
  1032. &m_FindData);
  1033. return (m_hFind!=INVALID_HANDLE_VALUE);
  1034. }
  1035. BOOL CFindFile::Next()
  1036. {
  1037. return xFindNextFile(
  1038. m_hFind,
  1039. &m_FindData);
  1040. }
  1041. PWIN32_FIND_DATA CFindFile::Found()
  1042. {
  1043. return &m_FindData;
  1044. }
  1045. HANDLE xCreateMutex(
  1046. LPSECURITY_ATTRIBUTES lpMutexAttributes, // SD
  1047. BOOL bInitialOwner, // initial owner
  1048. LPCTSTR lpName) // object name
  1049. {
  1050. HANDLE hResult = ::CreateMutex(
  1051. lpMutexAttributes, // SD
  1052. bInitialOwner, // initial owner
  1053. lpName); // object name
  1054. if (NULL==hResult)
  1055. {
  1056. throw CWin32ApiExcept(
  1057. GetLastError(),
  1058. _T("CreateMutex"));
  1059. }
  1060. return hResult;
  1061. }
  1062. void xReleaseMutex(
  1063. HANDLE hMutex)
  1064. {
  1065. BOOL bResult = ::ReleaseMutex(
  1066. hMutex); // mutex object handle
  1067. if (!bResult)
  1068. {
  1069. throw CWin32ApiExcept(
  1070. GetLastError(),
  1071. _T("ReleaseMutex"));
  1072. }
  1073. }