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
29 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(msi);
36 static CRITICAL_SECTION MSI_handle_cs;
37 static CRITICAL_SECTION_DEBUG MSI_handle_cs_debug =
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 =
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
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)
81 for(i=0; i<msihandletable_size; i++)
82 if( !msihandletable[i].u.obj && !msihandletable[i].u.unk )
84 if( i==msihandletable_size )
88 if (msihandletable_size == 0)
91 p = msi_alloc_zero(newsize*sizeof(msi_handle_info));
95 newsize = msihandletable_size * 2;
96 p = msi_realloc_zero(msihandletable,
97 newsize*sizeof(msi_handle_info));
102 msihandletable_size = newsize;
107 MSIHANDLE alloc_msihandle( MSIOBJECTHDR *obj )
109 msi_handle_info *entry;
112 EnterCriticalSection( &MSI_handle_cs );
114 ret = alloc_handle_table_entry();
117 entry = &msihandletable[ ret - 1 ];
118 msiobj_addref( obj );
120 entry->dwThreadId = GetCurrentThreadId();
121 entry->remote = FALSE;
124 LeaveCriticalSection( &MSI_handle_cs );
126 TRACE("%p -> %ld\n", obj, ret );
131 MSIHANDLE alloc_msi_remote_handle( IUnknown *unk )
133 msi_handle_info *entry;
136 EnterCriticalSection( &MSI_handle_cs );
138 ret = alloc_handle_table_entry();
141 entry = &msihandletable[ ret - 1 ];
142 IUnknown_AddRef( unk );
144 entry->dwThreadId = GetCurrentThreadId();
145 entry->remote = TRUE;
148 LeaveCriticalSection( &MSI_handle_cs );
150 TRACE("%p -> %ld\n", unk, ret);
155 void *msihandle2msiinfo(MSIHANDLE handle, UINT type)
157 MSIOBJECTHDR *ret = NULL;
159 EnterCriticalSection( &MSI_handle_cs );
161 if( handle >= msihandletable_size )
163 if( msihandletable[handle].remote)
165 if( !msihandletable[handle].u.obj )
167 if( msihandletable[handle].u.obj->magic != MSIHANDLE_MAGIC )
169 if( type && (msihandletable[handle].u.obj->type != type) )
171 ret = msihandletable[handle].u.obj;
172 msiobj_addref( ret );
175 LeaveCriticalSection( &MSI_handle_cs );
180 IUnknown *msi_get_remote( MSIHANDLE handle )
182 IUnknown *unk = NULL;
184 EnterCriticalSection( &MSI_handle_cs );
186 if( handle>=msihandletable_size )
188 if( !msihandletable[handle].remote)
190 unk = msihandletable[handle].u.unk;
192 IUnknown_AddRef( unk );
195 LeaveCriticalSection( &MSI_handle_cs );
200 void *alloc_msiobject(UINT type, UINT size, msihandledestructor destroy )
204 info = msi_alloc_zero( size );
207 info->magic = MSIHANDLE_MAGIC;
210 info->destructor = destroy;
216 void msiobj_addref( MSIOBJECTHDR *info )
221 if( info->magic != MSIHANDLE_MAGIC )
223 ERR("Invalid handle!\n");
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 )
247 if( info->magic != MSIHANDLE_MAGIC )
249 ERR("Invalid handle!\n");
253 ret = InterlockedDecrement( &info->refcount );
256 if( info->destructor )
257 info->destructor( info );
259 TRACE("object %p destroyed\n", info);
265 /***********************************************************
266 * MsiCloseHandle [MSI.@]
268 UINT WINAPI MsiCloseHandle(MSIHANDLE handle)
270 MSIOBJECTHDR *info = NULL;
271 UINT ret = ERROR_INVALID_HANDLE;
273 TRACE("%lx\n",handle);
276 return ERROR_SUCCESS;
278 EnterCriticalSection( &MSI_handle_cs );
281 if (handle >= msihandletable_size)
284 if (msihandletable[handle].remote)
286 IUnknown_Release( msihandletable[handle].u.unk );
290 info = msihandletable[handle].u.obj;
294 if( info->magic != MSIHANDLE_MAGIC )
296 ERR("Invalid handle!\n");
301 msihandletable[handle].u.obj = NULL;
302 msihandletable[handle].remote = 0;
303 msihandletable[handle].dwThreadId = 0;
307 TRACE("handle %lx destroyed\n", handle+1);
309 LeaveCriticalSection( &MSI_handle_cs );
311 msiobj_release( info );
316 /***********************************************************
317 * MsiCloseAllHandles [MSI.@]
319 * Closes all handles owned by the current thread
322 * The number of handles closed
324 UINT WINAPI MsiCloseAllHandles(void)
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 );
341 LeaveCriticalSection( &MSI_handle_cs );