Treat missing MSI tables as empty.
[wine] / dlls / msi / handle.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2002 Mike McCormack for CodeWeavers
5  *
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.
10  *
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.
15  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22
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"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(msi);
33
34 MSIHANDLEINFO *msihandletable[MSIMAXHANDLES];
35
36 MSIHANDLE alloc_msihandle(UINT type, UINT size, msihandledestructor destroy)
37 {
38     MSIHANDLEINFO *info;
39     UINT i;
40
41     /* find a slot */
42     for(i=0; i<MSIMAXHANDLES; i++)
43         if( !msihandletable[i] )
44             break;
45     if( (i>=MSIMAXHANDLES) || msihandletable[i] )
46         return 0;
47
48     size += sizeof (MSIHANDLEINFO);
49     info = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size );
50     if( !info )
51         return 0;
52
53     info->magic = MSIHANDLE_MAGIC;
54     info->type = type;
55     info->destructor = destroy;
56
57     msihandletable[i] = info;
58
59     return (MSIHANDLE) (i+1);
60 }
61
62 void *msihandle2msiinfo(MSIHANDLE handle, UINT type)
63 {
64     handle--;
65     if( handle<0 )
66         return NULL;
67     if( handle>=MSIMAXHANDLES )
68         return NULL;
69     if( !msihandletable[handle] )
70         return NULL;
71     if( msihandletable[handle]->magic != MSIHANDLE_MAGIC )
72         return NULL;
73     if( type && (msihandletable[handle]->type != type) )
74         return NULL;
75
76     return &msihandletable[handle][1];
77 }
78
79 UINT WINAPI MsiCloseHandle(MSIHANDLE handle)
80 {
81     MSIHANDLEINFO *info = msihandle2msiinfo(handle, 0);
82
83     TRACE("%lx\n",handle);
84
85     if( !info )
86         return ERROR_INVALID_HANDLE;
87
88     info--;
89
90     if( info->magic != MSIHANDLE_MAGIC )
91     {
92         ERR("Invalid handle!\n");
93         return ERROR_INVALID_HANDLE;
94     }
95
96     if( info->destructor )
97         info->destructor( &info[1] );
98
99     HeapFree( GetProcessHeap(), 0, info );
100     msihandletable[handle-1] = NULL;
101
102     TRACE("Destroyed\n");
103
104     return 0;
105 }
106
107 UINT WINAPI MsiCloseAllHandles(void)
108 {
109     UINT i;
110
111     TRACE("\n");
112
113     for(i=0; i<MSIMAXHANDLES; i++)
114         MsiCloseHandle( i+1 );
115
116     return 0;
117 }