first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / application / dll_root.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : DLL Main Root Support                                             *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 1995-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 // Thanks to Andy Tan for some research into MFC extension dlls.
16
17 #include <application/base_application.h>
18 #include <basis/utf_conversion.h>
19 #include <structures/static_memory_gremlin.h>
20
21 //HOOPLE_STARTUP_CODE_DLL;
22   // initialize objects needed by the hoople libs.
23
24 #ifdef _AFXDLL
25
26 #ifdef DEBUG
27   #define TRACE_PRINTER(s) TRACE_PRINT(s)
28 #else
29   #define TRACE_PRINTER(s) 
30 #endif
31
32 // base for AFX dlls.
33
34 #include <afxext.h>  // MFC extensions.
35 #include <afxdllx.h>  // Extension DLL declarations; include only once!
36
37 bool check_DLL_versions();
38   // supplied by the library's version of dllmain.cpp.
39
40 static AFX_EXTENSION_MODULE SomeDLL = { NULL, NULL };
41
42 #ifndef DLL_NAME
43   #define DLL_NAME "unnamed DLL"
44 #endif
45
46 extern "C" int APIENTRY
47 DllMain(application_instance instance, DWORD reason, LPVOID reserved)
48 {
49   SET_INSTANCE_HANDLE(instance);
50   // Remove this if you use lpReserved.
51   UNREFERENCED_PARAMETER(reserved);
52
53   char *dll_name = DLL_NAME;
54     // mainly for debugging purposes.  having the value for DLL_NAME actually
55     // stored should allow us to know which dll is being debugged.
56
57   static CDynLinkLibrary *dll_link = NIL;
58
59   static int dll_entry_count = 0;
60
61   switch (reason) {
62     case DLL_PROCESS_ATTACH: {
63       char *message = DLL_NAME " Initializing!\n";
64       TRACE_PRINTER(message);
65
66       if (!check_DLL_versions()) return 0;
67     
68       // Extension DLL one-time initialization
69       if (!AfxInitExtensionModule(SomeDLL, instance)) return 0;
70
71       // Insert this DLL into the resource chain.
72       dll_link = new CDynLinkLibrary(SomeDLL);
73
74       // NOTE: If this Extension DLL is being implicitly linked to by an MFC
75       // Regular DLL (such as an ActiveX Control) instead of an MFC
76       // application, then you will want to remove this line from DllMain and
77       // put it in a separate function exported from this Extension DLL.  The
78       // Regular DLL that uses this Extension DLL should then explicitly call
79       // that function to initialize this Extension DLL.  Otherwise, the
80       // CDynLinkLibrary object will not be attached to the Regular DLL's
81       // resource chain, and serious problems will result.
82       ++dll_entry_count;
83       break;
84     }
85     case DLL_PROCESS_DETACH: {
86       --dll_entry_count;
87       char *message = DLL_NAME " Terminating!\n";
88       TRACE_PRINTER(message);
89       // clean up our other stuff.
90       WHACK(dll_link);
91       // Terminate the library before destructors are called.
92       AfxTermExtensionModule(SomeDLL);
93       break;
94     }
95     case DLL_THREAD_ATTACH:
96         ++dll_entry_count;
97         break;
98     case DLL_THREAD_DETACH:
99         --dll_entry_count;
100         break;
101     default:
102 // do nothing.
103       break;
104   }
105
106   return 1;
107 }
108
109 #elif defined(__WIN32__)
110
111 // regular dll base.
112
113 #include <application/windoze_helper.h>  // base windows stuff.
114
115 bool check_DLL_versions();
116   // supplied by the library's version of dllmain.cpp.
117
118 BOOL APIENTRY DllMain(HANDLE module, DWORD ul_reason_for_call, LPVOID reserved)
119 {
120   SET_INSTANCE_HANDLE((application_instance)module);
121   switch (ul_reason_for_call) {
122     case DLL_PROCESS_ATTACH:
123       if (!check_DLL_versions()) return 0;
124       break;
125     
126     // these are currently not processed.
127     case DLL_THREAD_ATTACH:
128     case DLL_THREAD_DETACH:
129     case DLL_PROCESS_DETACH:
130       break;
131   }
132   return true;
133 }
134
135 #endif
136