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 )
161 if( info->magic != MSIHANDLE_MAGIC )
163 ERR("Invalid handle!\n");
167 InterlockedIncrement(&info->refcount);
170 void msiobj_lock( MSIOBJECTHDR *info )
172 EnterCriticalSection( &MSI_object_cs );
175 void msiobj_unlock( MSIOBJECTHDR *info )
177 LeaveCriticalSection( &MSI_object_cs );
180 int msiobj_release( MSIOBJECTHDR *info )
189 if( info->magic != MSIHANDLE_MAGIC )
191 ERR("Invalid handle!\n");
195 ret = InterlockedDecrement( &info->refcount );
198 if( info->destructor )
199 info->destructor( info );
201 TRACE("object %p destroyed\n", info);
207 /***********************************************************
208 * MsiCloseHandle [MSI.@]
210 UINT WINAPI MsiCloseHandle(MSIHANDLE handle)
213 UINT ret = ERROR_INVALID_HANDLE;
215 TRACE("%lx\n",handle);
218 return ERROR_SUCCESS;
220 EnterCriticalSection( &MSI_handle_cs );
222 info = msihandle2msiinfo(handle, 0);
226 if( info->magic != MSIHANDLE_MAGIC )
228 ERR("Invalid handle!\n");
232 msiobj_release( info );
233 msihandletable[handle-1].obj = NULL;
236 TRACE("handle %lx Destroyed\n", handle);
238 LeaveCriticalSection( &MSI_handle_cs );
240 msiobj_release( info );
245 /***********************************************************
246 * MsiCloseAllHandles [MSI.@]
248 * Closes all handles owned by the current thread
251 * The number of handles closed
253 UINT WINAPI MsiCloseAllHandles(void)
259 EnterCriticalSection( &MSI_handle_cs );
260 for(i=0; i<msihandletable_size; i++)
262 if(msihandletable[i].dwThreadId == GetCurrentThreadId())
264 LeaveCriticalSection( &MSI_handle_cs );
265 MsiCloseHandle( i+1 );
266 EnterCriticalSection( &MSI_handle_cs );
270 LeaveCriticalSection( &MSI_handle_cs );