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.

150 lines
3.6 KiB

  1. /*****************************************************************************\
  2. * MODULE: dllini.cxx
  3. *
  4. * Dll entry/exit routines.
  5. *
  6. *
  7. * Copyright (C) 1997-1998 Hewlett-Packard Company.
  8. * Copyright (C) 1997-1998 Microsoft Corporation.
  9. *
  10. * History:
  11. * 10-Oct-1997 GFS Created
  12. * 22-Jun-1998 CHW Cleaned
  13. *
  14. \*****************************************************************************/
  15. #include "libpriv.h"
  16. /*********************************************************** local routine ***\
  17. * dll_ThunkError
  18. *
  19. *
  20. \*****************************************************************************/
  21. BOOL dll_ThunkError(VOID)
  22. {
  23. LPTSTR pszMsg;
  24. int nFmt;
  25. pszMsg = NULL;
  26. nFmt = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  27. NULL,
  28. GetLastError(),
  29. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  30. (LPTSTR)&pszMsg,
  31. 0,
  32. NULL);
  33. if (pszMsg && (nFmt > 0)) {
  34. // Display the string.
  35. //
  36. MessageBox(NULL, pszMsg, g_szMsgThunkFail, MB_OK | MB_ICONINFORMATION);
  37. // Free the buffer.
  38. //
  39. GlobalFree(pszMsg);
  40. }
  41. return FALSE;
  42. }
  43. /*********************************************************** local routine ***\
  44. * dll_ProcessAttach
  45. *
  46. * Called when the process attaches to the dynalink. This is the place
  47. * that is good to allocate global resouces.
  48. *
  49. \*****************************************************************************/
  50. _inline BOOL dll_ProcessAttach(
  51. BOOL bThk,
  52. HINSTANCE hModule)
  53. {
  54. BOOL bRet;
  55. // Set the global hInstance.
  56. //
  57. g_hLibInst = hModule;
  58. bRet = InitStrings();
  59. if (bRet && (bThk == FALSE))
  60. bRet = dll_ThunkError();
  61. return bRet;
  62. }
  63. /*********************************************************** local routine ***\
  64. * dll_ProcessDetach
  65. *
  66. * Called when a process detaches from the DLL. This is only called once,
  67. * so all resources allocated on behalf of the dynalink are freed.
  68. *
  69. \*****************************************************************************/
  70. _inline BOOL dll_ProcessDetach(
  71. BOOL bThk,
  72. HINSTANCE hModule)
  73. {
  74. if (bThk == FALSE)
  75. dll_ThunkError();
  76. FreeStrings();
  77. return TRUE;
  78. }
  79. /****************************************************** entry/exit routine ***\
  80. * DllMain
  81. *
  82. * This routine is called upon startup of the dynalink. If all goes well
  83. * then return TRUE. By returning FALSE you prevent the dynalink from
  84. * loading.
  85. *
  86. * The parameters to this function double under DOS and NT. The meanings are
  87. * slightly different however.
  88. *
  89. * Parameter DOS NT
  90. * -------- ----------------- -------------
  91. * hHandle Instance handle Module handle (same as instance)
  92. * nAttach Dynalink Data*Seg Attach type.
  93. * pContext Command Line args pointer to context structur.
  94. *
  95. *
  96. \*****************************************************************************/
  97. BOOL APIENTRY DllMain(
  98. HINSTANCE hModule,
  99. int nAttach,
  100. PCONTEXT pContext)
  101. {
  102. BOOL bRet;
  103. BOOL bThk;
  104. UNREFPARM(pContext);
  105. // Connect the thunk.
  106. //
  107. bThk = thk_ThunkConnect32(g_szDll16, g_szDll32, hModule, nAttach);
  108. switch (nAttach) {
  109. case DLL_PROCESS_ATTACH:
  110. bRet = dll_ProcessAttach(bThk, hModule);
  111. break;
  112. case DLL_PROCESS_DETACH:
  113. bRet = dll_ProcessDetach(bThk, hModule);
  114. break;
  115. case DLL_THREAD_ATTACH:
  116. case DLL_THREAD_DETACH:
  117. bRet = TRUE;
  118. break;
  119. }
  120. return bRet;
  121. }