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.

850 lines
24 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1997 - 1999
  3. Module Name:
  4. CalMsgs
  5. Abstract:
  6. This module provides Message logging services.
  7. Author:
  8. Doug Barlow (dbarlow) 5/29/1997
  9. Environment:
  10. Win32, C++
  11. Notes:
  12. --*/
  13. #define __SUBROUTINE__
  14. #ifndef WIN32_LEAN_AND_MEAN
  15. #define WIN32_LEAN_AND_MEAN
  16. #endif
  17. #include <windows.h>
  18. #include <tchar.h>
  19. #include <WinSCard.h>
  20. #include <CalMsgs.h>
  21. #include <CalaisLb.h>
  22. #include <CalCom.h>
  23. #include <scarderr.h>
  24. #ifdef DBG
  25. #include <stdio.h>
  26. #include <stdarg.h>
  27. #endif
  28. #ifndef FACILITY_SCARD
  29. #define FACILITY_SCARD 16
  30. #endif
  31. // #define ErrorCode(x) (0xc0000000 | (FACILITY_SCARD << 16) + (x))
  32. // #define WarnCode(x) (0x80000000 | (FACILITY_SCARD << 16) + (x))
  33. // #define InfoCode(x) (0x40000000 | (FACILITY_SCARD << 16) + (x))
  34. // #define SuccessCode(x) ((FACILITY_SCARD << 16) + (x))
  35. #if defined(_DEBUG)
  36. BOOL g_fDebug = FALSE;
  37. BOOL g_fGuiWarnings = TRUE;
  38. WORD g_wGuiSeverity = EVENTLOG_WARNING_TYPE | EVENTLOG_ERROR_TYPE;
  39. WORD g_wLogSeverity = 0x03;
  40. #ifndef DBG
  41. #define DBG
  42. #endif
  43. #elif defined(DBG)
  44. BOOL g_fDebug = FALSE;
  45. BOOL g_fGuiWarnings = FALSE;
  46. WORD g_wGuiSeverity = EVENTLOG_ERROR_TYPE;
  47. WORD g_wLogSeverity = EVENTLOG_WARNING_TYPE | EVENTLOG_ERROR_TYPE;
  48. #else
  49. WORD g_wLogSeverity = EVENTLOG_ERROR_TYPE;
  50. #endif
  51. static LPCTSTR l_szServiceName = TEXT("SCard Client");
  52. static HANDLE l_hEventLogger = NULL;
  53. static BOOL l_fServer = FALSE;
  54. static const TCHAR l_szDefaultMessage[] = TEXT("SCARDSVR!CalaisMessageLog error logging is broken: %1");
  55. //
  56. // Common global strings.
  57. //
  58. const LPCTSTR g_rgszDefaultStrings[]
  59. = {
  60. /* CALSTR_CALAISEXECUTABLE */ TEXT("%windir%\\system32\\SCardSvr.exe"),
  61. /* CALSTR_PRIMARYSERVICE */ TEXT("SCardSvr"),
  62. /* CALSTR_LEGACYSERVICE */ TEXT("SCardDrv"),
  63. /* CALSTR_CALAISREGISTRYKEY */ TEXT("SOFTWARE\\Microsoft\\Cryptography\\Calais"),
  64. /* CALSTR_READERREGISTRYKEY */ TEXT("SOFTWARE\\Microsoft\\Cryptography\\Calais\\Readers"),
  65. /* CALSTR_SMARTCARDREGISTRYKEY */ TEXT("SOFTWARE\\Microsoft\\Cryptography\\Calais\\SmartCards"),
  66. /* CALSTR_READERREGISTRYSUBKEY */ TEXT("Readers"),
  67. /* CALSTR_DEVICEREGISTRYSUBKEY */ TEXT("Device"),
  68. /* CALSTR_GROUPSREGISTRYSUBKEY */ TEXT("Groups"),
  69. /* CALSTR_ATRREGISTRYSUBKEY */ TEXT("ATR"),
  70. /* CALSTR_ATRMASKREGISTRYSUBKEY */ TEXT("ATRMask"),
  71. /* CALSTR_INTERFACESREGISTRYSUBKEY */ TEXT("Supported Interfaces"),
  72. /* CALSTR_PRIMARYPROVIDERSUBKEY */ TEXT("Primary Provider"),
  73. /* CALSTR_CRYPTOPROVIDERSUBKEY */ TEXT("Crypto Provider"),
  74. /* CALSTR_SERVICESREGISTRYKEY */ TEXT("SYSTEM\\CurrentControlSet\\Services"),
  75. /* CALSTR_EVENTLOGREGISTRYKEY */ TEXT("SYSTEM\\CurrentControlSet\\Services\\EventLog"),
  76. /* CALSTR_SYSTEMREGISTRYSUBKEY */ TEXT("System"),
  77. /* CALSTR_EVENTMESSAGEFILESUBKEY */ TEXT("EventMessageFile"),
  78. /* CALSTR_TYPESSUPPORTEDSUBKEY */ TEXT("TypesSupported"),
  79. /* CALSTR_PNPDEVICEREGISTRYKEY */ TEXT("SYSTEM\\CurrentControlSet\\Control\\DeviceClasses\\{50dd5230-ba8a-11d1-bf5d-0000f805f530}"),
  80. /* CALSTR_SYMBOLICLINKSUBKEY */ TEXT("SymbolicLink"),
  81. /* CALSTR_VXDPATHREGISTRYKEY */ TEXT("System\\CurrentControlSet\\Services\\VxD\\Smclib\\Devices"),
  82. /* CALSTR_LEGACYDEPENDONGROUP */ TEXT("+Smart Card Reader"),
  83. /* CALSTR_NEWREADEREVENTNAME */ TEXT("Global\\Microsoft Smart Card Resource Manager New Reader"),
  84. /* CALSTR_STARTEDEVENTNAME */ TEXT("Global\\Microsoft Smart Card Resource Manager Started"),
  85. /* CALSTR_CANCELEVENTPREFIX */ TEXT("Global\\Microsoft Smart Card Cancel Event for %1!d!"),
  86. /* CALSTR_COMMPIPENAME */ TEXT("Microsoft Smart Card Resource Manager"),
  87. /* CALSTR_LEGACYDEVICEHEADER */ TEXT("\\\\.\\"),
  88. /* CALSTR_LEGACYDEVICENAME */ TEXT("SCReader"),
  89. /* CALSTR_MAXLEGACYDEVICES */ TEXT("MaxLegacyDevices"),
  90. /* CALSTR_MAXDEFAULTBUFFER */ TEXT("MaxDefaultBuffer"),
  91. /* CALSTR_PIPEDEVICEHEADER */ TEXT("\\\\.\\pipe\\"),
  92. /* CALSTR_SERVICEDEPENDENCIES */ TEXT("PlugPlay\000"),
  93. /* CALSTR_SPECIALREADERHEADER */ TEXT("\\\\?PNP?\\"),
  94. /* CALSTR_ACTIVEREADERCOUNTREADER */ TEXT("NOTIFICATION"),
  95. /* CALSTR_CERTPROPREGISTRY */ TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\Notify"),
  96. /* CALSTR_CERTPROPKEY */ TEXT("ScCertProp"),
  97. /* CALSTR_DLLNAME */ TEXT("DLLName"),
  98. /* CALSTR_LOGON */ TEXT("Logon"),
  99. /* CALSTR_LOGOFF */ TEXT("Logoff"),
  100. /* CALSTR_LOCK */ TEXT("Lock"),
  101. /* CALSTR_UNLOCK */ TEXT("Unlock"),
  102. /* CALSTR_ENABLED */ TEXT("Enabled"),
  103. /* CALSTR_IMPERSONATE */ TEXT("Impersonate"),
  104. /* CALSTR_ASYNCHRONOUS */ TEXT("Asynchronous"),
  105. /* CALSTR_CERTPROPDLL */ TEXT("WlNotify.dll"),
  106. /* CALSTR_CERTPROPSTART */ TEXT("SCardStartCertProp"),
  107. /* CALSTR_CERTPROPSTOP */ TEXT("SCardStopCertProp"),
  108. /* CALSTR_CERTPROPSUSPEND */ TEXT("SCardSuspendCertProp"),
  109. /* CALSTR_CERTPROPRESUME */ TEXT("SCardResumeCertProp"),
  110. /* CALSTR_SMARTCARDINSERTION */ TEXT("SmartcardInsertion"),
  111. /* CALSTR_SMARTCARDREMOVAL */ TEXT("SmartcardRemoval"),
  112. /* CALSTR_APPEVENTS */ TEXT("AppEvents"),
  113. /* CALSTR_EVENTLABELS */ TEXT("EventLabels"),
  114. /* CALSTR_DOT_DEFAULT */ TEXT(".Default"),
  115. /* CALSTR_DOT_CURRENT */ TEXT(".Current"),
  116. /* CALSTR_SOUNDSREGISTRY */ TEXT("Schemes\\Apps\\.Default"),
  117. /* CALSTR_LOGONREGISTRY */ TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"),
  118. /* CALSTR_LOGONREMOVEOPTION */ TEXT("ScRemoveOption"),
  119. /* CALSTR_STOPPEDEVENTNAME */ TEXT("Global\\Microsoft Smart Card Resource Manager Stopped"),
  120. // Unused
  121. // /* CALSTR_TEMPLATEREGISTRYKEY */ TEXT("SOFTWARE\\Microsoft\\Cryptography\\Calais\\SmartCard Templates"),
  122. // /* CALSTR_OEMCONFIGREGISTRYSUBKEY */ TEXT("OEM Configuration"),
  123. // Debug only
  124. /* CALSTR_DEBUGSERVICE */ TEXT("SCardDbg"),
  125. /* CALSTR_DEBUGREGISTRYSUBKEY */ TEXT("Debug"),
  126. #ifdef DBG
  127. /* CALSTR_DEBUGLOGSUBKEY */ TEXT("Debug"),
  128. /* CALSTR_GUIWARNINGSUBKEY */ TEXT("GuiWarnings"),
  129. /* CALSTR_LOGSEVERITYSUBKEY */ TEXT("LogSeverity"),
  130. /* CALSTR_GUISEVERITYSUBKEY */ TEXT("GuiSeverity"),
  131. /* CALSTR_APITRACEFILENAME */ TEXT("C:\\SCard.log"),
  132. /* CALSTR_DRIVERTRACEFILENAME */ TEXT("C:\\Calais.log"),
  133. /* CALSTR_MESSAGETAG */ TEXT(" *MESSAGE* "),
  134. /* CALSTR_INFOMESSAGETAG */ TEXT(" *INFO* "),
  135. /* CALSTR_WARNINGMESSAGETAG */ TEXT(" *WARNING* "),
  136. /* CALSTR_ERRORMESSAGETAG */ TEXT(" *ERROR* "),
  137. /* CALSTR_DEBUGSERVICEDISPLAY */ TEXT("Smart Card Debug"),
  138. /* CALSTR_DEBUGSERVICEDESC */ TEXT("Start this service first to debug Smart card service startup"),
  139. #endif
  140. NULL };
  141. /*++
  142. CalaisMessageLog:
  143. This function and it's derivatives provide convienent error logging
  144. capabilities. On NT, errors are logged to the Event Log file. Otherwise,
  145. the errors are placed in a message box for the user.
  146. Arguments:
  147. wSeverity - Supplies the severity of the event. Possible values are:
  148. EVENTLOG_SUCCESS - A success event is to be logged.
  149. EVENTLOG_ERROR_TYPE - An Error event is to be logged.
  150. EVENTLOG_WARNING_TYPE - A Warning event is to be logged.
  151. EVENTLOG_INFORMATION_TYPE - An Informational event is to be logged.
  152. dwMessageId - Message Id from the resource file.
  153. szMessageStr - Message, supplied as a string.
  154. cbBinaryData - Size, in bytes, of any binary data to include with the log.
  155. pvBinaryData - Pointer to binary data to include with the log, or NULL.
  156. rgszParams - An array of pointers to strings to be included as parameters.
  157. The last pointer must be NULL.
  158. szParam<n> - A string parameter to include with the message
  159. dwParam<n> - A DWORD value to include with the message.
  160. Return Value:
  161. None
  162. Throws:
  163. None
  164. Author:
  165. Doug Barlow (dbarlow) 5/9/1997
  166. --*/
  167. #undef __SUBROUTINE__
  168. #define __SUBROUTINE__ DBGT("CalaisMessageLog")
  169. void
  170. CalaisMessageLog(
  171. DEBUG_TEXT szSubroutine,
  172. WORD wSeverity,
  173. DWORD dwMessageId,
  174. LPCTSTR *rgszParams,
  175. LPCVOID pvBinaryData,
  176. DWORD cbBinaryData)
  177. {
  178. LPTSTR szMessage = (LPTSTR)l_szDefaultMessage;
  179. DWORD cchMessage, dwLen;
  180. LCID SaveLCID;
  181. BOOL fSts;
  182. if (0 != (wSeverity & g_wLogSeverity))
  183. {
  184. WORD cszParams = 0;
  185. if (NULL != rgszParams)
  186. {
  187. while (NULL != rgszParams[cszParams])
  188. cszParams += 1;
  189. }
  190. if (EVENTLOG_INFORMATION_TYPE > wSeverity)
  191. {
  192. if (l_fServer && (NULL == l_hEventLogger))
  193. {
  194. l_hEventLogger = RegisterEventSource(
  195. NULL,
  196. CalaisString(CALSTR_PRIMARYSERVICE));
  197. }
  198. if (NULL != l_hEventLogger)
  199. {
  200. fSts = ReportEvent(
  201. l_hEventLogger,
  202. wSeverity,
  203. 0,
  204. dwMessageId,
  205. NULL,
  206. cszParams,
  207. cbBinaryData,
  208. rgszParams,
  209. (LPVOID)pvBinaryData);
  210. }
  211. }
  212. #ifdef DBG
  213. // Don't pass specific lang id to FormatMessage, as it fails if there's
  214. // no msg in that language. Instead, set the thread locale, which will
  215. // get FormatMessage to use a search algorithm to find a message of the
  216. // appropriate language, or use a reasonable fallback msg if there's
  217. // none.
  218. SaveLCID = GetThreadLocale();
  219. SetThreadLocale(LOCALE_SYSTEM_DEFAULT);
  220. cchMessage = FormatMessage(
  221. FORMAT_MESSAGE_ALLOCATE_BUFFER
  222. | FORMAT_MESSAGE_FROM_HMODULE
  223. | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  224. GetModuleHandle(TEXT("winscard.dll")), // NULL on server
  225. dwMessageId,
  226. 0,
  227. (LPTSTR)&szMessage,
  228. 0,
  229. (va_list *)rgszParams);
  230. SetThreadLocale(SaveLCID);
  231. dwLen = lstrlen(szMessage);
  232. if (0 < dwLen)
  233. {
  234. dwLen -= 1;
  235. while (!_istgraph(szMessage[dwLen]))
  236. {
  237. szMessage[dwLen] = 0;
  238. if (0 == dwLen)
  239. break;
  240. dwLen -= 1;
  241. }
  242. }
  243. {
  244. CTextString tzOutMessage;
  245. tzOutMessage = l_szServiceName;
  246. tzOutMessage += TEXT("!");
  247. tzOutMessage += szSubroutine;
  248. if (0 != (EVENTLOG_ERROR_TYPE & wSeverity))
  249. tzOutMessage += CalaisString(CALSTR_ERRORMESSAGETAG);
  250. else if (0 != (EVENTLOG_WARNING_TYPE & wSeverity))
  251. tzOutMessage += CalaisString(CALSTR_WARNINGMESSAGETAG);
  252. else if (0 != (EVENTLOG_INFORMATION_TYPE & wSeverity))
  253. tzOutMessage += CalaisString(CALSTR_INFOMESSAGETAG);
  254. else
  255. tzOutMessage += CalaisString(CALSTR_MESSAGETAG);
  256. if ((0 == cchMessage) || (NULL == szMessage))
  257. tzOutMessage += CErrorString(GetLastError());
  258. else
  259. tzOutMessage += szMessage;
  260. tzOutMessage += TEXT("\n");
  261. #ifdef _DEBUG
  262. _putts(tzOutMessage);
  263. #else
  264. OutputDebugString(tzOutMessage);
  265. #endif
  266. }
  267. if ((g_fGuiWarnings) && (0 != (g_wGuiSeverity & wSeverity)))
  268. {
  269. int nAction;
  270. DWORD dwIcon;
  271. if (0 != (EVENTLOG_ERROR_TYPE & wSeverity))
  272. dwIcon = MB_ICONERROR;
  273. else if (0 != (EVENTLOG_WARNING_TYPE & wSeverity))
  274. dwIcon = MB_ICONWARNING;
  275. else if (0 != (EVENTLOG_INFORMATION_TYPE & wSeverity))
  276. dwIcon = MB_ICONINFORMATION;
  277. else
  278. dwIcon = 0;
  279. if ((0 == cchMessage) || (NULL == szMessage))
  280. {
  281. nAction = MessageBox(
  282. NULL,
  283. CErrorString(GetLastError()),
  284. l_szDefaultMessage,
  285. MB_SYSTEMMODAL
  286. | MB_OKCANCEL
  287. | dwIcon);
  288. }
  289. else
  290. {
  291. nAction = MessageBox(
  292. NULL,
  293. szMessage,
  294. l_szServiceName,
  295. MB_SYSTEMMODAL
  296. | MB_OKCANCEL
  297. | dwIcon);
  298. }
  299. if (IDCANCEL == nAction)
  300. {
  301. breakpoint;
  302. }
  303. }
  304. if ((NULL != szMessage) && (l_szDefaultMessage != szMessage))
  305. LocalFree((LPVOID)szMessage);
  306. #endif
  307. }
  308. }
  309. #ifdef DBG
  310. #undef __SUBROUTINE__
  311. #define __SUBROUTINE__ DBGT("CalaisMessageLog")
  312. void
  313. CalaisMessageLog(
  314. DEBUG_TEXT szSubroutine,
  315. WORD wSeverity,
  316. LPCTSTR szMessageStr,
  317. LPCTSTR *rgszParams,
  318. LPCVOID pvBinaryData,
  319. DWORD cbBinaryData)
  320. {
  321. if (0 != (wSeverity & g_wLogSeverity))
  322. {
  323. LPCTSTR szMessage = l_szDefaultMessage;
  324. DWORD cchMessage;
  325. LPCTSTR szArgs[2];
  326. cchMessage = FormatMessage(
  327. FORMAT_MESSAGE_ALLOCATE_BUFFER
  328. | FORMAT_MESSAGE_FROM_STRING
  329. | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  330. szMessageStr,
  331. 0,
  332. 0,
  333. (LPTSTR)&szMessage,
  334. 0,
  335. (va_list *)rgszParams);
  336. szArgs[0] = szMessage;
  337. szArgs[1] = NULL;
  338. CalaisMessageLog(
  339. szSubroutine,
  340. wSeverity,
  341. 1, // "%1"
  342. szArgs,
  343. pvBinaryData,
  344. cbBinaryData);
  345. if ((NULL != szMessage) && (l_szDefaultMessage != szMessage))
  346. LocalFree((LPVOID)szMessage);
  347. }
  348. }
  349. /*++
  350. CalaisError:
  351. CalaisWarning:
  352. CalaisInfo:
  353. The following routines supply convienent access to the error logging
  354. services, above.
  355. Arguments:
  356. dwMessageId - Supplies a message Id code to use to obtain the message from
  357. the current image's message resource.
  358. szMessage - Supplies the message as a string.
  359. dwErrorCode - Supples an error code to be converted into a string as the
  360. parameter %1.
  361. szParam<n> - Supplies an optional parameter for the message as %<n>.
  362. Return Value:
  363. None
  364. Author:
  365. Doug Barlow (dbarlow) 5/29/1997
  366. --*/
  367. #undef __SUBROUTINE__
  368. #define __SUBROUTINE__ DBGT("CalaisInfo")
  369. void
  370. CalaisInfo(
  371. DEBUG_TEXT szSubroutine,
  372. LPCTSTR szMessage,
  373. LPCTSTR szParam1,
  374. LPCTSTR szParam2,
  375. LPCTSTR szParam3)
  376. {
  377. LPCTSTR rgszParams[4];
  378. rgszParams[0] = szParam1;
  379. rgszParams[1] = szParam2;
  380. rgszParams[2] = szParam3;
  381. rgszParams[3] = NULL;
  382. CalaisMessageLog(
  383. szSubroutine,
  384. EVENTLOG_INFORMATION_TYPE,
  385. szMessage,
  386. rgszParams);
  387. }
  388. void
  389. CalaisInfo(
  390. DEBUG_TEXT szSubroutine,
  391. LPCTSTR szMessage,
  392. DWORD dwErrorCode,
  393. LPCTSTR szParam2,
  394. LPCTSTR szParam3)
  395. {
  396. LPCTSTR rgszParams[4];
  397. CErrorString szErrStr(dwErrorCode);
  398. rgszParams[0] = szErrStr.Value();
  399. rgszParams[1] = szParam2;
  400. rgszParams[2] = szParam3;
  401. rgszParams[3] = NULL;
  402. CalaisMessageLog(
  403. szSubroutine,
  404. EVENTLOG_INFORMATION_TYPE,
  405. szMessage,
  406. rgszParams);
  407. }
  408. #undef __SUBROUTINE__
  409. #define __SUBROUTINE__ DBGT("CalaisWarning")
  410. void
  411. CalaisWarning(
  412. DEBUG_TEXT szSubroutine,
  413. LPCTSTR szMessage,
  414. LPCTSTR szParam1,
  415. LPCTSTR szParam2,
  416. LPCTSTR szParam3)
  417. {
  418. LPCTSTR rgszParams[4];
  419. rgszParams[0] = szParam1;
  420. rgszParams[1] = szParam2;
  421. rgszParams[2] = szParam3;
  422. rgszParams[3] = NULL;
  423. CalaisMessageLog(
  424. szSubroutine,
  425. EVENTLOG_WARNING_TYPE,
  426. szMessage,
  427. rgszParams);
  428. }
  429. void
  430. CalaisWarning(
  431. DEBUG_TEXT szSubroutine,
  432. LPCTSTR szMessage,
  433. DWORD dwErrorCode,
  434. LPCTSTR szParam2,
  435. LPCTSTR szParam3)
  436. {
  437. LPCTSTR rgszParams[4];
  438. CErrorString szErrStr(dwErrorCode);
  439. rgszParams[0] = szErrStr.Value();
  440. rgszParams[1] = szParam2;
  441. rgszParams[2] = szParam3;
  442. rgszParams[3] = NULL;
  443. CalaisMessageLog(
  444. szSubroutine,
  445. EVENTLOG_WARNING_TYPE,
  446. szMessage,
  447. rgszParams);
  448. }
  449. #undef __SUBROUTINE__
  450. #define __SUBROUTINE__ DBGT("CalaisError")
  451. void
  452. CalaisError(
  453. DEBUG_TEXT szSubroutine,
  454. DEBUG_TEXT szMessage,
  455. LPCTSTR szParam1,
  456. LPCTSTR szParam2,
  457. DWORD dwLineNo)
  458. {
  459. LPCTSTR rgszParams[4];
  460. TCHAR szLineNo[32];
  461. _stprintf(szLineNo, TEXT("%d"), dwLineNo);
  462. rgszParams[0] = szParam1;
  463. rgszParams[1] = szParam2;
  464. rgszParams[2] = szLineNo;
  465. rgszParams[3] = NULL;
  466. CalaisMessageLog(
  467. szSubroutine,
  468. EVENTLOG_ERROR_TYPE,
  469. szMessage,
  470. rgszParams);
  471. }
  472. #endif
  473. #undef __SUBROUTINE__
  474. #define __SUBROUTINE__ DBGT("CalaisError")
  475. void
  476. CalaisError(
  477. DEBUG_TEXT szSubroutine,
  478. DWORD dwMessageId,
  479. DWORD dwErrorCode,
  480. LPCTSTR szParam2,
  481. LPCTSTR szParam3)
  482. {
  483. LPCTSTR rgszParams[4];
  484. CErrorString szErrStr(dwErrorCode);
  485. rgszParams[0] = szErrStr.Value();
  486. rgszParams[1] = szParam2;
  487. rgszParams[2] = szParam3;
  488. rgszParams[3] = NULL;
  489. CalaisMessageLog(
  490. szSubroutine,
  491. EVENTLOG_ERROR_TYPE,
  492. dwMessageId,
  493. rgszParams);
  494. }
  495. void
  496. CalaisError(
  497. DEBUG_TEXT szSubroutine,
  498. DWORD dwMessageId,
  499. LPCTSTR szParam1,
  500. LPCTSTR szParam2,
  501. LPCTSTR szParam3)
  502. {
  503. LPCTSTR rgszParams[4];
  504. rgszParams[0] = szParam1;
  505. rgszParams[1] = szParam2;
  506. rgszParams[2] = szParam3;
  507. rgszParams[3] = NULL;
  508. CalaisMessageLog(
  509. szSubroutine,
  510. EVENTLOG_ERROR_TYPE,
  511. dwMessageId,
  512. rgszParams);
  513. }
  514. /*++
  515. CalaisMessageInit:
  516. This routine prepares the error logging system.
  517. Arguments:
  518. szTitle supplies the title of the module for logging purposes.
  519. hEventLogger supplies a handle to an event logging service. This parameter
  520. may be NULL.
  521. fServer supplies an indicator as to whether or not this process is a
  522. service which should try really hard to log errors.
  523. Return Value:
  524. None
  525. Throws:
  526. None
  527. Author:
  528. Doug Barlow (dbarlow) 5/29/1997
  529. --*/
  530. #undef __SUBROUTINE__
  531. #define __SUBROUTINE__ DBGT("CalaisMessageInit")
  532. void
  533. CalaisMessageInit(
  534. LPCTSTR szTitle,
  535. HANDLE hEventLogger,
  536. BOOL fServer)
  537. {
  538. ASSERT(NULL == l_hEventLogger);
  539. l_szServiceName = szTitle;
  540. l_hEventLogger = hEventLogger;
  541. l_fServer = fServer;
  542. #ifdef DBG
  543. try
  544. {
  545. DWORD dwValue;
  546. CRegistry regSc(
  547. HKEY_LOCAL_MACHINE,
  548. CalaisString(CALSTR_CALAISREGISTRYKEY),
  549. KEY_READ);
  550. CRegistry regDebug(
  551. regSc,
  552. CalaisString(CALSTR_DEBUGREGISTRYSUBKEY),
  553. KEY_READ);
  554. regDebug.GetValue(CalaisString(CALSTR_DEBUGLOGSUBKEY), &dwValue);
  555. g_fDebug = (0 != dwValue);
  556. regDebug.GetValue(CalaisString(CALSTR_LOGSEVERITYSUBKEY), &dwValue);
  557. g_wLogSeverity = (WORD)dwValue;
  558. regDebug.GetValue(CalaisString(CALSTR_GUIWARNINGSUBKEY), &dwValue);
  559. g_fGuiWarnings = (0 != dwValue);
  560. regDebug.GetValue(CalaisString(CALSTR_GUISEVERITYSUBKEY), &dwValue);
  561. g_wGuiSeverity = (WORD)dwValue;
  562. }
  563. catch (...) {}
  564. #endif
  565. }
  566. /*++
  567. CalaisMessageClose:
  568. This routine closes out any error loging in progress, and cleans up.
  569. Arguments:
  570. None
  571. Return Value:
  572. None
  573. Throws:
  574. None
  575. Author:
  576. Doug Barlow (dbarlow) 5/29/1997
  577. --*/
  578. #undef __SUBROUTINE__
  579. #define __SUBROUTINE__ DBGT("CalaisMessageClose")
  580. void
  581. CalaisMessageClose(
  582. void)
  583. {
  584. if (NULL != l_hEventLogger)
  585. DeregisterEventSource(l_hEventLogger);
  586. l_hEventLogger = NULL;
  587. l_szServiceName = NULL;
  588. }
  589. /*++
  590. CalaisString:
  591. This routine converts a string identifier into a string.
  592. Arguments:
  593. dwStringId supplies the identifier for the string.
  594. Return Value:
  595. The target string value.
  596. Remarks:
  597. String Ids larger than CALSTR_RESOURCELIMIT are assumed to be resources.
  598. Author:
  599. Doug Barlow (dbarlow) 4/8/1999
  600. --*/
  601. #undef __SUBROUTINE__
  602. #define __SUBROUTINE__ DBGT("CalaisString")
  603. LPCTSTR
  604. CalaisString(
  605. DWORD dwStringId)
  606. {
  607. static LPTSTR rgszResources[]
  608. = { NULL, NULL, NULL, NULL, NULL, NULL }; // 6 is all we have for now...
  609. LPCTSTR szReturn;
  610. int nStrLen;
  611. DWORD dwResId = (dwStringId % CALSTR_RESOURCELIMIT) - 1;
  612. if (CALSTR_RESOURCELIMIT > dwStringId)
  613. {
  614. //
  615. // This is a straight internal text string.
  616. //
  617. szReturn = g_rgszDefaultStrings[(dwStringId) - 1];
  618. }
  619. else if (dwResId > (sizeof(rgszResources) / sizeof(LPCTSTR)))
  620. {
  621. //
  622. // Make sure the request isn't out of our range.
  623. //
  624. ASSERT(FALSE); // Make that 6 bigger.
  625. szReturn = TEXT("<Resource out of Range>");
  626. }
  627. else if (NULL != rgszResources[dwResId])
  628. {
  629. //
  630. // Have we already loaded that resource? If so, return
  631. // it from the cache.
  632. //
  633. szReturn = rgszResources[dwResId];
  634. }
  635. else
  636. {
  637. TCHAR szString[MAX_PATH];
  638. //
  639. // OK, we've got to load the resource into the cache.
  640. //
  641. nStrLen = LoadString(
  642. NULL,
  643. dwStringId - CALSTR_RESOURCELIMIT,
  644. szString,
  645. sizeof(szString));
  646. if (0 < nStrLen)
  647. {
  648. rgszResources[dwResId]
  649. = (LPTSTR)HeapAlloc(
  650. GetProcessHeap(),
  651. 0,
  652. (nStrLen + 1) * sizeof(TCHAR));
  653. if (NULL != rgszResources[dwResId])
  654. {
  655. lstrcpy(rgszResources[dwResId], szString);
  656. szReturn = rgszResources[dwResId];
  657. }
  658. else
  659. szReturn = TEXT("<Resource Load Error>");
  660. }
  661. else
  662. szReturn = TEXT("<Unavailable Resource>");
  663. }
  664. return szReturn;
  665. }
  666. //
  667. //==============================================================================
  668. //
  669. // Hard core debugging routines.
  670. //
  671. #ifdef DBG
  672. #undef __SUBROUTINE__
  673. #define __SUBROUTINE__ DBGT("CalaisSetDebug")
  674. void
  675. CalaisSetDebug(
  676. BOOLEAN Debug
  677. )
  678. {
  679. g_fDebug = Debug;
  680. }
  681. #undef __SUBROUTINE__
  682. #define __SUBROUTINE__ DBGT("_CalaisDebug")
  683. void
  684. _CalaisDebug(
  685. LPCTSTR szFormat,
  686. ...
  687. )
  688. {
  689. TCHAR szBuffer[512];
  690. va_list ap;
  691. if (g_fDebug == FALSE) {
  692. return;
  693. }
  694. va_start(ap, szFormat);
  695. _vstprintf(szBuffer, szFormat, ap);
  696. #ifdef _DEBUG
  697. _putts(szBuffer);
  698. #else
  699. OutputDebugString(szBuffer);
  700. #endif
  701. }
  702. #endif