source: trunk/src/ole32/library.cpp@ 784

Last change on this file since 784 was 784, checked in by davidr, 26 years ago

Added IUnknown & library handling.
Removed stubs accordingly.

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