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"
36 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
38 static CRITICAL_SECTION MSI_handle_cs
;
39 static CRITICAL_SECTION_DEBUG MSI_handle_cs_debug
=
42 { &MSI_handle_cs_debug
.ProcessLocksList
,
43 &MSI_handle_cs_debug
.ProcessLocksList
},
44 0, 0, { (DWORD_PTR
)(__FILE__
": MSI_handle_cs") }
46 static CRITICAL_SECTION MSI_handle_cs
= { &MSI_handle_cs_debug
, -1, 0, 0, 0, 0 };
48 static CRITICAL_SECTION MSI_object_cs
;
49 static CRITICAL_SECTION_DEBUG MSI_object_cs_debug
=
52 { &MSI_object_cs_debug
.ProcessLocksList
,
53 &MSI_object_cs_debug
.ProcessLocksList
},
54 0, 0, { (DWORD_PTR
)(__FILE__
": MSI_object_cs") }
56 static CRITICAL_SECTION MSI_object_cs
= { &MSI_object_cs_debug
, -1, 0, 0, 0, 0 };
58 typedef struct msi_handle_info_t
68 static msi_handle_info
*msihandletable
= NULL
;
69 static unsigned int msihandletable_size
= 0;
71 void msi_free_handle_table(void)
73 msi_free( msihandletable
);
74 msihandletable
= NULL
;
75 msihandletable_size
= 0;
76 DeleteCriticalSection(&MSI_handle_cs
);
77 DeleteCriticalSection(&MSI_object_cs
);
80 static MSIHANDLE
alloc_handle_table_entry(void)
85 for(i
=0; i
<msihandletable_size
; i
++)
86 if( !msihandletable
[i
].u
.obj
&& !msihandletable
[i
].u
.rem
)
88 if( i
==msihandletable_size
)
92 if (msihandletable_size
== 0)
95 p
= msi_alloc_zero(newsize
*sizeof(msi_handle_info
));
99 newsize
= msihandletable_size
* 2;
100 p
= msi_realloc_zero(msihandletable
,
101 newsize
*sizeof(msi_handle_info
));
106 msihandletable_size
= newsize
;
111 MSIHANDLE
alloc_msihandle( MSIOBJECTHDR
*obj
)
113 msi_handle_info
*entry
;
116 EnterCriticalSection( &MSI_handle_cs
);
118 ret
= alloc_handle_table_entry();
121 entry
= &msihandletable
[ ret
- 1 ];
122 msiobj_addref( obj
);
124 entry
->dwThreadId
= GetCurrentThreadId();
125 entry
->remote
= FALSE
;
128 LeaveCriticalSection( &MSI_handle_cs
);
130 TRACE("%p -> %d\n", obj
, ret
);
135 MSIHANDLE
alloc_msi_remote_handle(MSIHANDLE remote
)
137 msi_handle_info
*entry
;
140 EnterCriticalSection( &MSI_handle_cs
);
142 ret
= alloc_handle_table_entry();
145 entry
= &msihandletable
[ ret
- 1 ];
146 entry
->u
.rem
= remote
;
147 entry
->dwThreadId
= GetCurrentThreadId();
148 entry
->remote
= TRUE
;
151 LeaveCriticalSection( &MSI_handle_cs
);
153 TRACE("%d -> %d\n", remote
, ret
);
158 void *msihandle2msiinfo(MSIHANDLE handle
, UINT type
)
160 MSIOBJECTHDR
*ret
= NULL
;
162 EnterCriticalSection( &MSI_handle_cs
);
164 if( handle
>= msihandletable_size
)
166 if( msihandletable
[handle
].remote
)
168 if( !msihandletable
[handle
].u
.obj
)
170 if( msihandletable
[handle
].u
.obj
->magic
!= MSIHANDLE_MAGIC
)
172 if( type
&& (msihandletable
[handle
].u
.obj
->type
!= type
) )
174 ret
= msihandletable
[handle
].u
.obj
;
175 msiobj_addref( ret
);
178 LeaveCriticalSection( &MSI_handle_cs
);
183 MSIHANDLE
msi_get_remote( MSIHANDLE handle
)
187 EnterCriticalSection( &MSI_handle_cs
);
189 if( handle
>=msihandletable_size
)
191 if( !msihandletable
[handle
].remote
)
193 ret
= msihandletable
[handle
].u
.rem
;
196 LeaveCriticalSection( &MSI_handle_cs
);
201 void *alloc_msiobject(UINT type
, UINT size
, msihandledestructor destroy
)
205 info
= msi_alloc_zero( size
);
208 info
->magic
= MSIHANDLE_MAGIC
;
211 info
->destructor
= destroy
;
217 void msiobj_addref( MSIOBJECTHDR
*info
)
222 if( info
->magic
!= MSIHANDLE_MAGIC
)
224 ERR("Invalid handle!\n");
228 InterlockedIncrement(&info
->refcount
);
231 void msiobj_lock( MSIOBJECTHDR
*info
)
233 EnterCriticalSection( &MSI_object_cs
);
236 void msiobj_unlock( MSIOBJECTHDR
*info
)
238 LeaveCriticalSection( &MSI_object_cs
);
241 int msiobj_release( MSIOBJECTHDR
*info
)
248 if( info
->magic
!= MSIHANDLE_MAGIC
)
250 ERR("Invalid handle!\n");
254 ret
= InterlockedDecrement( &info
->refcount
);
257 if( info
->destructor
)
258 info
->destructor( info
);
260 TRACE("object %p destroyed\n", info
);
266 /***********************************************************
267 * MsiCloseHandle [MSI.@]
269 UINT WINAPI
MsiCloseHandle(MSIHANDLE handle
)
271 MSIOBJECTHDR
*info
= NULL
;
272 UINT ret
= ERROR_INVALID_HANDLE
;
274 TRACE("%x\n",handle
);
277 return ERROR_SUCCESS
;
279 EnterCriticalSection( &MSI_handle_cs
);
282 if (handle
>= msihandletable_size
)
285 if (msihandletable
[handle
].remote
)
287 remote_CloseHandle( msihandletable
[handle
].u
.rem
);
291 info
= msihandletable
[handle
].u
.obj
;
295 if( info
->magic
!= MSIHANDLE_MAGIC
)
297 ERR("Invalid handle!\n");
302 msihandletable
[handle
].u
.obj
= NULL
;
303 msihandletable
[handle
].remote
= 0;
304 msihandletable
[handle
].dwThreadId
= 0;
308 TRACE("handle %x destroyed\n", handle
+1);
310 LeaveCriticalSection( &MSI_handle_cs
);
312 msiobj_release( info
);
317 /***********************************************************
318 * MsiCloseAllHandles [MSI.@]
320 * Closes all handles owned by the current thread
323 * The number of handles closed
325 UINT WINAPI
MsiCloseAllHandles(void)
331 EnterCriticalSection( &MSI_handle_cs
);
332 for(i
=0; i
<msihandletable_size
; i
++)
334 if(msihandletable
[i
].dwThreadId
== GetCurrentThreadId())
336 LeaveCriticalSection( &MSI_handle_cs
);
337 MsiCloseHandle( i
+1 );
338 EnterCriticalSection( &MSI_handle_cs
);
342 LeaveCriticalSection( &MSI_handle_cs
);
347 UINT __cdecl
s_remote_CloseHandle(MSIHANDLE handle
)
349 return MsiCloseHandle(handle
);