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.

148 lines
4.3 KiB

  1. // slbCsp.cpp : Defines the initialization routines for the
  2. // Schlumberger CSP DLL.
  3. //
  4. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  5. // 1999. This computer program includes Confidential, Proprietary
  6. // Information and is a Trade Secret of Schlumberger Technology Corp. All
  7. // use, disclosure, and/or reproduction is prohibited unless authorized
  8. // in writing. All Rights Reserved.
  9. #include "stdafx.h"
  10. #include <scuOsExc.h>
  11. #include "MasterLock.h"
  12. #include "CspProfile.h"
  13. #include "slbCsp.h"
  14. // #include "initsvr.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. using namespace ProviderProfile;
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CSLBDllApp
  23. BEGIN_MESSAGE_MAP(CSLBDllApp, CWinApp)
  24. //{{AFX_MSG_MAP(CSLBDllApp)
  25. // NOTE - the ClassWizard will add and remove mapping macros here.
  26. // DO NOT EDIT what you see in these blocks of generated code!
  27. //}}AFX_MSG_MAP
  28. END_MESSAGE_MAP()
  29. // To ensure the value set by SetLastError isn't reset when the DLL is
  30. // unloaded, the DLL must be "locked" into memory until the calling
  31. // application exits. This is accomplished by bumping the reference
  32. // count of this DLL using LoadLibrary without a corresponding
  33. // FreeLibrary call. When the application exits, the system will
  34. // unload the DLL even though the reference count hasn't gone to zero.
  35. namespace
  36. {
  37. static void
  38. LockDLLIntoMemory()
  39. {
  40. static bool bLocked = false;
  41. if (!bLocked)
  42. {
  43. HINSTANCE hThisDll = AfxGetInstanceHandle();
  44. if (NULL == hThisDll)
  45. throw scu::OsException(GetLastError());
  46. TCHAR szModulePath[MAX_PATH];
  47. DWORD cLength = GetModuleFileName(hThisDll, szModulePath,
  48. (sizeof szModulePath /
  49. sizeof *szModulePath));
  50. if (0 == cLength)
  51. throw scu::OsException(GetLastError());
  52. szModulePath[cLength] = '\0';
  53. if (!LoadLibrary(szModulePath))
  54. throw scu::OsException(GetLastError());
  55. bLocked = true;
  56. }
  57. }
  58. } // namespace
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CSLBDllApp construction
  61. CSLBDllApp::CSLBDllApp()
  62. {
  63. // TODO: add construction code here,
  64. // Place all significant initialization in InitInstance
  65. }
  66. /////////////////////////////////////////////////////////////////////////////
  67. // The one and only CSLBDllApp object
  68. CSLBDllApp theApp;
  69. BOOL CSLBDllApp::InitInstance()
  70. {
  71. BOOL fSuccess = TRUE;
  72. try
  73. {
  74. // Initialize OLE module for regular DLL
  75. AfxOleInitModule();
  76. // Each public CSP interface defined in CSP_API.cpp (by
  77. // definition of the Microsoft CryptoAPI) uses SetLastError
  78. // upon returning for the calling application to determine the
  79. // specifics on any failure. Unfortunately, the CSP uses MFC
  80. // which also calls SetLastError as part of its run-down
  81. // procedures when the CSP DLL is being unloaded (at least on
  82. // NT 4/Windows 95, didn't observe this on NT 5). Normally,
  83. // one might want the CSP DLL to be unloaded when there aren't
  84. // any CSP resources being used by the application, (e.g. no
  85. // cards in the readers or no contexts acquired, etc.)
  86. // However, if the CSP DLL is unloaded after returning to the
  87. // application, then these MFC run-down procedures will stomp
  88. // on the result of the SetLastError call made by CSP before
  89. // returning to the calling application from advapi32.dll.
  90. // When the calling application finally gets control, the
  91. // result of the CSPs call to SetLastError is long gone. To
  92. // avoid this, the CSP DLL is "locked" into memory during DLL
  93. // initialization until the application exits at which point
  94. // the system forces the DLL to unload.
  95. LockDLLIntoMemory();
  96. CWinApp::InitInstance();
  97. SetupMasterLock();
  98. // Initialize the CSP's world
  99. CspProfile::Instance();
  100. }
  101. catch (...)
  102. {
  103. fSuccess = FALSE;
  104. }
  105. return fSuccess;
  106. }
  107. int CSLBDllApp::ExitInstance()
  108. {
  109. OnIdle(1);
  110. CspProfile::Release();
  111. DestroyMasterLock();
  112. // UnloadServer();
  113. return CWinApp::ExitInstance();
  114. }