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
27 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(msi);
34 static CRITICAL_SECTION MSI_handle_cs;
35 static CRITICAL_SECTION_DEBUG MSI_handle_cs_debug =
38 { &MSI_handle_cs_debug.ProcessLocksList,
39 &MSI_handle_cs_debug.ProcessLocksList },
40 0, 0, { (DWORD_PTR)(__FILE__ ": MSI_handle_cs") }
42 static CRITICAL_SECTION MSI_handle_cs = { &MSI_handle_cs_debug, -1, 0, 0, 0, 0 };
44 static CRITICAL_SECTION MSI_object_cs;
45 static CRITICAL_SECTION_DEBUG MSI_object_cs_debug =
48 { &MSI_object_cs_debug.ProcessLocksList,
49 &MSI_object_cs_debug.ProcessLocksList },
50 0, 0, { (DWORD_PTR)(__FILE__ ": MSI_object_cs") }
52 static CRITICAL_SECTION MSI_object_cs = { &MSI_object_cs_debug, -1, 0, 0, 0, 0 };
54 typedef struct msi_handle_info_t
60 static msi_handle_info *msihandletable = NULL;
61 static int msihandletable_size = 0;
63 void msi_free_handle_table(void)
65 msi_free( msihandletable );
66 msihandletable = NULL;
67 msihandletable_size = 0;
70 MSIHANDLE alloc_msihandle( MSIOBJECTHDR *obj )
75 EnterCriticalSection( &MSI_handle_cs );
78 for(i=0; i<msihandletable_size; i++)
79 if( !msihandletable[i].obj )
81 if( i==msihandletable_size )
85 if (msihandletable_size == 0)
88 p = msi_alloc_zero(newsize*sizeof(msi_handle_info));
92 newsize = msihandletable_size * 2;
93 p = msi_realloc_zero(msihandletable,
94 newsize*sizeof(msi_handle_info));
99 msihandletable_size = newsize;
102 msiobj_addref( obj );
103 msihandletable[i].obj = obj;
104 msihandletable[i].dwThreadId = GetCurrentThreadId();
105 ret = (MSIHANDLE) (i+1);
107 TRACE("%p -> %ld\n", obj, ret );
109 LeaveCriticalSection( &MSI_handle_cs );
113 void *msihandle2msiinfo(MSIHANDLE handle, UINT type)
115 MSIOBJECTHDR *ret = NULL;
117 EnterCriticalSection( &MSI_handle_cs );
121 if( handle>=msihandletable_size )
123 if( !msihandletable[handle].obj )
125 if( msihandletable[handle].obj->magic != MSIHANDLE_MAGIC )
127 if( type && (msihandletable[handle].obj->type != type) )
129 ret = msihandletable[handle].obj;
130 msiobj_addref( ret );
133 LeaveCriticalSection( &MSI_handle_cs );
138 void *alloc_msiobject(UINT type, UINT size, msihandledestructor destroy )
142 info = msi_alloc_zero( size );
145 info->magic = MSIHANDLE_MAGIC;
148 info->destructor = destroy;
154 void msiobj_addref( MSIOBJECTHDR *info )
159 if( info->magic != MSIHANDLE_MAGIC )
161 ERR("Invalid handle!\n");
165 InterlockedIncrement(&info->refcount);
168 void msiobj_lock( MSIOBJECTHDR *info )
170 EnterCriticalSection( &MSI_object_cs );
173 void msiobj_unlock( MSIOBJECTHDR *info )
175 LeaveCriticalSection( &MSI_object_cs );
178 int msiobj_release( MSIOBJECTHDR *info )
185 if( info->magic != MSIHANDLE_MAGIC )
187 ERR("Invalid handle!\n");
191 ret = InterlockedDecrement( &info->refcount );
194 if( info->destructor )
195 info->destructor( info );
197 TRACE("object %p destroyed\n", info);
203 /***********************************************************
204 * MsiCloseHandle [MSI.@]
206 UINT WINAPI MsiCloseHandle(MSIHANDLE handle)
209 UINT ret = ERROR_INVALID_HANDLE;
211 TRACE("%lx\n",handle);
214 return ERROR_SUCCESS;
216 EnterCriticalSection( &MSI_handle_cs );
218 info = msihandle2msiinfo(handle, 0);
222 if( info->magic != MSIHANDLE_MAGIC )
224 ERR("Invalid handle!\n");
228 msiobj_release( info );
229 msihandletable[handle-1].obj = NULL;
232 TRACE("handle %lx Destroyed\n", handle);
234 LeaveCriticalSection( &MSI_handle_cs );
236 msiobj_release( info );
241 /***********************************************************
242 * MsiCloseAllHandles [MSI.@]
244 * Closes all handles owned by the current thread
247 * The number of handles closed
249 UINT WINAPI MsiCloseAllHandles(void)
255 EnterCriticalSection( &MSI_handle_cs );
256 for(i=0; i<msihandletable_size; i++)
258 if(msihandletable[i].dwThreadId == GetCurrentThreadId())
260 LeaveCriticalSection( &MSI_handle_cs );
261 MsiCloseHandle( i+1 );
262 EnterCriticalSection( &MSI_handle_cs );
266 LeaveCriticalSection( &MSI_handle_cs );