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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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, { 0, (DWORD)(__FILE__ ": MSI_handle_cs") }
42 static CRITICAL_SECTION MSI_handle_cs = { &MSI_handle_cs_debug, -1, 0, 0, 0, 0 };
44 MSIOBJECTHDR *msihandletable[MSIMAXHANDLES];
46 MSIHANDLE alloc_msihandle( MSIOBJECTHDR *obj )
51 EnterCriticalSection( &MSI_handle_cs );
54 for(i=0; i<MSIMAXHANDLES; i++)
55 if( !msihandletable[i] )
57 if( (i>=MSIMAXHANDLES) || msihandletable[i] )
61 msihandletable[i] = obj;
62 ret = (MSIHANDLE) (i+1);
64 TRACE("%p -> %ld\n", obj, ret );
66 LeaveCriticalSection( &MSI_handle_cs );
70 void *msihandle2msiinfo(MSIHANDLE handle, UINT type)
72 MSIOBJECTHDR *ret = NULL;
74 EnterCriticalSection( &MSI_handle_cs );
78 if( handle>=MSIMAXHANDLES )
80 if( !msihandletable[handle] )
82 if( msihandletable[handle]->magic != MSIHANDLE_MAGIC )
84 if( type && (msihandletable[handle]->type != type) )
86 ret = msihandletable[handle];
90 LeaveCriticalSection( &MSI_handle_cs );
95 MSIHANDLE msiobj_findhandle( MSIOBJECTHDR *hdr )
102 EnterCriticalSection( &MSI_handle_cs );
103 for(i=0; (i<MSIMAXHANDLES) && !ret; i++)
104 if( msihandletable[i] == hdr )
106 LeaveCriticalSection( &MSI_handle_cs );
108 TRACE("%p -> %ld\n", hdr, ret);
110 msiobj_addref( hdr );
114 void *alloc_msiobject(UINT type, UINT size, msihandledestructor destroy )
118 info = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size );
121 info->magic = MSIHANDLE_MAGIC;
124 info->destructor = destroy;
130 void msiobj_addref( MSIOBJECTHDR *info )
137 if( info->magic != MSIHANDLE_MAGIC )
139 ERR("Invalid handle!\n");
146 int msiobj_release( MSIOBJECTHDR *info )
155 if( info->magic != MSIHANDLE_MAGIC )
157 ERR("Invalid handle!\n");
161 ret = info->refcount--;
162 if (info->refcount == 0)
164 if( info->destructor )
165 info->destructor( info );
166 HeapFree( GetProcessHeap(), 0, info );
167 TRACE("object %p destroyed\n", info);
173 UINT WINAPI MsiCloseHandle(MSIHANDLE handle)
176 UINT ret = ERROR_INVALID_HANDLE;
178 TRACE("%lx\n",handle);
180 EnterCriticalSection( &MSI_handle_cs );
182 info = msihandle2msiinfo(handle, 0);
186 if( info->magic != MSIHANDLE_MAGIC )
188 ERR("Invalid handle!\n");
192 msiobj_release( info );
193 msihandletable[handle-1] = NULL;
196 TRACE("handle %lx Destroyed\n", handle);
198 LeaveCriticalSection( &MSI_handle_cs );
200 msiobj_release( info );
205 UINT WINAPI MsiCloseAllHandles(void)
211 for(i=0; i<MSIMAXHANDLES; i++)
212 MsiCloseHandle( i+1 );