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
23 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
25 static CRITICAL_SECTION MSI_handle_cs
;
26 static CRITICAL_SECTION_DEBUG MSI_handle_cs_debug
=
29 { &MSI_handle_cs_debug
.ProcessLocksList
,
30 &MSI_handle_cs_debug
.ProcessLocksList
},
31 0, 0, { (DWORD_PTR
)(__FILE__
": MSI_handle_cs") }
33 static CRITICAL_SECTION MSI_handle_cs
= { &MSI_handle_cs_debug
, -1, 0, 0, 0, 0 };
35 static CRITICAL_SECTION MSI_object_cs
;
36 static CRITICAL_SECTION_DEBUG MSI_object_cs_debug
=
39 { &MSI_object_cs_debug
.ProcessLocksList
,
40 &MSI_object_cs_debug
.ProcessLocksList
},
41 0, 0, { (DWORD_PTR
)(__FILE__
": MSI_object_cs") }
43 static CRITICAL_SECTION MSI_object_cs
= { &MSI_object_cs_debug
, -1, 0, 0, 0, 0 };
45 typedef struct msi_handle_info_t
55 static msi_handle_info
*msihandletable
= NULL
;
56 static unsigned int msihandletable_size
= 0;
58 void msi_free_handle_table(void)
60 msi_free( msihandletable
);
61 msihandletable
= NULL
;
62 msihandletable_size
= 0;
63 DeleteCriticalSection(&MSI_handle_cs
);
64 DeleteCriticalSection(&MSI_object_cs
);
67 static MSIHANDLE
alloc_handle_table_entry(void)
72 for(i
=0; i
<msihandletable_size
; i
++)
73 if( !msihandletable
[i
].u
.obj
&& !msihandletable
[i
].u
.unk
)
75 if( i
==msihandletable_size
)
79 if (msihandletable_size
== 0)
82 p
= msi_alloc_zero(newsize
*sizeof(msi_handle_info
));
86 newsize
= msihandletable_size
* 2;
87 p
= msi_realloc_zero(msihandletable
,
88 newsize
*sizeof(msi_handle_info
));
93 msihandletable_size
= newsize
;
98 MSIHANDLE
alloc_msihandle( MSIOBJECTHDR
*obj
)
100 msi_handle_info
*entry
;
103 EnterCriticalSection( &MSI_handle_cs
);
105 ret
= alloc_handle_table_entry();
108 entry
= &msihandletable
[ ret
- 1 ];
109 msiobj_addref( obj
);
111 entry
->dwThreadId
= GetCurrentThreadId();
112 entry
->remote
= FALSE
;
115 LeaveCriticalSection( &MSI_handle_cs
);
117 TRACE("%p -> %d\n", obj
, ret
);
122 MSIHANDLE
alloc_msi_remote_handle( IUnknown
*unk
)
124 msi_handle_info
*entry
;
127 EnterCriticalSection( &MSI_handle_cs
);
129 ret
= alloc_handle_table_entry();
132 entry
= &msihandletable
[ ret
- 1 ];
133 IUnknown_AddRef( unk
);
135 entry
->dwThreadId
= GetCurrentThreadId();
136 entry
->remote
= TRUE
;
139 LeaveCriticalSection( &MSI_handle_cs
);
141 TRACE("%p -> %d\n", unk
, ret
);
146 void *msihandle2msiinfo(MSIHANDLE handle
, UINT type
)
148 MSIOBJECTHDR
*ret
= NULL
;
150 EnterCriticalSection( &MSI_handle_cs
);
152 if( handle
>= msihandletable_size
)
154 if( msihandletable
[handle
].remote
)
156 if( !msihandletable
[handle
].u
.obj
)
158 if( msihandletable
[handle
].u
.obj
->magic
!= MSIHANDLE_MAGIC
)
160 if( type
&& (msihandletable
[handle
].u
.obj
->type
!= type
) )
162 ret
= msihandletable
[handle
].u
.obj
;
163 msiobj_addref( ret
);
166 LeaveCriticalSection( &MSI_handle_cs
);
171 IUnknown
*msi_get_remote( MSIHANDLE handle
)
173 IUnknown
*unk
= NULL
;
175 EnterCriticalSection( &MSI_handle_cs
);
177 if( handle
>=msihandletable_size
)
179 if( !msihandletable
[handle
].remote
)
181 unk
= msihandletable
[handle
].u
.unk
;
183 IUnknown_AddRef( unk
);
186 LeaveCriticalSection( &MSI_handle_cs
);
191 void *alloc_msiobject(UINT type
, UINT size
, msihandledestructor destroy
)
195 info
= msi_alloc_zero( size
);
198 info
->magic
= MSIHANDLE_MAGIC
;
201 info
->destructor
= destroy
;
207 void msiobj_addref( MSIOBJECTHDR
*info
)
212 if( info
->magic
!= MSIHANDLE_MAGIC
)
214 ERR("Invalid handle!\n");
218 InterlockedIncrement(&info
->refcount
);
221 void msiobj_lock( MSIOBJECTHDR
*info
)
223 EnterCriticalSection( &MSI_object_cs
);
226 void msiobj_unlock( MSIOBJECTHDR
*info
)
228 LeaveCriticalSection( &MSI_object_cs
);
231 int msiobj_release( MSIOBJECTHDR
*info
)
238 if( info
->magic
!= MSIHANDLE_MAGIC
)
240 ERR("Invalid handle!\n");
244 ret
= InterlockedDecrement( &info
->refcount
);
247 if( info
->destructor
)
248 info
->destructor( info
);
250 TRACE("object %p destroyed\n", info
);
256 /***********************************************************
257 * MsiCloseHandle [MSI.@]
259 UINT WINAPI
MsiCloseHandle(MSIHANDLE handle
)
261 MSIOBJECTHDR
*info
= NULL
;
262 UINT ret
= ERROR_INVALID_HANDLE
;
264 TRACE("%x\n",handle
);
267 return ERROR_SUCCESS
;
269 EnterCriticalSection( &MSI_handle_cs
);
272 if (handle
>= msihandletable_size
)
275 if (msihandletable
[handle
].remote
)
277 IUnknown_Release( msihandletable
[handle
].u
.unk
);
281 info
= msihandletable
[handle
].u
.obj
;
285 if( info
->magic
!= MSIHANDLE_MAGIC
)
287 ERR("Invalid handle!\n");
292 msihandletable
[handle
].u
.obj
= NULL
;
293 msihandletable
[handle
].remote
= 0;
294 msihandletable
[handle
].dwThreadId
= 0;
298 TRACE("handle %x destroyed\n", handle
+1);
300 LeaveCriticalSection( &MSI_handle_cs
);
302 msiobj_release( info
);
307 /***********************************************************
308 * MsiCloseAllHandles [MSI.@]
310 * Closes all handles owned by the current thread
313 * The number of handles closed
315 UINT WINAPI
MsiCloseAllHandles(void)
321 EnterCriticalSection( &MSI_handle_cs
);
322 for(i
=0; i
<msihandletable_size
; i
++)
324 if(msihandletable
[i
].dwThreadId
== GetCurrentThreadId())
326 LeaveCriticalSection( &MSI_handle_cs
);
327 MsiCloseHandle( i
+1 );
328 EnterCriticalSection( &MSI_handle_cs
);
332 LeaveCriticalSection( &MSI_handle_cs
);