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;
74 DeleteCriticalSection(&MSI_handle_cs);
75 DeleteCriticalSection(&MSI_object_cs);
78 static MSIHANDLE alloc_handle_table_entry(void)
83 for(i=0; i<msihandletable_size; i++)
84 if( !msihandletable[i].u.obj && !msihandletable[i].u.unk )
86 if( i==msihandletable_size )
90 if (msihandletable_size == 0)
93 p = msi_alloc_zero(newsize*sizeof(msi_handle_info));
97 newsize = msihandletable_size * 2;
98 p = msi_realloc_zero(msihandletable,
99 newsize*sizeof(msi_handle_info));
104 msihandletable_size = newsize;
109 MSIHANDLE alloc_msihandle( MSIOBJECTHDR *obj )
111 msi_handle_info *entry;
114 EnterCriticalSection( &MSI_handle_cs );
116 ret = alloc_handle_table_entry();
119 entry = &msihandletable[ ret - 1 ];
120 msiobj_addref( obj );
122 entry->dwThreadId = GetCurrentThreadId();
123 entry->remote = FALSE;
126 LeaveCriticalSection( &MSI_handle_cs );
128 TRACE("%p -> %d\n", obj, ret );
133 MSIHANDLE alloc_msi_remote_handle( IUnknown *unk )
135 msi_handle_info *entry;
138 EnterCriticalSection( &MSI_handle_cs );
140 ret = alloc_handle_table_entry();
143 entry = &msihandletable[ ret - 1 ];
144 IUnknown_AddRef( unk );
146 entry->dwThreadId = GetCurrentThreadId();
147 entry->remote = TRUE;
150 LeaveCriticalSection( &MSI_handle_cs );
152 TRACE("%p -> %d\n", unk, ret);
157 void *msihandle2msiinfo(MSIHANDLE handle, UINT type)
159 MSIOBJECTHDR *ret = NULL;
161 EnterCriticalSection( &MSI_handle_cs );
163 if( handle >= msihandletable_size )
165 if( msihandletable[handle].remote)
167 if( !msihandletable[handle].u.obj )
169 if( msihandletable[handle].u.obj->magic != MSIHANDLE_MAGIC )
171 if( type && (msihandletable[handle].u.obj->type != type) )
173 ret = msihandletable[handle].u.obj;
174 msiobj_addref( ret );
177 LeaveCriticalSection( &MSI_handle_cs );
182 IUnknown *msi_get_remote( MSIHANDLE handle )
184 IUnknown *unk = NULL;
186 EnterCriticalSection( &MSI_handle_cs );
188 if( handle>=msihandletable_size )
190 if( !msihandletable[handle].remote)
192 unk = msihandletable[handle].u.unk;
194 IUnknown_AddRef( unk );
197 LeaveCriticalSection( &MSI_handle_cs );
202 void *alloc_msiobject(UINT type, UINT size, msihandledestructor destroy )
206 info = msi_alloc_zero( size );
209 info->magic = MSIHANDLE_MAGIC;
212 info->destructor = destroy;
218 void msiobj_addref( MSIOBJECTHDR *info )
223 if( info->magic != MSIHANDLE_MAGIC )
225 ERR("Invalid handle!\n");
229 InterlockedIncrement(&info->refcount);
232 void msiobj_lock( MSIOBJECTHDR *info )
234 EnterCriticalSection( &MSI_object_cs );
237 void msiobj_unlock( MSIOBJECTHDR *info )
239 LeaveCriticalSection( &MSI_object_cs );
242 int msiobj_release( MSIOBJECTHDR *info )
249 if( info->magic != MSIHANDLE_MAGIC )
251 ERR("Invalid handle!\n");
255 ret = InterlockedDecrement( &info->refcount );
258 if( info->destructor )
259 info->destructor( info );
261 TRACE("object %p destroyed\n", info);
267 /***********************************************************
268 * MsiCloseHandle [MSI.@]
270 UINT WINAPI MsiCloseHandle(MSIHANDLE handle)
272 MSIOBJECTHDR *info = NULL;
273 UINT ret = ERROR_INVALID_HANDLE;
275 TRACE("%x\n",handle);
278 return ERROR_SUCCESS;
280 EnterCriticalSection( &MSI_handle_cs );
283 if (handle >= msihandletable_size)
286 if (msihandletable[handle].remote)
288 IUnknown_Release( msihandletable[handle].u.unk );
292 info = msihandletable[handle].u.obj;
296 if( info->magic != MSIHANDLE_MAGIC )
298 ERR("Invalid handle!\n");
303 msihandletable[handle].u.obj = NULL;
304 msihandletable[handle].remote = 0;
305 msihandletable[handle].dwThreadId = 0;
309 TRACE("handle %x destroyed\n", handle+1);
311 LeaveCriticalSection( &MSI_handle_cs );
313 msiobj_release( info );
318 /***********************************************************
319 * MsiCloseAllHandles [MSI.@]
321 * Closes all handles owned by the current thread
324 * The number of handles closed
326 UINT WINAPI MsiCloseAllHandles(void)
332 EnterCriticalSection( &MSI_handle_cs );
333 for(i=0; i<msihandletable_size; i++)
335 if(msihandletable[i].dwThreadId == GetCurrentThreadId())
337 LeaveCriticalSection( &MSI_handle_cs );
338 MsiCloseHandle( i+1 );
339 EnterCriticalSection( &MSI_handle_cs );
343 LeaveCriticalSection( &MSI_handle_cs );