odbccp32/tests: Add missing '\n' to an ok() call.
[wine.git] / dlls / msi / handle.c
blob6af3c4110f5414a3a0f320ece39832cbb6b7488b
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 if( !info )
157 return;
159 if( info->magic != MSIHANDLE_MAGIC )
161 ERR("Invalid handle!\n");
162 return;
165 InterlockedIncrement(&info->refcount);
168 void msiobj_lock( MSIOBJECTHDR *info )
170 EnterCriticalSection( &MSI_object_cs );
173 void msiobj_unlock( MSIOBJECTHDR *info )
175 LeaveCriticalSection( &MSI_object_cs );
178 int msiobj_release( MSIOBJECTHDR *info )
180 int ret;
182 if( !info )
183 return -1;
185 if( info->magic != MSIHANDLE_MAGIC )
187 ERR("Invalid handle!\n");
188 return -1;
191 ret = InterlockedDecrement( &info->refcount );
192 if( ret==0 )
194 if( info->destructor )
195 info->destructor( info );
196 msi_free( info );
197 TRACE("object %p destroyed\n", info);
200 return ret;
203 /***********************************************************
204 * MsiCloseHandle [MSI.@]
206 UINT WINAPI MsiCloseHandle(MSIHANDLE handle)
208 MSIOBJECTHDR *info;
209 UINT ret = ERROR_INVALID_HANDLE;
211 TRACE("%lx\n",handle);
213 if (!handle)
214 return ERROR_SUCCESS;
216 EnterCriticalSection( &MSI_handle_cs );
218 info = msihandle2msiinfo(handle, 0);
219 if( !info )
220 goto out;
222 if( info->magic != MSIHANDLE_MAGIC )
224 ERR("Invalid handle!\n");
225 goto out;
228 msiobj_release( info );
229 msihandletable[handle-1].obj = NULL;
230 ret = ERROR_SUCCESS;
232 TRACE("handle %lx Destroyed\n", handle);
233 out:
234 LeaveCriticalSection( &MSI_handle_cs );
235 if( info )
236 msiobj_release( info );
238 return ret;
241 /***********************************************************
242 * MsiCloseAllHandles [MSI.@]
244 * Closes all handles owned by the current thread
246 * RETURNS:
247 * The number of handles closed
249 UINT WINAPI MsiCloseAllHandles(void)
251 UINT i, n=0;
253 TRACE("\n");
255 EnterCriticalSection( &MSI_handle_cs );
256 for(i=0; i<msihandletable_size; i++)
258 if(msihandletable[i].dwThreadId == GetCurrentThreadId())
260 LeaveCriticalSection( &MSI_handle_cs );
261 MsiCloseHandle( i+1 );
262 EnterCriticalSection( &MSI_handle_cs );
263 n++;
266 LeaveCriticalSection( &MSI_handle_cs );
268 return n;