dll_root.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : DLL Main Root Support                                             *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 1995-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 // Thanks to Andy Tan for some research into MFC extension dlls.
00016 
00017 #include <application/base_application.h>
00018 #include <basis/utf_conversion.h>
00019 #include <structures/static_memory_gremlin.h>
00020 
00021 //HOOPLE_STARTUP_CODE_DLL;
00022   // initialize objects needed by the hoople libs.
00023 
00024 #ifdef _AFXDLL
00025 
00026 #ifdef DEBUG
00027   #define TRACE_PRINTER(s) TRACE_PRINT(s)
00028 #else
00029   #define TRACE_PRINTER(s) 
00030 #endif
00031 
00032 // base for AFX dlls.
00033 
00034 #include <afxext.h>  // MFC extensions.
00035 #include <afxdllx.h>  // Extension DLL declarations; include only once!
00036 
00037 bool check_DLL_versions();
00038   // supplied by the library's version of dllmain.cpp.
00039 
00040 static AFX_EXTENSION_MODULE SomeDLL = { NULL, NULL };
00041 
00042 #ifndef DLL_NAME
00043   #define DLL_NAME "unnamed DLL"
00044 #endif
00045 
00046 extern "C" int APIENTRY
00047 DllMain(application_instance instance, DWORD reason, LPVOID reserved)
00048 {
00049   SET_INSTANCE_HANDLE(instance);
00050   // Remove this if you use lpReserved.
00051   UNREFERENCED_PARAMETER(reserved);
00052 
00053   char *dll_name = DLL_NAME;
00054     // mainly for debugging purposes.  having the value for DLL_NAME actually
00055     // stored should allow us to know which dll is being debugged.
00056 
00057   static CDynLinkLibrary *dll_link = NIL;
00058 
00059   static int dll_entry_count = 0;
00060 
00061   switch (reason) {
00062     case DLL_PROCESS_ATTACH: {
00063       char *message = DLL_NAME " Initializing!\n";
00064       TRACE_PRINTER(message);
00065 
00066       if (!check_DLL_versions()) return 0;
00067     
00068       // Extension DLL one-time initialization
00069       if (!AfxInitExtensionModule(SomeDLL, instance)) return 0;
00070 
00071       // Insert this DLL into the resource chain.
00072       dll_link = new CDynLinkLibrary(SomeDLL);
00073 
00074       // NOTE: If this Extension DLL is being implicitly linked to by an MFC
00075       // Regular DLL (such as an ActiveX Control) instead of an MFC
00076       // application, then you will want to remove this line from DllMain and
00077       // put it in a separate function exported from this Extension DLL.  The
00078       // Regular DLL that uses this Extension DLL should then explicitly call
00079       // that function to initialize this Extension DLL.  Otherwise, the
00080       // CDynLinkLibrary object will not be attached to the Regular DLL's
00081       // resource chain, and serious problems will result.
00082       ++dll_entry_count;
00083       break;
00084     }
00085     case DLL_PROCESS_DETACH: {
00086       --dll_entry_count;
00087       char *message = DLL_NAME " Terminating!\n";
00088       TRACE_PRINTER(message);
00089       // clean up our other stuff.
00090       WHACK(dll_link);
00091       // Terminate the library before destructors are called.
00092       AfxTermExtensionModule(SomeDLL);
00093       break;
00094     }
00095     case DLL_THREAD_ATTACH:
00096         ++dll_entry_count;
00097         break;
00098     case DLL_THREAD_DETACH:
00099         --dll_entry_count;
00100         break;
00101     default:
00102 // do nothing.
00103       break;
00104   }
00105 
00106   return 1;
00107 }
00108 
00109 #elif defined(__WIN32__)
00110 
00111 // regular dll base.
00112 
00113 #include <application/windoze_helper.h>  // base windows stuff.
00114 
00115 bool check_DLL_versions();
00116   // supplied by the library's version of dllmain.cpp.
00117 
00118 BOOL APIENTRY DllMain(HANDLE module, DWORD ul_reason_for_call, LPVOID reserved)
00119 {
00120   SET_INSTANCE_HANDLE((application_instance)module);
00121   switch (ul_reason_for_call) {
00122     case DLL_PROCESS_ATTACH:
00123       if (!check_DLL_versions()) return 0;
00124       break;
00125     
00126     // these are currently not processed.
00127     case DLL_THREAD_ATTACH:
00128     case DLL_THREAD_DETACH:
00129     case DLL_PROCESS_DETACH:
00130       break;
00131   }
00132   return true;
00133 }
00134 
00135 #endif
00136 
Generated on Sat Jan 28 04:22:07 2012 for hoople2 project by  doxygen 1.6.3