dbghelp: Dwarf compiland.
[wine/multimedia.git] / dlls / msi / handle.c
blob1cd82b37d2d23341f8483d65bd6816b56417af21
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[MSIMAXHANDLES];
62 MSIHANDLE alloc_msihandle( MSIOBJECTHDR *obj )
64 MSIHANDLE ret = 0;
65 UINT i;
67 EnterCriticalSection( &MSI_handle_cs );
69 /* find a slot */
70 for(i=0; i<MSIMAXHANDLES; i++)
71 if( !msihandletable[i].obj )
72 break;
73 if( (i>=MSIMAXHANDLES) || msihandletable[i].obj )
74 goto out;
76 msiobj_addref( obj );
77 msihandletable[i].obj = obj;
78 msihandletable[i].dwThreadId = GetCurrentThreadId();
79 ret = (MSIHANDLE) (i+1);
80 out:
81 TRACE("%p -> %ld\n", obj, ret );
83 LeaveCriticalSection( &MSI_handle_cs );
84 return ret;
87 void *msihandle2msiinfo(MSIHANDLE handle, UINT type)
89 MSIOBJECTHDR *ret = NULL;
91 EnterCriticalSection( &MSI_handle_cs );
92 handle--;
93 if( handle<0 )
94 goto out;
95 if( handle>=MSIMAXHANDLES )
96 goto out;
97 if( !msihandletable[handle].obj )
98 goto out;
99 if( msihandletable[handle].obj->magic != MSIHANDLE_MAGIC )
100 goto out;
101 if( type && (msihandletable[handle].obj->type != type) )
102 goto out;
103 ret = msihandletable[handle].obj;
104 msiobj_addref( ret );
106 out:
107 LeaveCriticalSection( &MSI_handle_cs );
109 return (void*) ret;
112 void *alloc_msiobject(UINT type, UINT size, msihandledestructor destroy )
114 MSIOBJECTHDR *info;
116 info = msi_alloc_zero( size );
117 if( info )
119 info->magic = MSIHANDLE_MAGIC;
120 info->type = type;
121 info->refcount = 1;
122 info->destructor = destroy;
125 return info;
128 void msiobj_addref( MSIOBJECTHDR *info )
130 TRACE("%p\n", info);
132 if( !info )
133 return;
135 if( info->magic != MSIHANDLE_MAGIC )
137 ERR("Invalid handle!\n");
138 return;
141 InterlockedIncrement(&info->refcount);
144 void msiobj_lock( MSIOBJECTHDR *info )
146 EnterCriticalSection( &MSI_object_cs );
149 void msiobj_unlock( MSIOBJECTHDR *info )
151 LeaveCriticalSection( &MSI_object_cs );
154 int msiobj_release( MSIOBJECTHDR *info )
156 int ret;
158 TRACE("%p\n",info);
160 if( !info )
161 return -1;
163 if( info->magic != MSIHANDLE_MAGIC )
165 ERR("Invalid handle!\n");
166 return -1;
169 ret = InterlockedDecrement( &info->refcount );
170 if( ret==0 )
172 if( info->destructor )
173 info->destructor( info );
174 msi_free( info );
175 TRACE("object %p destroyed\n", info);
178 return ret;
181 /***********************************************************
182 * MsiCloseHandle [MSI.@]
184 UINT WINAPI MsiCloseHandle(MSIHANDLE handle)
186 MSIOBJECTHDR *info;
187 UINT ret = ERROR_INVALID_HANDLE;
189 TRACE("%lx\n",handle);
191 if (!handle)
192 return ERROR_SUCCESS;
194 EnterCriticalSection( &MSI_handle_cs );
196 info = msihandle2msiinfo(handle, 0);
197 if( !info )
198 goto out;
200 if( info->magic != MSIHANDLE_MAGIC )
202 ERR("Invalid handle!\n");
203 goto out;
206 msiobj_release( info );
207 msihandletable[handle-1].obj = NULL;
208 ret = ERROR_SUCCESS;
210 TRACE("handle %lx Destroyed\n", handle);
211 out:
212 LeaveCriticalSection( &MSI_handle_cs );
213 if( info )
214 msiobj_release( info );
216 return ret;
219 /***********************************************************
220 * MsiCloseAllHandles [MSI.@]
222 * Closes all handles owned by the current thread
224 * RETURNS:
225 * The number of handles closed
227 UINT WINAPI MsiCloseAllHandles(void)
229 UINT i, n=0;
231 TRACE("\n");
233 for(i=0; i<MSIMAXHANDLES; i++)
235 if(msihandletable[i].dwThreadId == GetCurrentThreadId())
237 MsiCloseHandle( i+1 );
238 n++;
242 return n;