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 MSIHANDLE alloc_msihandle( MSIOBJECTHDR *obj )
68 EnterCriticalSection( &MSI_handle_cs );
71 for(i=0; i<msihandletable_size; i++)
72 if( !msihandletable[i].obj )
74 if( i==msihandletable_size )
78 if (msihandletable_size == 0)
81 p = msi_alloc_zero(newsize*sizeof(msi_handle_info));
85 newsize = msihandletable_size * 2;
86 p = msi_realloc_zero(msihandletable,
87 newsize*sizeof(msi_handle_info));
92 msihandletable_size = newsize;
96 msihandletable[i].obj = obj;
97 msihandletable[i].dwThreadId = GetCurrentThreadId();
98 ret = (MSIHANDLE) (i+1);
100 TRACE("%p -> %ld\n", obj, ret );
102 LeaveCriticalSection( &MSI_handle_cs );
106 void *msihandle2msiinfo(MSIHANDLE handle, UINT type)
108 MSIOBJECTHDR *ret = NULL;
110 EnterCriticalSection( &MSI_handle_cs );
114 if( handle>=msihandletable_size )
116 if( !msihandletable[handle].obj )
118 if( msihandletable[handle].obj->magic != MSIHANDLE_MAGIC )
120 if( type && (msihandletable[handle].obj->type != type) )
122 ret = msihandletable[handle].obj;
123 msiobj_addref( ret );
126 LeaveCriticalSection( &MSI_handle_cs );
131 void *alloc_msiobject(UINT type, UINT size, msihandledestructor destroy )
135 info = msi_alloc_zero( size );
138 info->magic = MSIHANDLE_MAGIC;
141 info->destructor = destroy;
147 void msiobj_addref( MSIOBJECTHDR *info )
154 if( info->magic != MSIHANDLE_MAGIC )
156 ERR("Invalid handle!\n");
160 InterlockedIncrement(&info->refcount);
163 void msiobj_lock( MSIOBJECTHDR *info )
165 EnterCriticalSection( &MSI_object_cs );
168 void msiobj_unlock( MSIOBJECTHDR *info )
170 LeaveCriticalSection( &MSI_object_cs );
173 int msiobj_release( MSIOBJECTHDR *info )
182 if( info->magic != MSIHANDLE_MAGIC )
184 ERR("Invalid handle!\n");
188 ret = InterlockedDecrement( &info->refcount );
191 if( info->destructor )
192 info->destructor( info );
194 TRACE("object %p destroyed\n", info);
200 /***********************************************************
201 * MsiCloseHandle [MSI.@]
203 UINT WINAPI MsiCloseHandle(MSIHANDLE handle)
206 UINT ret = ERROR_INVALID_HANDLE;
208 TRACE("%lx\n",handle);
211 return ERROR_SUCCESS;
213 EnterCriticalSection( &MSI_handle_cs );
215 info = msihandle2msiinfo(handle, 0);
219 if( info->magic != MSIHANDLE_MAGIC )
221 ERR("Invalid handle!\n");
225 msiobj_release( info );
226 msihandletable[handle-1].obj = NULL;
229 TRACE("handle %lx Destroyed\n", handle);
231 LeaveCriticalSection( &MSI_handle_cs );
233 msiobj_release( info );
238 /***********************************************************
239 * MsiCloseAllHandles [MSI.@]
241 * Closes all handles owned by the current thread
244 * The number of handles closed
246 UINT WINAPI MsiCloseAllHandles(void)
252 EnterCriticalSection( &MSI_handle_cs );
253 for(i=0; i<msihandletable_size; i++)
255 if(msihandletable[i].dwThreadId == GetCurrentThreadId())
257 LeaveCriticalSection( &MSI_handle_cs );
258 MsiCloseHandle( i+1 );
259 EnterCriticalSection( &MSI_handle_cs );
263 LeaveCriticalSection( &MSI_handle_cs );