| 1 | /* $Id: library.cpp,v 1.2 1999-09-08 11:29:27 davidr 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 | #include "ole32.h" | 
|---|
| 22 |  | 
|---|
| 23 | #include "oString.h" | 
|---|
| 24 | #include "moniker.h"    // RunningObjectTableImpl_*** | 
|---|
| 25 |  | 
|---|
| 26 | // ====================================================================== | 
|---|
| 27 | // Local Data | 
|---|
| 28 | // ====================================================================== | 
|---|
| 29 |  | 
|---|
| 30 | /* | 
|---|
| 31 | * this open DLL table belongs in a per process table, but my guess is that | 
|---|
| 32 | * it shouldn't live in the kernel, so I'll put them out here in DLL | 
|---|
| 33 | * space assuming that there is one OLE32 per process. | 
|---|
| 34 | */ | 
|---|
| 35 | typedef struct tagOpenDll | 
|---|
| 36 | { | 
|---|
| 37 | char *      DllName;                /* really only needed for debugging */ | 
|---|
| 38 | HINSTANCE   hLibrary; | 
|---|
| 39 | struct tagOpenDll * next; | 
|---|
| 40 | } OpenDll; | 
|---|
| 41 |  | 
|---|
| 42 | static OpenDll * openDllList = NULL;    /* linked list of open dlls */ | 
|---|
| 43 |  | 
|---|
| 44 | // ====================================================================== | 
|---|
| 45 | // Public API's | 
|---|
| 46 | // ====================================================================== | 
|---|
| 47 |  | 
|---|
| 48 | // ---------------------------------------------------------------------- | 
|---|
| 49 | // CoLoadLibrary | 
|---|
| 50 | // ---------------------------------------------------------------------- | 
|---|
| 51 | HINSTANCE WIN32API CoLoadLibrary(LPSTR lpszLibName, BOOL bAutoFree) | 
|---|
| 52 | { | 
|---|
| 53 | HINSTANCE   hLibrary; | 
|---|
| 54 | OpenDll *   ptr; | 
|---|
| 55 | OpenDll *   tmp; | 
|---|
| 56 | HANDLE      hdl = GetProcessHeap(); | 
|---|
| 57 |  | 
|---|
| 58 | dprintf(("OLE32: CoLoadLibrary(%s)", lpszLibName)); | 
|---|
| 59 |  | 
|---|
| 60 | hLibrary = LoadLibraryA(lpszLibName); | 
|---|
| 61 |  | 
|---|
| 62 | if (!bAutoFree) | 
|---|
| 63 | return hLibrary; | 
|---|
| 64 |  | 
|---|
| 65 | if (openDllList == NULL) | 
|---|
| 66 | { | 
|---|
| 67 | /* empty list -- add first node */ | 
|---|
| 68 | openDllList = (OpenDll*)HeapAlloc(hdl, 0, sizeof(OpenDll)); | 
|---|
| 69 | openDllList->DllName = (char *)HeapAlloc(hdl, 0, strlen(lpszLibName)); | 
|---|
| 70 | strcpy(openDllList->DllName, lpszLibName); | 
|---|
| 71 | openDllList->hLibrary = hLibrary; | 
|---|
| 72 | openDllList->next = NULL; | 
|---|
| 73 | } | 
|---|
| 74 | else | 
|---|
| 75 | { | 
|---|
| 76 | /* search for this dll */ | 
|---|
| 77 | int found = FALSE; | 
|---|
| 78 | for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) | 
|---|
| 79 | { | 
|---|
| 80 | if (ptr->hLibrary == hLibrary) | 
|---|
| 81 | { | 
|---|
| 82 | found = TRUE; | 
|---|
| 83 | break; | 
|---|
| 84 | } | 
|---|
| 85 | } | 
|---|
| 86 | if (!found) | 
|---|
| 87 | { | 
|---|
| 88 | /* dll not found, add it */ | 
|---|
| 89 | tmp = openDllList; | 
|---|
| 90 | openDllList = (OpenDll*)HeapAlloc(hdl, 0, sizeof(OpenDll)); | 
|---|
| 91 | openDllList->DllName = (char *)HeapAlloc(hdl, 0, strlen(lpszLibName)); | 
|---|
| 92 | strcpy(openDllList->DllName, lpszLibName); | 
|---|
| 93 | openDllList->hLibrary = hLibrary; | 
|---|
| 94 | openDllList->next = tmp; | 
|---|
| 95 | } | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | return hLibrary; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | // ---------------------------------------------------------------------- | 
|---|
| 102 | // CoFreeAllLibraries | 
|---|
| 103 | // ---------------------------------------------------------------------- | 
|---|
| 104 | void WIN32API CoFreeAllLibraries() | 
|---|
| 105 | { | 
|---|
| 106 | OpenDll *   ptr; | 
|---|
| 107 | OpenDll *   tmp; | 
|---|
| 108 |  | 
|---|
| 109 | dprintf(("OLE32: CoFreeAllLibraries")); | 
|---|
| 110 |  | 
|---|
| 111 | for (ptr = openDllList; ptr != NULL; ) | 
|---|
| 112 | { | 
|---|
| 113 | tmp=ptr->next; | 
|---|
| 114 | CoFreeLibrary(ptr->hLibrary); | 
|---|
| 115 | ptr = tmp; | 
|---|
| 116 | } | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | // ---------------------------------------------------------------------- | 
|---|
| 120 | // CoFreeLibrary | 
|---|
| 121 | // ---------------------------------------------------------------------- | 
|---|
| 122 | void WIN32API CoFreeLibrary(HINSTANCE hLibrary) | 
|---|
| 123 | { | 
|---|
| 124 | OpenDll *   ptr; | 
|---|
| 125 | OpenDll *   prev; | 
|---|
| 126 | OpenDll *   tmp; | 
|---|
| 127 |  | 
|---|
| 128 | dprintf(("OLE32: CoFreeLibrary")); | 
|---|
| 129 |  | 
|---|
| 130 | /* lookup library in linked list */ | 
|---|
| 131 | prev = NULL; | 
|---|
| 132 | for (ptr = openDllList; ptr != NULL; ptr=ptr->next) | 
|---|
| 133 | { | 
|---|
| 134 | if (ptr->hLibrary == hLibrary) | 
|---|
| 135 | break; | 
|---|
| 136 |  | 
|---|
| 137 | prev = ptr; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | if (ptr == NULL) | 
|---|
| 141 | return; | 
|---|
| 142 |  | 
|---|
| 143 | /* assert: ptr points to the library entry to free */ | 
|---|
| 144 |  | 
|---|
| 145 | /* free library and remove node from list */ | 
|---|
| 146 | FreeLibrary(hLibrary); | 
|---|
| 147 | if (ptr == openDllList) | 
|---|
| 148 | { | 
|---|
| 149 | tmp = openDllList->next; | 
|---|
| 150 | HeapFree(GetProcessHeap(), 0, openDllList->DllName); | 
|---|
| 151 | HeapFree(GetProcessHeap(), 0, openDllList); | 
|---|
| 152 | openDllList = tmp; | 
|---|
| 153 | } | 
|---|
| 154 | else | 
|---|
| 155 | { | 
|---|
| 156 | tmp = ptr->next; | 
|---|
| 157 | HeapFree(GetProcessHeap(), 0, ptr->DllName); | 
|---|
| 158 | HeapFree(GetProcessHeap(), 0, ptr); | 
|---|
| 159 | prev->next = tmp; | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | // ---------------------------------------------------------------------- | 
|---|
| 165 | // CoFreeUnusedLibraries | 
|---|
| 166 | // ---------------------------------------------------------------------- | 
|---|
| 167 | void WIN32API CoFreeUnusedLibraries() | 
|---|
| 168 | { | 
|---|
| 169 | OpenDll *ptr, *tmp; | 
|---|
| 170 | typedef HRESULT(*DllCanUnloadNowFunc)(void); | 
|---|
| 171 | DllCanUnloadNowFunc DllCanUnloadNow; | 
|---|
| 172 |  | 
|---|
| 173 | dprintf(("OLE32: CoFreeUnusedLibraries")); | 
|---|
| 174 |  | 
|---|
| 175 | for (ptr = openDllList; ptr != NULL; ) | 
|---|
| 176 | { | 
|---|
| 177 | DllCanUnloadNow = (DllCanUnloadNowFunc) GetProcAddress(ptr->hLibrary, "DllCanUnloadNow"); | 
|---|
| 178 |  | 
|---|
| 179 | if ( (DllCanUnloadNow != NULL) && (DllCanUnloadNow() == S_OK) ) | 
|---|
| 180 | { | 
|---|
| 181 | tmp=ptr->next; | 
|---|
| 182 | CoFreeLibrary(ptr->hLibrary); | 
|---|
| 183 | ptr = tmp; | 
|---|
| 184 | } | 
|---|
| 185 | else | 
|---|
| 186 | { | 
|---|
| 187 | ptr=ptr->next; | 
|---|
| 188 | } | 
|---|
| 189 | } | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|