handle.c 7.76 KB
Newer Older
1 2 3
/*
 * Implementation of the Microsoft Installer (msi.dll)
 *
4
 * Copyright 2002-2004 Mike McCormack for CodeWeavers
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
19 20
 */

21 22
#define COBJMACROS

23 24
#include <stdarg.h>

25 26 27 28 29 30 31 32 33 34 35
#include "windef.h"
#include "winbase.h"
#include "winreg.h"
#include "shlwapi.h"
#include "wine/debug.h"
#include "msi.h"
#include "msiquery.h"
#include "msipriv.h"

WINE_DEFAULT_DEBUG_CHANNEL(msi);

36 37 38 39 40 41
static CRITICAL_SECTION MSI_handle_cs;
static CRITICAL_SECTION_DEBUG MSI_handle_cs_debug =
{
    0, 0, &MSI_handle_cs,
    { &MSI_handle_cs_debug.ProcessLocksList, 
      &MSI_handle_cs_debug.ProcessLocksList },
42
      0, 0, { (DWORD_PTR)(__FILE__ ": MSI_handle_cs") }
43 44 45
};
static CRITICAL_SECTION MSI_handle_cs = { &MSI_handle_cs_debug, -1, 0, 0, 0, 0 };

46 47 48 49 50 51
static CRITICAL_SECTION MSI_object_cs;
static CRITICAL_SECTION_DEBUG MSI_object_cs_debug =
{
    0, 0, &MSI_object_cs,
    { &MSI_object_cs_debug.ProcessLocksList, 
      &MSI_object_cs_debug.ProcessLocksList },
52
      0, 0, { (DWORD_PTR)(__FILE__ ": MSI_object_cs") }
53 54 55
};
static CRITICAL_SECTION MSI_object_cs = { &MSI_object_cs_debug, -1, 0, 0, 0, 0 };

56 57
typedef struct msi_handle_info_t
{
58 59 60 61 62
    BOOL remote;
    union {
        MSIOBJECTHDR *obj;
        IUnknown *unk;
    } u;
63 64 65
    DWORD dwThreadId;
} msi_handle_info;

66
static msi_handle_info *msihandletable = NULL;
67
static unsigned int msihandletable_size = 0;
68

69 70 71 72 73
void msi_free_handle_table(void)
{
    msi_free( msihandletable );
    msihandletable = NULL;
    msihandletable_size = 0;
74 75
    DeleteCriticalSection(&MSI_handle_cs);
    DeleteCriticalSection(&MSI_object_cs);
76 77
}

78
static MSIHANDLE alloc_handle_table_entry(void)
79 80 81 82
{
    UINT i;

    /* find a slot */
83
    for(i=0; i<msihandletable_size; i++)
84
        if( !msihandletable[i].u.obj && !msihandletable[i].u.unk )
85
            break;
86 87 88 89 90 91 92 93 94
    if( i==msihandletable_size )
    {
        msi_handle_info *p;
        int newsize;
        if (msihandletable_size == 0)
        {
            newsize = 256;
            p = msi_alloc_zero(newsize*sizeof(msi_handle_info));
        }
95 96
        else
        {
97 98 99
            newsize = msihandletable_size * 2;
            p = msi_realloc_zero(msihandletable,
                            newsize*sizeof(msi_handle_info));
100
        }
101
        if (!p)
102
            return 0;
103 104 105
        msihandletable = p;
        msihandletable_size = newsize;
    }
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    return i + 1;
}

MSIHANDLE alloc_msihandle( MSIOBJECTHDR *obj )
{
    msi_handle_info *entry;
    MSIHANDLE ret;

    EnterCriticalSection( &MSI_handle_cs );

    ret = alloc_handle_table_entry();
    if (ret)
    {
        entry = &msihandletable[ ret - 1 ];
        msiobj_addref( obj );
        entry->u.obj = obj;
        entry->dwThreadId = GetCurrentThreadId();
        entry->remote = FALSE;
    }

    LeaveCriticalSection( &MSI_handle_cs );
127

128
    TRACE("%p -> %d\n", obj, ret );
129

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    return ret;
}

MSIHANDLE alloc_msi_remote_handle( IUnknown *unk )
{
    msi_handle_info *entry;
    MSIHANDLE ret;

    EnterCriticalSection( &MSI_handle_cs );

    ret = alloc_handle_table_entry();
    if (ret)
    {
        entry = &msihandletable[ ret - 1 ];
        IUnknown_AddRef( unk );
        entry->u.unk = unk;
        entry->dwThreadId = GetCurrentThreadId();
        entry->remote = TRUE;
    }

150
    LeaveCriticalSection( &MSI_handle_cs );
151

152
    TRACE("%p -> %d\n", unk, ret);
153

154
    return ret;
155 156 157 158
}

