Dllmain.c
上传用户:chzmdj
上传日期:2007-01-22
资源大小:135k
文件大小:5k
源码类别:

源码/资料

开发平台:

C/C++

  1. #include <windows.h>
  2. #include "NHTW32.H"
  3. #include "import.h"
  4. #include "getver.h"
  5. #include "HookApi.h"
  6. //#define MUTEXNAME "NOBLE HAND"
  7. #define DLL32NAME "NHW32.DLL"
  8. #define DLL16NAME "NHW16.DLL"
  9. extern APIHOOKSTRUCT g_ExtTextOutWHook;
  10. extern HANDLE hMutex;
  11. //*********************************************************************
  12. //Added by XGL, Jan 7th, 1999
  13. //for get word in Acrobat Reader
  14. HANDLE g_heventGetWord = NULL ; //event handle
  15. HANDLE g_hMappedFile = NULL ; //handle of file-mapping object
  16. void *g_pMemFile = NULL ; //pointer to memfile
  17. //flag indicates if the global variables
  18. //for getting word in Acrobat Reader have
  19. //been initialized.
  20. BOOL g_bAcroReaderInit = FALSE ;
  21. //*********************************************************************
  22. //function that initializes the global variables
  23. //for getting word in Acrobat Reader.
  24. //*********************************************************************
  25. BOOL InitForAcrobatR()
  26. {
  27. BOOL bMappingExisted = FALSE ;
  28. //just initialize once
  29. if (g_bAcroReaderInit)
  30. {
  31. return TRUE ;
  32. }
  33. //open existed mapping-file
  34. g_hMappedFile = OpenFileMapping(FILE_MAP_ALL_ACCESS,
  35. FALSE, MEM_FILE_NAME) ;
  36. if (NULL == g_hMappedFile)
  37. {
  38. //create a new mapping-file
  39. g_hMappedFile = CreateFileMapping((HANDLE)0xffffffff,
  40. NULL, PAGE_READWRITE, 0, MEM_FILE_SIZE, MEM_FILE_NAME) ;
  41. if (NULL == g_hMappedFile)
  42. {
  43. return FALSE ;
  44. }
  45. }
  46. else
  47. {
  48. bMappingExisted = TRUE ;
  49. }
  50. //map the file into our address
  51. g_pMemFile = MapViewOfFile(g_hMappedFile, FILE_MAP_WRITE,
  52. 0, 0, 0) ;
  53. if (NULL == g_pMemFile)
  54. {
  55. CloseHandle(g_hMappedFile) ;
  56. g_hMappedFile = NULL ;
  57. return FALSE ;
  58. }
  59. if (!bMappingExisted)
  60. {
  61. //set plug-in unexisted flag
  62. *((BYTE*)(g_pMemFile) + PLUGIN_FLAG_POS) = NOT_EXISTED ;
  63. }
  64. //set mainproc existed flag 
  65. *((BYTE*)(g_pMemFile) + MAINPROC_FLAG_POS) = EXISTED ;
  66. //open getword event
  67. g_heventGetWord = OpenEvent(EVENT_ALL_ACCESS,
  68. FALSE, GET_WORD_EVENT) ;
  69. if (NULL == g_heventGetWord)
  70. {
  71. //if it does not exist, create it
  72. g_heventGetWord = CreateEvent(NULL, TRUE, TRUE, GET_WORD_EVENT) ;
  73. if (NULL == g_heventGetWord)
  74. {
  75. if (NULL != g_hMappedFile)
  76. {
  77. CloseHandle(g_hMappedFile) ;
  78. g_hMappedFile = NULL ;
  79. }
  80. if (NULL != g_pMemFile)
  81. {
  82. UnmapViewOfFile(g_pMemFile) ;
  83. g_pMemFile = NULL ;
  84. }
  85. return FALSE;
  86. }
  87. }
  88. g_bAcroReaderInit = TRUE ;
  89. return TRUE ;
  90. }
  91. //*********************************************************************
  92. //function that uninitializes the global variables
  93. //for getting word in Acrobat Reader.
  94. //*********************************************************************
  95. void UninitForAcrobatR()
  96. {
  97. if (g_bAcroReaderInit)
  98. {
  99. //set exit flag
  100. *((BYTE*)(g_pMemFile) + MAINPROC_FLAG_POS) = NOT_EXISTED ;
  101. //close handles
  102. if (NULL != g_heventGetWord)
  103. {
  104. CloseHandle(g_heventGetWord) ;
  105. g_heventGetWord = NULL ;
  106. }
  107. if (NULL != g_hMappedFile)
  108. {
  109. CloseHandle(g_hMappedFile) ;
  110. g_hMappedFile = NULL ;
  111. }
  112. if (NULL != g_pMemFile)
  113. {
  114. UnmapViewOfFile(g_pMemFile) ;
  115. g_pMemFile = NULL ;
  116. }
  117. }
  118. g_bAcroReaderInit = FALSE ;
  119. }
  120. //end of addition, XGL, Jan 7th, 1999
  121. //*********************************************************************
  122. BOOL _stdcall thk_ThunkConnect32(LPSTR      pszDll16,
  123.                                  LPSTR      pszDll32,
  124.                                  HINSTANCE  hInst,
  125.                                  DWORD      dwReason);
  126. BOOL WINAPI DllMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
  127. {
  128. g_ExtTextOutWHook.hInst = hDLLInst;
  129. switch (fdwReason)
  130. {
  131. case DLL_PROCESS_ATTACH:
  132. {
  133. if (!(thk_ThunkConnect32(DLL16NAME,
  134.  DLL32NAME,
  135.  hDLLInst,
  136.  fdwReason)))
  137. {
  138. return FALSE;
  139. }
  140. // DbgPrintf("NhW32.dll:          DLL_PROCESS_ATTACH");
  141. if (IsDesiredFileVersion("GDI.EXE", CHINESE_TAIWAN))
  142. {
  143. BL_SetVer16(CHINESE_TAIWAN);
  144. }
  145. if (IsDesiredFileVersion("GDI.EXE", CHINESE_PRC))
  146. {
  147. BL_SetVer16(CHINESE_PRC);
  148. }
  149. hMutex = CreateMutex(NULL, FALSE, MUTEXNAME);
  150. if (hMutex == NULL)
  151. {
  152. return FALSE;
  153. }
  154. //added by XGL, Jan 7th, 1999
  155. if (!InitForAcrobatR())
  156. {
  157. return FALSE ;
  158. }
  159. }
  160. //            break;
  161. case DLL_THREAD_ATTACH:
  162. break;
  163. case DLL_PROCESS_DETACH:
  164. RestoreWin32Api(&g_ExtTextOutWHook, HOOK_ONLY_READ);
  165. if (!(thk_ThunkConnect32(DLL16NAME,
  166.  DLL32NAME,
  167.  hDLLInst,
  168.  fdwReason)))
  169. {
  170. return FALSE;
  171. }
  172. CloseHandle(hMutex);
  173. //added by XGL, Jan 7th, 1999
  174. UninitForAcrobatR() ;
  175. break;
  176. case DLL_THREAD_DETACH:
  177. if (!(thk_ThunkConnect32(DLL16NAME,
  178.  DLL32NAME,
  179.  hDLLInst,
  180.  fdwReason)))
  181. {
  182. return FALSE;
  183. }
  184. break;
  185. }
  186. return TRUE;
  187. }