MsiCloseAllHandles only closes handles allocated in the calling
[wine] / dlls / msi / handle.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2002-2004 Mike McCormack for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winreg.h"
26 #include "shlwapi.h"
27 #include "wine/debug.h"
28 #include "msi.h"
29 #include "msiquery.h"
30 #include "msipriv.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(msi);
33
34 static CRITICAL_SECTION MSI_handle_cs;
35 static CRITICAL_SECTION_DEBUG MSI_handle_cs_debug =
36 {
37     0, 0, &MSI_handle_cs,
38     { &MSI_handle_cs_debug.ProcessLocksList, 
39       &MSI_handle_cs_debug.ProcessLocksList },
40       0, 0, { 0, (DWORD)(__FILE__ ": MSI_handle_cs") }
41 };
42 static CRITICAL_SECTION MSI_handle_cs = { &MSI_handle_cs_debug, -1, 0, 0, 0, 0 };
43
44 static CRITICAL_SECTION MSI_object_cs;
45 static CRITICAL_SECTION_DEBUG MSI_object_cs_debug =
46 {
47     0, 0, &MSI_object_cs,
48     { &MSI_object_cs_debug.ProcessLocksList, 
49       &MSI_object_cs_debug.ProcessLocksList },
50       0, 0, { 0, (DWORD)(__FILE__ ": MSI_object_cs") }
51 };
52 static CRITICAL_SECTION MSI_object_cs = { &MSI_object_cs_debug, -1, 0, 0, 0, 0 };
53
54 typedef struct msi_handle_info_t
55 {
56     MSIOBJECTHDR *obj;
57     DWORD dwThreadId;
58 } msi_handle_info;
59
60 static msi_handle_info msihandletable[MSIMAXHANDLES];
61
62 MSIHANDLE alloc_msihandle( MSIOBJECTHDR *obj )
63 {
64     MSIHANDLE ret = 0;
65     UINT i;
66
67     EnterCriticalSection( &MSI_handle_cs );
68
69     /* find a slot */
70     for(i=0; i<MSIMAXHANDLES; i++)
71         if( !msihandletable[i].obj )
72             break;
73     if( (i>=MSIMAXHANDLES) || msihandletable[i].obj )
74         goto out;
75
76     msiobj_addref( obj );
77     msihandletable[i].obj = obj;
78     msihandletable[i].dwThreadId = GetCurrentThreadId();
79     ret = (MSIHANDLE) (i+1);
80 out:
81     TRACE("%p -> %ld\n", obj, ret );
82
83     LeaveCriticalSection( &MSI_handle_cs );
84     return ret;
85 }
86
87 void *msihandle2msiinfo(MSIHANDLE handle, UINT type)
88 {
89     MSIOBJECTHDR *ret = NULL;
90
91     EnterCriticalSection( &MSI_handle_cs );
92     handle--;
93     if( handle<0 )
94         goto out;
95     if( handle>=MSIMAXHANDLES )
96         goto out;
97     if( !msihandletable[handle].obj )
98         goto out;
99     if( msihandletable[handle].obj->magic != MSIHANDLE_MAGIC )
100         goto out;
101     if( type && (msihandletable[handle].obj->type != type) )
102         goto out;
103     ret = msihandletable[handle].obj;
104     msiobj_addref( ret );
105     
106 out:
107     LeaveCriticalSection( &MSI_handle_cs );
108
109     return (void*) ret;
110 }
111
112 MSIHANDLE msiobj_findhandle( MSIOBJECTHDR *hdr )
113 {
114     MSIHANDLE ret = 0;
115     UINT i;
116
117     TRACE("%p\n", hdr);
118
119     EnterCriticalSection( &MSI_handle_cs );
120     for(i=0; (i<MSIMAXHANDLES) && !ret; i++)
121         if( msihandletable[i].obj == hdr )
122             ret = i+1;
123     LeaveCriticalSection( &MSI_handle_cs );
124
125     TRACE("%p -> %ld\n", hdr, ret);
126
127     msiobj_addref( hdr );
128     return ret;
129 }
130
131 void *alloc_msiobject(UINT type, UINT size, msihandledestructor destroy )
132 {
133     MSIOBJECTHDR *info;
134
135     info = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size );
136     if( info )
137     {
138         info->magic = MSIHANDLE_MAGIC;
139         info->type = type;
140         info->refcount = 1;
141         info->destructor = destroy;
142     }
143
144     return info;
145 }
146
147 void msiobj_addref( MSIOBJECTHDR *info )
148 {
149     TRACE("%p\n", info);
150
151     if( !info )
152         return;
153
154     if( info->magic != MSIHANDLE_MAGIC )
155     {
156         ERR("Invalid handle!\n");
157         return;
158     }
159
160     info->refcount++;
161 }
162
163 void msiobj_lock( MSIOBJECTHDR *info )
164 {
165     EnterCriticalSection( &MSI_object_cs );
166 }
167
168 void msiobj_unlock( MSIOBJECTHDR *info )
169 {
170     LeaveCriticalSection( &MSI_object_cs );
171 }
172
173 int msiobj_release( MSIOBJECTHDR *info )
174 {
175     int ret;
176
177     TRACE("%p\n",info);
178
179     if( !info )
180         return -1;
181
182     if( info->magic != MSIHANDLE_MAGIC )
183     {
184         ERR("Invalid handle!\n");
185         return -1;
186     }
187
188     ret = info->refcount--;
189     if (info->refcount == 0)
190     {
191     if( info->destructor )
192             info->destructor( info );
193     HeapFree( GetProcessHeap(), 0, info );
194         TRACE("object %p destroyed\n", info);
195     }
196
197     return ret;
198 }
199
200 /***********************************************************
201  *   MsiCloseHandle   [MSI.@]
202  */
203 UINT WINAPI MsiCloseHandle(MSIHANDLE handle)
204 {
205     MSIOBJECTHDR *info;
206     UINT ret = ERROR_INVALID_HANDLE;
207
208     TRACE("%lx\n",handle);
209
210     EnterCriticalSection( &MSI_handle_cs );
211
212     info = msihandle2msiinfo(handle, 0);
213     if( !info )
214         goto out;
215
216     if( info->magic != MSIHANDLE_MAGIC )
217     {
218         ERR("Invalid handle!\n");
219         goto out;
220     }
221
222     msiobj_release( info );
223     msihandletable[handle-1].obj = NULL;
224     ret = ERROR_SUCCESS;
225
226     TRACE("handle %lx Destroyed\n", handle);
227 out:
228     LeaveCriticalSection( &MSI_handle_cs );
229     if( info )
230         msiobj_release( info );
231
232     return ret;
233 }
234
235 /***********************************************************
236  *   MsiCloseAllHandles   [MSI.@]
237  *
238  *  Closes all handles owned by the current thread
239  *
240  *  RETURNS:
241  *   The number of handles closed
242  */
243 UINT WINAPI MsiCloseAllHandles(void)
244 {
245     UINT i, n=0;
246
247     TRACE("\n");
248
249     for(i=0; i<MSIMAXHANDLES; i++)
250     {
251         if(msihandletable[i].dwThreadId == GetCurrentThreadId())
252         {
253             MsiCloseHandle( i+1 );
254             n++;
255         }
256     }
257
258     return n;
259 }