void *msihandle2msiinfo(MSIHANDLE handle, UINT type)
{
159 160 161
    MSIOBJECTHDR *ret = NULL;

    EnterCriticalSection( &MSI_handle_cs );
162
    handle--;
163
    if( handle >= msihandletable_size )
164
        goto out;
165
    if( msihandletable[handle].remote)
166
        goto out;
167
    if( !msihandletable[handle].u.obj )
168
        goto out;
169
    if( msihandletable[handle].u.obj->magic != MSIHANDLE_MAGIC )
170
        goto out;
171 172 173
    if( type && (msihandletable[handle].u.obj->type != type) )
        goto out;
    ret = msihandletable[handle].u.obj;
174
    msiobj_addref( ret );
175

176 177 178
out:
    LeaveCriticalSection( &MSI_handle_cs );

179
    return ret;
180 181
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
IUnknown *msi_get_remote( MSIHANDLE handle )
{
    IUnknown *unk = NULL;

    EnterCriticalSection( &MSI_handle_cs );
    handle--;
    if( handle>=msihandletable_size )
        goto out;
    if( !msihandletable[handle].remote)
        goto out;
    unk = msihandletable[handle].u.unk;
    if( unk )
        IUnknown_AddRef( unk );

out:
    LeaveCriticalSection( &MSI_handle_cs );

    return unk;
}

202
void *alloc_msiobject(UINT type, UINT size, msihandledestructor destroy )
203
{
204
    MSIOBJECTHDR *info;
205

206
    info = msi_alloc_zero( size );
207 208 209 210 211 212 213 214 215 216 217 218 219
    if( info )
    {
        info->magic = MSIHANDLE_MAGIC;
        info->type = type;
        info->refcount = 1;
        info->destructor = destroy;
    }

    return info;
}

void msiobj_addref( MSIOBJECTHDR *info )
{
220 221 222 223 224 225 226 227 228
    if( !info )
        return;

    if( info->magic != MSIHANDLE_MAGIC )
    {
        ERR("Invalid handle!\n");
        return;
    }

229
    InterlockedIncrement(&info->refcount);
230 231
}

232 233 234 235 236 237 238 239 240 241
void msiobj_lock( MSIOBJECTHDR *info )
{
    EnterCriticalSection( &MSI_object_cs );
}

void msiobj_unlock( MSIOBJECTHDR *info )
{
    LeaveCriticalSection( &MSI_object_cs );
}

242
int msiobj_release( MSIOBJECTHDR *info )
243
{
244
    int ret;
245 246

    if( !info )
247
        return -1;
248 249 250 251

    if( info->magic != MSIHANDLE_MAGIC )
    {
        ERR("Invalid handle!\n");
252
        return -1;
253 254
    }

255 256
    ret = InterlockedDecrement( &info->refcount );
    if( ret==0 )
257
    {
258
        if( info->destructor )
259
            info->destructor( info );
260
        msi_free( info );
261 262 263 264 265 266
        TRACE("object %p destroyed\n", info);
    }

    return ret;
}

267 268 269
/***********************************************************
 *   MsiCloseHandle   [MSI.@]
 */
270 271
UINT WINAPI MsiCloseHandle(MSIHANDLE handle)
{
272
    MSIOBJECTHDR *info = NULL;
273 274
    UINT ret = ERROR_INVALID_HANDLE;

275
    TRACE("%x\n",handle);
276

277 278 279
    if (!handle)
        return ERROR_SUCCESS;

280 281
    EnterCriticalSection( &MSI_handle_cs );

282 283 284 285 286
    handle--;
    if (handle >= msihandletable_size)
        goto out;

    if (msihandletable[handle].remote)
287
    {
288
        IUnknown_Release( msihandletable[handle].u.unk );
289
    }
290 291
    else
    {
292
        info = msihandletable[handle].u.obj;
293 294 295 296 297 298 299 300 301 302
        if( !info )
            goto out;

        if( info->magic != MSIHANDLE_MAGIC )
        {
            ERR("Invalid handle!\n");
            goto out;
        }
    }

303 304 305
    msihandletable[handle].u.obj = NULL;
    msihandletable[handle].remote = 0;
    msihandletable[handle].dwThreadId = 0;
306 307

    ret = ERROR_SUCCESS;
308

309
    TRACE("handle %x destroyed\n", handle+1);
310 311 312 313
out:
    LeaveCriticalSection( &MSI_handle_cs );
    if( info )
        msiobj_release( info );
314

315
    return ret;
316 317
}

318 319 320 321 322 323 324 325
/***********************************************************
 *   MsiCloseAllHandles   [MSI.@]
 *
 *  Closes all handles owned by the current thread
 *
 *  RETURNS:
 *   The number of handles closed
 */
326 327
UINT WINAPI MsiCloseAllHandles(void)
{
328
    UINT i, n=0;
329 330 331

    TRACE("\n");

332 333
    EnterCriticalSection( &MSI_handle_cs );
    for(i=0; i<msihandletable_size; i++)
334 335 336
    {
        if(msihandletable[i].dwThreadId == GetCurrentThreadId())
        {
337
            LeaveCriticalSection( &MSI_handle_cs );
338
            MsiCloseHandle( i+1 );
339
            EnterCriticalSection( &MSI_handle_cs );
340 341 342
            n++;
        }
    }
343
    LeaveCriticalSection( &MSI_handle_cs );
344

345
    return n;
346
}