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.

156 lines
4.6 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. #ifdef ISOLATION_AWARE_ENABLED
  73. SHFusionInitializeFromModuleID (m_hInstance, 2);
  74. #endif
  75. try
  76. {
  77. // Initialize OLE module for regular DLL
  78. AfxOleInitModule();
  79. // Each public CSP interface defined in CSP_API.cpp (by
  80. // definition of the Microsoft CryptoAPI) uses SetLastError
  81. // upon returning for the calling application to determine the
  82. // specifics on any failure. Unfortunately, the CSP uses MFC
  83. // which also calls SetLastError as part of its run-down
  84. // procedures when the CSP DLL is being unloaded (at least on
  85. // NT 4/Windows 95, didn't observe this on NT 5). Normally,
  86. // one might want the CSP DLL to be unloaded when there aren't
  87. // any CSP resources being used by the application, (e.g. no
  88. // cards in the readers or no contexts acquired, etc.)
  89. // However, if the CSP DLL is unloaded after returning to the
  90. // application, then these MFC run-down procedures will stomp
  91. // on the result of the SetLastError call made by CSP before
  92. // returning to the calling application from advapi32.dll.
  93. // When the calling application finally gets control, the
  94. // result of the CSPs call to SetLastError is long gone. To
  95. // avoid this, the CSP DLL is "locked" into memory during DLL
  96. // initialization until the application exits at which point
  97. // the system forces the DLL to unload.
  98. LockDLLIntoMemory();
  99. CWinApp::InitInstance();
  100. SetupMasterLock();
  101. // Initialize the CSP's world
  102. CspProfile::Instance();
  103. }
  104. catch (...)
  105. {
  106. fSuccess = FALSE;
  107. }
  108. return fSuccess;
  109. }
  110. int CSLBDllApp::ExitInstance()
  111. {
  112. OnIdle(1);
  113. CspProfile::Release();
  114. DestroyMasterLock();
  115. #ifdef ISOLATION_AWARE_ENABLED
  116. SHFusionUninitialize();
  117. #endif
  118. // UnloadServer();
  119. return CWinApp::ExitInstance();
  120. }