| 1 | /* $Id: library.cpp,v 1.1 2001-04-26 19:26:11 sandervl Exp $ */ | 
|---|
| 2 | /* | 
|---|
| 3 | * | 
|---|
| 4 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 5 | * | 
|---|
| 6 | */ | 
|---|
| 7 | /* | 
|---|
| 8 | * COM/OLE misc. functions. | 
|---|
| 9 | * | 
|---|
| 10 | * 1/7/99 | 
|---|
| 11 | * | 
|---|
| 12 | * Copyright 1999 David J. Raison | 
|---|
| 13 | * | 
|---|
| 14 | * Some portions from Wine Implementation | 
|---|
| 15 | *   Copyright 1995  Martin von Loewis | 
|---|
| 16 | *   Copyright 1998  Justin Bradford | 
|---|
| 17 | *   Copyright 1999  Francis Beaudet | 
|---|
| 18 | *   Copyright 1999  Sylvain St-Germain | 
|---|
| 19 | */ | 
|---|
| 20 |  | 
|---|
| 21 | /***************************************************************************** | 
|---|
| 22 | * Includes                                                                  * | 
|---|
| 23 | *****************************************************************************/ | 
|---|
| 24 |  | 
|---|
| 25 | #include <odin.h> | 
|---|
| 26 | #include <odinwrap.h> | 
|---|
| 27 | #include <os2sel.h> | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | #include "ole32.h" | 
|---|
| 31 |  | 
|---|
| 32 | #include "oString.h" | 
|---|
| 33 | #include "moniker.h"    // RunningObjectTableImpl_*** | 
|---|
| 34 |  | 
|---|
| 35 |  | 
|---|
| 36 | /***************************************************************************** | 
|---|
| 37 | * Defines                                                                   * | 
|---|
| 38 | *****************************************************************************/ | 
|---|
| 39 |  | 
|---|
| 40 | ODINDEBUGCHANNEL(OLE32-LIBRARY) | 
|---|
| 41 |  | 
|---|
| 42 |  | 
|---|
| 43 | // ====================================================================== | 
|---|
| 44 | // Local Data | 
|---|
| 45 | // ====================================================================== | 
|---|
| 46 |  | 
|---|
| 47 | /* | 
|---|
| 48 | * this open DLL table belongs in a per process table, but my guess is that | 
|---|
| 49 | * it shouldn't live in the kernel, so I'll put them out here in DLL | 
|---|
| 50 | * space assuming that there is one OLE32 per process. | 
|---|
| 51 | */ | 
|---|
| 52 | typedef struct tagOpenDll | 
|---|
| 53 | { | 
|---|
| 54 | char *      DllName;                /* really only needed for debugging */ | 
|---|
| 55 | HINSTANCE   hLibrary; | 
|---|
| 56 | struct tagOpenDll * next; | 
|---|
| 57 | } OpenDll; | 
|---|
| 58 |  | 
|---|
| 59 | static OpenDll * openDllList = NULL;    /* linked list of open dlls */ | 
|---|
| 60 |  | 
|---|
| 61 | // ====================================================================== | 
|---|
| 62 | // Public API's | 
|---|
| 63 | // ====================================================================== | 
|---|
| 64 |  | 
|---|
| 65 | // ---------------------------------------------------------------------- | 
|---|
| 66 | // CoLoadLibrary | 
|---|
| 67 | // ---------------------------------------------------------------------- | 
|---|
| 68 | //HINSTANCE WIN32API CoLoadLibrary(LPSTR lpszLibName, BOOL bAutoFree) | 
|---|
| 69 | ODINFUNCTION2(HINSTANCE, CoLoadLibrary, | 
|---|
| 70 | LPSTR, lpszLibName, | 
|---|
| 71 | BOOL, bAutoFree) | 
|---|
| 72 | { | 
|---|
| 73 | HINSTANCE   hLibrary; | 
|---|
| 74 | OpenDll *   ptr; | 
|---|
| 75 | OpenDll *   tmp; | 
|---|
| 76 | HANDLE      hdl = GetProcessHeap(); | 
|---|
| 77 |  | 
|---|
| 78 | dprintf(("OLE32: CoLoadLibrary(%s)", lpszLibName)); | 
|---|
| 79 |  | 
|---|
| 80 | hLibrary = LoadLibraryExA(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH); | 
|---|
| 81 |  | 
|---|
| 82 | if (!bAutoFree) | 
|---|
| 83 | return hLibrary; | 
|---|
| 84 |  | 
|---|
| 85 | if (openDllList == NULL) | 
|---|
| 86 | { | 
|---|
| 87 | /* empty list -- add first node */ | 
|---|
| 88 | openDllList = (OpenDll*)HeapAlloc(hdl, 0, sizeof(OpenDll)); | 
|---|
| 89 | openDllList->DllName = (char *)HeapAlloc(hdl, 0, strlen(lpszLibName)); | 
|---|
| 90 | strcpy(openDllList->DllName, lpszLibName); | 
|---|
| 91 | openDllList->hLibrary = hLibrary; | 
|---|
| 92 | openDllList->next = NULL; | 
|---|
| 93 | } | 
|---|
| 94 | else | 
|---|
| 95 | { | 
|---|
| 96 | /* search for this dll */ | 
|---|
| 97 | int found = FALSE; | 
|---|
| 98 | for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) | 
|---|
| 99 | { | 
|---|
| 100 | if (ptr->hLibrary == hLibrary) | 
|---|
| 101 | { | 
|---|
| 102 | found = TRUE; | 
|---|
| 103 | break; | 
|---|
| 104 | } | 
|---|
| 105 | } | 
|---|
| 106 | if (!found) | 
|---|
| 107 | { | 
|---|
| 108 | /* dll not found, add it */ | 
|---|
| 109 | tmp = openDllList; | 
|---|
| 110 | openDllList = (OpenDll*)HeapAlloc(hdl, 0, sizeof(OpenDll)); | 
|---|
| 111 | openDllList->DllName = (char *)HeapAlloc(hdl, 0, strlen(lpszLibName)); | 
|---|
| 112 | strcpy(openDllList->DllName, lpszLibName); | 
|---|
| 113 | openDllList->hLibrary = hLibrary; | 
|---|
| 114 | openDllList->next = tmp; | 
|---|
| 115 | } | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | return hLibrary; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | // ---------------------------------------------------------------------- | 
|---|
| 122 | // CoFreeAllLibraries | 
|---|
| 123 | // ---------------------------------------------------------------------- | 
|---|
| 124 | void WIN32API CoFreeAllLibraries() | 
|---|
| 125 | { | 
|---|
| 126 | OpenDll *   ptr; | 
|---|
| 127 | OpenDll *   tmp; | 
|---|
| 128 |  | 
|---|
| 129 | dprintf(("OLE32: CoFreeAllLibraries")); | 
|---|
| 130 |  | 
|---|
| 131 | for (ptr = openDllList; ptr != NULL; ) | 
|---|
| 132 | { | 
|---|
| 133 | tmp=ptr->next; | 
|---|
| 134 | CoFreeLibrary(ptr->hLibrary); | 
|---|
| 135 | ptr = tmp; | 
|---|
| 136 | } | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | // ---------------------------------------------------------------------- | 
|---|
| 140 | // CoFreeLibrary | 
|---|
| 141 | // ---------------------------------------------------------------------- | 
|---|
| 142 | void WIN32API CoFreeLibrary(HINSTANCE hLibrary) | 
|---|
| 143 | { | 
|---|
| 144 | OpenDll *   ptr; | 
|---|
| 145 | OpenDll *   prev; | 
|---|
| 146 | OpenDll *   tmp; | 
|---|
| 147 |  | 
|---|
| 148 | dprintf(("OLE32: CoFreeLibrary")); | 
|---|
| 149 |  | 
|---|
| 150 | /* lookup library in linked list */ | 
|---|
| 151 | prev = NULL; | 
|---|
| 152 | for (ptr = openDllList; ptr != NULL; ptr=ptr->next) | 
|---|
| 153 | { | 
|---|
| 154 | if (ptr->hLibrary == hLibrary) | 
|---|
| 155 | break; | 
|---|
| 156 |  | 
|---|
| 157 | prev = ptr; | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | if (ptr == NULL) | 
|---|
| 161 | return; | 
|---|
| 162 |  | 
|---|
| 163 | /* assert: ptr points to the library entry to free */ | 
|---|
| 164 |  | 
|---|
| 165 | /* free library and remove node from list */ | 
|---|
| 166 | FreeLibrary(hLibrary); | 
|---|
| 167 | if (ptr == openDllList) | 
|---|
| 168 | { | 
|---|
| 169 | tmp = openDllList->next; | 
|---|
| 170 | HeapFree(GetProcessHeap(), 0, openDllList->DllName); | 
|---|
| 171 | HeapFree(GetProcessHeap(), 0, openDllList); | 
|---|
| 172 | openDllList = tmp; | 
|---|
| 173 | } | 
|---|
| 174 | else | 
|---|
| 175 | { | 
|---|
| 176 | tmp = ptr->next; | 
|---|
| 177 | HeapFree(GetProcessHeap(), 0, ptr->DllName); | 
|---|
| 178 | HeapFree(GetProcessHeap(), 0, ptr); | 
|---|
| 179 | prev->next = tmp; | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | // ---------------------------------------------------------------------- | 
|---|
| 185 | // CoFreeUnusedLibraries | 
|---|
| 186 | // ---------------------------------------------------------------------- | 
|---|
| 187 | void WIN32API CoFreeUnusedLibraries() | 
|---|
| 188 | { | 
|---|
| 189 | OpenDll *ptr, *tmp; | 
|---|
| 190 | typedef HRESULT(*DllCanUnloadNowFunc)(void); | 
|---|
| 191 | DllCanUnloadNowFunc DllCanUnloadNow; | 
|---|
| 192 |  | 
|---|
| 193 | dprintf(("OLE32: CoFreeUnusedLibraries")); | 
|---|
| 194 |  | 
|---|
| 195 | for (ptr = openDllList; ptr != NULL; ) | 
|---|
| 196 | { | 
|---|
| 197 | DllCanUnloadNow = (DllCanUnloadNowFunc) GetProcAddress(ptr->hLibrary, "DllCanUnloadNow"); | 
|---|
| 198 |  | 
|---|
| 199 | if ( (DllCanUnloadNow != NULL) && (DllCanUnloadNow() == S_OK) ) | 
|---|
| 200 | { | 
|---|
| 201 | tmp=ptr->next; | 
|---|
| 202 | CoFreeLibrary(ptr->hLibrary); | 
|---|
| 203 | ptr = tmp; | 
|---|
| 204 | } | 
|---|
| 205 | else | 
|---|
| 206 | { | 
|---|
| 207 | ptr=ptr->next; | 
|---|
| 208 | } | 
|---|
| 209 | } | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|