src/dll_entry.c

00001 
00002 #ifdef WIN32
00003 #include "ortp-config-win32.h"
00004 #else
00005 #include "ortp-config.h"
00006 #endif
00007 #include "ortp/ortp.h"  
00008 
00009 typedef struct __STRUCT_SHARED_DATA__
00010 {
00011         DWORD                           m_nReference;
00012         DWORD                           m_dwStartTime;
00013         BOOL                            m_bInitialize;
00014 
00015 } SHARED_DATA, * LPSHARED_DATA;
00016 
00017 #ifdef EXTERNAL_LOGGER
00018 #include "logger.h"
00019 #else
00020 #define RegisterLog(logVar, logString);
00021 #define UnregisterLog(logVar, logString);
00022 #endif
00023 
00024 extern DWORD dwoRTPLogLevel;
00025 
00026 #define SHMEMSIZE       sizeof(SHARED_DATA)
00027 
00028 static  LPSHARED_DATA   lpSharedData;
00029 static  HANDLE                  hMapObject       = NULL;  // handle to file mapping
00030 
00031 BOOL WINAPI DllMain( 
00032                                          HINSTANCE hinstDLL,    // handle to DLL module
00033                                          DWORD fdwReason,               // reason for calling function
00034                                          LPVOID lpReserved              // reserved
00035                                    )  
00036 {
00037         BOOL    fInit = FALSE;
00038         WORD    wVersionRequested;
00039         WSADATA wsaData;
00040 
00041     // Perform actions based on the reason for calling.
00042     switch( fdwReason ) 
00043     { 
00044         case DLL_PROCESS_ATTACH:
00045 
00046                         OutputDebugString("--> dll_entry.c - oRTP.dll - DLL_PROCESS_ATTACH()\n");
00047                  
00048                         wVersionRequested = MAKEWORD( 1, 0 );
00049 
00050                         if (WSAStartup(wVersionRequested,&wsaData)!=0) 
00051                         {
00052                                 return FALSE;
00053                         }
00054 
00055             // Create a named file mapping object. 
00056             hMapObject = CreateFileMapping( INVALID_HANDLE_VALUE,       // use paging file
00057                                                                                         NULL,                                   // default security attributes
00058                                                                                         PAGE_READWRITE,                 // read/write access
00059                                                                                         0,                                              // size: high 32-bits
00060                                                                                         SHMEMSIZE,                              // size: low 32-bits
00061                                                                                         "oRTPSharedMemory");  // name of map object
00062 
00063             if (hMapObject == NULL) 
00064                 return FALSE; 
00065  
00066             // The first process to attach initializes memory. 
00067             fInit = (GetLastError() != ERROR_ALREADY_EXISTS); 
00068  
00069             // Get a pointer to the file-mapped shared memory.
00070  
00071             lpSharedData = (LPSHARED_DATA) MapViewOfFile(   hMapObject,     // object to map view of
00072                                                                                                                         FILE_MAP_WRITE, // read/write access
00073                                                                                                                         0,              // high offset:  map from
00074                                                                                                                         0,              // low offset:   beginning
00075                                                                                                                         0);             // default: map entire file
00076             if (lpSharedData == NULL) 
00077                 return FALSE; 
00078  
00079             // Initialize memory if this is the first process.
00080  
00081             if (fInit) 
00082                         {
00083                                 OutputDebugString("--> dll_entry.c - oRTP.dll - Initializing module\n");
00084 
00085                                 lpSharedData->m_dwStartTime     = GetTickCount();
00086                                 lpSharedData->m_nReference      = 1;
00087                                 lpSharedData->m_bInitialize = FALSE;
00088 
00089                                 // Register the log
00090                                 RegisterLog(&dwoRTPLogLevel, "LOG_ORTP");
00091                         }
00092                         else
00093                         {
00094                                 OutputDebugString("--> dll_entry.c - oRTP.dll - Binding\n");
00095                                 lpSharedData->m_nReference++;
00096                         }
00097             break;
00098 
00099         case DLL_THREAD_ATTACH:
00100 
00101                         if (lpSharedData != NULL)
00102                         {
00103                                 if (lpSharedData->m_bInitialize == FALSE)
00104                                 {
00105                                         // Initialize oRTP
00106                                         ortp_init();
00107 
00108                                         // Start the scheduler
00109                                         //ortp_scheduler_init();
00110 
00111                                         lpSharedData->m_bInitialize = TRUE;
00112                                 }
00113                         }
00114             break;
00115 
00116         case DLL_THREAD_DETACH:
00117                         break;
00118 
00119         case DLL_PROCESS_DETACH:
00120 
00121                         if (lpSharedData != NULL)
00122                         {                       
00123                                 OutputDebugString("--> dll_entry.c - oRTP.dll - Binding\n");
00124                                 lpSharedData->m_nReference--;
00125 
00126                                 if (lpSharedData->m_nReference == 0)
00127                                 {
00128                                         OutputDebugString("--> dll_entry.c - oRTP.dll - Detaching\n");
00129 
00130                                         ortp_exit();
00131                                         UnregisterLog(&dwoRTPLogLevel, "LOG_ORTP");
00132 
00133 
00134                                         // Unmap shared memory from the process's address space. 
00135                                         UnmapViewOfFile(lpSharedData);
00136                                         lpSharedData = NULL;
00137          
00138                                         // Close the process's handle to the file-mapping object.
00139                                         CloseHandle(hMapObject); 
00140                                         hMapObject = INVALID_HANDLE_VALUE;
00141                                 }
00142                         }
00143             break;
00144     }
00145 
00146     return TRUE;  // Successful DLL_PROCESS_ATTACH.
00147 }

Generated on Fri Jun 22 17:29:02 2007 for oRTP by  doxygen 1.5.2