1 /* cygwin_dll.h 2 3 Copyright 1998, 1999, 2000 Cygnus Solutions 4 5 This file is part of Cygwin. 6 7 This software is a copyrighted work licensed under the terms of the 8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for 9 details. */ 10 11 #ifndef __CYGWIN_CYGWIN_DLL_H__ 12 #define __CYGWIN_CYGWIN_DLL_H__ 13 14 #include <windows.h> 15 16 #ifdef __cplusplus 17 #define CDECL_BEGIN extern "C" { 18 #define CDECL_END } 19 #else 20 #define CDECL_BEGIN 21 #define CDECL_END 22 #endif 23 24 #define DECLARE_CYGWIN_DLL(Entry) \ 25 \ 26 CDECL_BEGIN \ 27 int WINAPI Entry (HINSTANCE h, DWORD reason, void *ptr); \ 28 typedef int (*mainfunc) (int, char **, char **); \ 29 extern int cygwin_attach_dll (HMODULE, mainfunc); \ 30 extern void cygwin_detach_dll (DWORD); \ 31 CDECL_END \ 32 \ 33 static HINSTANCE storedHandle; \ 34 static DWORD storedReason; \ 35 static void* storedPtr; \ 36 \ 37 static int __dllMain (int a, char **b, char **c) \ 38 { \ 39 return Entry (storedHandle, storedReason, storedPtr); \ 40 } \ 41 \ 42 static DWORD dll_index; \ 43 \ 44 int WINAPI _cygwin_dll_entry (HINSTANCE h, DWORD reason, void *ptr) \ 45 { \ 46 int ret; \ 47 ret = 1; \ 48 \ 49 switch (reason) \ 50 { \ 51 case DLL_PROCESS_ATTACH: \ 52 { \ 53 storedHandle = h; \ 54 storedReason = reason; \ 55 storedPtr = ptr; \ 56 dll_index = cygwin_attach_dll (h, &__dllMain); \ 57 if (dll_index == (DWORD) -1) \ 58 ret = 0; \ 59 } \ 60 break; \ 61 \ 62 case DLL_PROCESS_DETACH: \ 63 { \ 64 ret = Entry (h, reason, ptr); \ 65 if (ret) \ 66 { \ 67 cygwin_detach_dll (dll_index); \ 68 dll_index = (DWORD) -1; \ 69 } \ 70 } \ 71 break; \ 72 \ 73 case DLL_THREAD_ATTACH: \ 74 { \ 75 ret = Entry (h, reason, ptr); \ 76 } \ 77 break; \ 78 \ 79 case DLL_THREAD_DETACH: \ 80 { \ 81 ret = Entry (h, reason, ptr); \ 82 } \ 83 break; \ 84 } \ 85 return ret; \ 86 } \ 87 \ 88 /* OBSOLETE: This is only provided for source level compatibility. */ \ 89 int WINAPI _cygwin_noncygwin_dll_entry (HINSTANCE h, DWORD reason, void *ptr) \ 90 { \ 91 return _cygwin_dll_entry (h, reason, ptr); \ 92 } \ 93 94 #endif /* __CYGWIN_CYGWIN_DLL_H__ */ |