dpwsockx: Implementation of SPInit
[wine/gsoc_dplay.git] / dlls / msi / handle.c
blob8d43775464d4b5f9aa6b9f72e6d6b44de08aa857
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-2004 Mike McCormack for CodeWeavers
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define COBJMACROS
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "shlwapi.h"
29 #include "wine/debug.h"
30 #include "msi.h"
31 #include "msiquery.h"
32 #include "msipriv.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(msi);
36 static CRITICAL_SECTION MSI_handle_cs;
37 static CRITICAL_SECTION_DEBUG MSI_handle_cs_debug =
39 0, 0, &MSI_handle_cs,
40 { &MSI_handle_cs_debug.ProcessLocksList,
41 &MSI_handle_cs_debug.ProcessLocksList },
42 0, 0, { (DWORD_PTR)(__FILE__ ": MSI_handle_cs") }
44 static CRITICAL_SECTION MSI_handle_cs = { &MSI_handle_cs_debug, -1, 0, 0, 0, 0 };
46 static CRITICAL_SECTION MSI_object_cs;
47 static CRITICAL_SECTION_DEBUG MSI_object_cs_debug =
49 0, 0, &MSI_object_cs,
50 { &MSI_object_cs_debug.ProcessLocksList,
51 &MSI_object_cs_debug.ProcessLocksList },
52 0, 0, { (DWORD_PTR)(__FILE__ ": MSI_object_cs") }
54 static CRITICAL_SECTION MSI_object_cs = { &MSI_object_cs_debug, -1, 0, 0, 0, 0 };
56 typedef struct msi_handle_info_t
58 BOOL remote;
59 union {
60 MSIOBJECTHDR *obj;
61 IUnknown *unk;
62 } u;
63 DWORD dwThreadId;
64 } msi_handle_info;
66 static msi_handle_info *msihandletable = NULL;
67 static unsigned int msihandletable_size = 0;
69 void msi_free_handle_table(void)
71 msi_free( msihandletable );
72 msihandletable = NULL;
73 msihandletable_size = 0;
76 static MSIHANDLE alloc_handle_table_entry(void)
78 UINT i;
80 /* find a slot */
81 for(i=0; i<msihandletable_size; i++)
82 if( !msihandletable[i].u.obj && !msihandletable[i].u.unk )
83 break;
84 if( i==msihandletable_size )
86 msi_handle_info *p;
87 int newsize;
88 if (msihandletable_size == 0)
90 newsize = 256;
91 p = msi_alloc_zero(newsize*sizeof(msi_handle_info));
93 else
95 newsize = msihandletable_size * 2;
96 p = msi_realloc_zero(msihandletable,
97 newsize*sizeof(msi_handle_info));
99 if (!p)
100 return 0;
101 msihandletable = p;
102 msihandletable_size = newsize;
104 return i + 1;
107 MSIHANDLE alloc_msihandle( MSIOBJECTHDR *obj )
109 msi_handle_info *entry;
110 MSIHANDLE ret;
112 EnterCriticalSection( &MSI_handle_cs );
114 ret = alloc_handle_table_entry();
115 if (ret)
117 entry = &msihandletable[ ret - 1 ];
118 msiobj_addref( obj );
119 entry->u.obj = obj;
120 entry->dwThreadId = GetCurrentThreadId();
121 entry->remote = FALSE;
124 LeaveCriticalSection( &MSI_handle_cs );
126 TRACE("%p -> %d\n", obj, ret );
128 return ret;
131 MSIHANDLE alloc_msi_remote_handle( IUnknown *unk )
133 msi_handle_info *entry;
134 MSIHANDLE ret;
136 EnterCriticalSection( &MSI_handle_cs );
138 ret = alloc_handle_table_entry();
139 if (ret)
141 entry = &msihandletable[ ret - 1 ];
142 IUnknown_AddRef( unk );
143 entry->u.unk = unk;
144 entry->dwThreadId = GetCurrentThreadId();
145 entry->remote = TRUE;
148 LeaveCriticalSection( &MSI_handle_cs );
150 TRACE("%p -> %d\n", unk, ret);
152 return ret;
155 void *msihandle2msiinfo(MSIHANDLE handle, UINT type)
157 MSIOBJECTHDR *ret = NULL;
159 EnterCriticalSection( &MSI_handle_cs );
160 handle--;
161 if( handle >= msihandletable_size )
162 goto out;
163 if( msihandletable[handle].remote)
164 goto out;
165 if( !msihandletable[handle].u.obj )
166 goto out;
167 if( msihandletable[handle].u.obj->magic != MSIHANDLE_MAGIC )
168 goto out;
169 if( type && (msihandletable[handle].u.obj->type != type) )
170 goto out;
171 ret = msihandletable[handle].u.obj;
172 msiobj_addref( ret );
174 out:
175 LeaveCriticalSection( &MSI_handle_cs );
177 return ret;
180 IUnknown *msi_get_remote( MSIHANDLE handle )
182 IUnknown *unk = NULL;
184 EnterCriticalSection( &MSI_handle_cs );
185 handle--;
186 if( handle>=msihandletable_size )
187 goto out;
188 if( !msihandletable[handle].remote)
189 goto out;
190 unk = msihandletable[handle].u.unk;
191 if( unk )
192 IUnknown_AddRef( unk );
194 out:
195 LeaveCriticalSection( &MSI_handle_cs );
197 return unk;
200 void *alloc_msiobject(UINT type, UINT size, msihandledestructor destroy )
202 MSIOBJECTHDR *info;
204 info = msi_alloc_zero( size );
205 if( info )
207 info->magic = MSIHANDLE_MAGIC;
208 info->type = type;
209 info->refcount = 1;
210 info->destructor = destroy;
213 return info;
216 void msiobj_addref( MSIOBJECTHDR *info )
218 if( !info )
219 return;
221 if( info->magic != MSIHANDLE_MAGIC )
223 ERR("Invalid handle!\n");
224 return;
227 InterlockedIncrement(&info->refcount);
230 void msiobj_lock( MSIOBJECTHDR *info )
232 EnterCriticalSection( &MSI_object_cs );
235 void msiobj_unlock( MSIOBJECTHDR *info )
237 LeaveCriticalSection( &MSI_object_cs );
240 int msiobj_release( MSIOBJECTHDR *info )
242 int ret;
244 if( !info )
245 return -1;
247 if( info->magic != MSIHANDLE_MAGIC )
249 ERR("Invalid handle!\n");
250 return -1;
253 ret = InterlockedDecrement( &info->refcount );
254 if( ret==0 )
256 if( info->destructor )
257 info->destructor( info );
258 msi_free( info );
259 TRACE("object %p destroyed\n", info);
262 return ret;
265 /***********************************************************
266 * MsiCloseHandle [MSI.@]
268 UINT WINAPI MsiCloseHandle(MSIHANDLE handle)
270 MSIOBJECTHDR *info = NULL;
271 UINT ret = ERROR_INVALID_HANDLE;
273 TRACE("%x\n",handle);
275 if (!handle)
276 return ERROR_SUCCESS;
278 EnterCriticalSection( &MSI_handle_cs );
280 handle--;
281 if (handle >= msihandletable_size)
282 goto out;
284 if (msihandletable[handle].remote)
286 IUnknown_Release( msihandletable[handle].u.unk );
288 else
290 info = msihandletable[handle].u.obj;
291 if( !info )
292 goto out;
294 if( info->magic != MSIHANDLE_MAGIC )
296 ERR("Invalid handle!\n");
297 goto out;
301 msihandletable[handle].u.obj = NULL;
302 msihandletable[handle].remote = 0;
303 msihandletable[handle].dwThreadId = 0;
305 ret = ERROR_SUCCESS;
307 TRACE("handle %x destroyed\n", handle+1);
308 out:
309 LeaveCriticalSection( &MSI_handle_cs );
310 if( info )
311 msiobj_release( info );
313 return ret;
316 /***********************************************************
317 * MsiCloseAllHandles [MSI.@]
319 * Closes all handles owned by the current thread
321 * RETURNS:
322 * The number of handles closed
324 UINT WINAPI MsiCloseAllHandles(void)
326 UINT i, n=0;
328 TRACE("\n");
330 EnterCriticalSection( &MSI_handle_cs );
331 for(i=0; i<msihandletable_size; i++)
333 if(msihandletable[i].dwThreadId == GetCurrentThreadId())
335 LeaveCriticalSection( &MSI_handle_cs );
336 MsiCloseHandle( i+1 );
337 EnterCriticalSection( &MSI_handle_cs );
338 n++;
341 LeaveCriticalSection( &MSI_handle_cs );
343 return n;