// MyDLL.cpp : Defines the entry point for the DLL application. // #include "stdafx.h" #include "MyDLL.h" #ifdef _MANAGED //if this is using managed code... #pragma managed(push, off) #endif // see http://msdn2.microsoft.com/en-us/library/ms682583.aspx for a description // of the details of how DllMain works BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { //lpvReserved //If fdwReason is DLL_PROCESS_ATTACH, lpvReserved is NULL for dynamic loads //and non-NULL for static loads. //If fdwReason is DLL_PROCESS_DETACH, lpvReserved is NULL if FreeLibrary has //been called or the DLL load failed and non-NULL if the process is terminating. switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: // = 1 //ul_reason_for_call is this when the DLL is being loaded into the virtual address space of //the current process as a result of the process starting up, or as a result of a call to //the LoadLibrary API function. DLLs can use this opportunity to initialize any instance data or //to use the TlsAlloc function to allocate a thread local storage (TLS) index. case DLL_THREAD_ATTACH: // = 2 //The current process is creating a new thread. When this occurs, the system calls // the entry-point function of all DLLs currently attached to the process. The call //is made in the context of the new thread. DLLs can use this opportunity to initialize //a TLS slot for the thread. A thread calling the DLL entry-point function with DLL_PROCESS_ATTACH //does not call the DLL entry-point function with DLL_THREAD_ATTACH. //Note that a DLL's entry-point function is called with this value only by threads created //after the DLL is loaded by the process. When a DLL is loaded using LoadLibrary, existing //threads do not call the entry-point function of the newly loaded DLL. case DLL_THREAD_DETACH: // = 3 //A thread is exiting cleanly. If the DLL has stored a pointer to allocated memory in //a TLS slot, it should use this opportunity to free the memory. The system calls the //entry-point function of all currently loaded DLLs with this value. The call is made //in the context of the exiting thread. case DLL_PROCESS_DETACH: // = 0 //The DLL is being unloaded from the virtual address space of the calling process //because it was loaded unsuccessfully or the reference count has reached zero //(the processes has either terminated or called FreeLibrary one time for each time it //called LoadLibrary). The DLL can use this opportunity to call the TlsFree function to //free any TLS indices allocated by using TlsAlloc and to free any thread local data. //Note that the thread that receives the DLL_PROCESS_DETACH notification is not necessarily //the same thread that received the DLL_PROCESS_ATTACH notification. break; } return TRUE; } #ifdef _MANAGED #pragma managed(pop) #endif // This is an example of an exported variable MYDLL_API int nMyDLL=0; // This is an example of an exported function. MYDLL_API int fnMyDLL(void) { return 42; } // This is the constructor of a class that has been exported. // see MyDLL.h for the class definition CMyDLL::CMyDLL() { return; }