gdi: Fix test for GetGlyphIndicesW on non Unicode systems.
[wine/hacks.git] / dlls / msi / handle.c
blob0193277c875e0cfbaf4943085ef28ae7b88114e6
1 /*
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
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winreg.h"
26 #include "shlwapi.h"
27 #include "wine/debug.h"
28 #include "msi.h"
29 #include "msiquery.h"
30 #include "msipriv.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(msi);
34 static CRITICAL_SECTION MSI_handle_cs;
35 static CRITICAL_SECTION_DEBUG MSI_handle_cs_debug =
37 0, 0, &MSI_handle_cs,
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 =
47 0, 0, &MSI_object_cs,
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
56 MSIOBJECTHDR *obj;
57 DWORD dwThreadId;
58 } msi_handle_info;
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 )
72 MSIHANDLE ret = 0;
73 UINT i;
75 EnterCriticalSection( &MSI_handle_cs );
77 /* find a slot */
78 for(i=0; i<msihandletable_size; i++)
79 if( !msihandletable[i].obj )
80 break;
81 if( i==msihandletable_size )
83 msi_handle_info *p;
84 int newsize;
85 if (msihandletable_size == 0)
87 newsize = 256;
88 p = msi_alloc_zero(newsize*sizeof(msi_handle_info));
90 else
92 newsize = msihandletable_size * 2;
93 p = msi_realloc_zero(msihandletable,
94 newsize*sizeof(msi_handle_info));
96 if (!p)
97 goto out;
98 msihandletable = p;
99 msihandletable_size = newsize;
102 msiobj_addref( obj );
103 msihandletable[i].obj = obj;
104 msihandletable[i].dwThreadId = GetCurrentThreadId();
105 ret = (MSIHANDLE) (i+1);
106 out:
107 TRACE("%p -> %ld\n", obj, ret );
109 LeaveCriticalSection( &MSI_handle_cs );
110 return ret;
113 void *msihandle2msiinfo(MSIHANDLE handle, UINT type)
115 MSIOBJECTHDR *ret = NULL;
117 EnterCriticalSection( &MSI_handle_cs );
118 handle--;
119 if( handle<0 )
120 goto out;
121 if( handle>=msihandletable_size )
122 goto out;
123 if( !msihandletable[handle].obj )
124 goto out;
125 if( msihandletable[handle].obj->magic != MSIHANDLE_MAGIC )
126 goto out;
127 if( type && (msihandletable[handle].obj->type != type) )
128 goto out;
129 ret = msihandletable[handle].obj;
130 msiobj_addref( ret );
132 out:
133 LeaveCriticalSection( &MSI_handle_cs );
135 return (void*) ret;
138 void *alloc_msiobject(UINT type, UINT size, msihandledestructor destroy )
140 MSIOBJECTHDR *info;
142 info = msi_alloc_zero( size );
143 if( info )
145 info->magic = MSIHANDLE_MAGIC;
146 info->type = type;
147 info->refcount = 1;
148 info->destructor = destroy;
151 return info;
154 void msiobj_addref( MSIOBJECTHDR *info )
156 TRACE("%p\n", info);
158 if( !info )
159 return;
161 if( info->magic != MSIHANDLE_MAGIC )
163 ERR("Invalid handle!\n");
164 return;
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 )
182 int ret;
184 TRACE("%p\n",info);
186 if( !info )
187 return -1;
189 if( info->magic != MSIHANDLE_MAGIC )
191 ERR("Invalid handle!\n");
192 return -1;
195 ret = InterlockedDecrement( &info->refcount );
196 if( ret==0 )
198 if( info->destructor )
199 info->destructor( info );
200 msi_free( info );
201 TRACE("object %p destroyed\n", info);
204 return ret;
207 /***********************************************************
208 * MsiCloseHandle [MSI.@]
210 UINT WINAPI MsiCloseHandle(MSIHANDLE handle)
212 MSIOBJECTHDR *info;
213 UINT ret = ERROR_INVALID_HANDLE;
215 TRACE("%lx\n",handle);
217 if (!handle)
218 return ERROR_SUCCESS;
220 EnterCriticalSection( &MSI_handle_cs );
222 info = msihandle2msiinfo(handle, 0);
223 if( !info )
224 goto out;
226 if( info->magic != MSIHANDLE_MAGIC )
228 ERR("Invalid handle!\n");
229 goto out;
232 msiobj_release( info );
233 msihandletable[handle-1].obj = NULL;
234 ret = ERROR_SUCCESS;
236 TRACE("handle %lx Destroyed\n", handle);
237 out:
238 LeaveCriticalSection( &MSI_handle_cs );
239 if( info )
240 msiobj_release( info );
242 return ret;
245 /***********************************************************
246 * MsiCloseAllHandles [MSI.@]
248 * Closes all handles owned by the current thread
250 * RETURNS:
251 * The number of handles closed
253 UINT WINAPI MsiCloseAllHandles(void)
255 UINT i, n=0;
257 TRACE("\n");
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 );
267 n++;
270 LeaveCriticalSection( &MSI_handle_cs );
272 return n;