2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002,2003,2004,2005 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
24 #define NONAMELESSUNION
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
37 #include "msiserver.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
43 DEFINE_GUID( CLSID_MsiDatabase, 0x000c1084, 0x0000, 0x0000,
44 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
45 DEFINE_GUID( CLSID_MsiPatch, 0x000c1086, 0x0000, 0x0000,
46 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
51 * An .msi file is a structured storage file.
52 * It contains a number of streams.
53 * A stream for each table in the database.
54 * Two streams for the string table in the database.
55 * Any binary data in a table is a reference to a stream.
58 static VOID MSI_CloseDatabase( MSIOBJECTHDR *arg )
60 MSIDATABASE *db = (MSIDATABASE *) arg;
63 free_cached_tables( db );
64 msi_free_transforms( db );
65 msi_destroy_stringtable( db->strings );
66 IStorage_Release( db->storage );
69 DeleteFileW( db->deletefile );
70 msi_free( db->deletefile );
74 UINT MSI_OpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIDATABASE **pdb)
78 MSIDATABASE *db = NULL;
79 UINT ret = ERROR_FUNCTION_FAILED;
80 LPCWSTR szMode, save_path;
85 static const WCHAR backslash[] = {'\\',0};
86 static WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
88 TRACE("%s %s\n",debugstr_w(szDBPath),debugstr_w(szPersist) );
91 return ERROR_INVALID_PARAMETER;
95 if( HIWORD( szPersist ) )
97 if (!CopyFileW( szDBPath, szPersist, FALSE ))
98 return ERROR_OPEN_FAILED;
100 szDBPath = szPersist;
101 szPersist = MSIDBOPEN_TRANSACT;
105 if( szPersist == MSIDBOPEN_READONLY )
107 r = StgOpenStorage( szDBPath, NULL,
108 STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
110 else if( szPersist == MSIDBOPEN_CREATE || szPersist == MSIDBOPEN_CREATEDIRECT )
112 /* FIXME: MSIDBOPEN_CREATE should case STGM_TRANSACTED flag to be
114 r = StgCreateDocfile( szDBPath,
115 STGM_CREATE|STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, 0, &stg);
116 if( r == ERROR_SUCCESS )
118 IStorage_SetClass( stg, &CLSID_MsiDatabase );
119 /* create the _Tables stream */
120 r = write_stream_data(stg, szTables, NULL, 0, TRUE);
122 r = msi_init_string_table( stg );
126 else if( szPersist == MSIDBOPEN_TRANSACT )
128 /* FIXME: MSIDBOPEN_TRANSACT should case STGM_TRANSACTED flag to be
130 r = StgOpenStorage( szDBPath, NULL,
131 STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
133 else if( szPersist == MSIDBOPEN_DIRECT )
135 r = StgOpenStorage( szDBPath, NULL,
136 STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
140 ERR("unknown flag %p\n",szPersist);
141 return ERROR_INVALID_PARAMETER;
144 if( FAILED( r ) || !stg )
146 FIXME("open failed r = %08x for %s\n", r, debugstr_w(szDBPath));
147 return ERROR_FUNCTION_FAILED;
150 r = IStorage_Stat( stg, &stat, STATFLAG_NONAME );
153 FIXME("Failed to stat storage\n");
157 if ( !IsEqualGUID( &stat.clsid, &CLSID_MsiDatabase ) &&
158 !IsEqualGUID( &stat.clsid, &CLSID_MsiPatch ) )
160 ERR("storage GUID is not a MSI database GUID %s\n",
161 debugstr_guid(&stat.clsid) );
165 db = alloc_msiobject( MSIHANDLETYPE_DATABASE, sizeof (MSIDATABASE),
169 FIXME("Failed to allocate a handle\n");
173 if (!strchrW( save_path, '\\' ))
175 GetCurrentDirectoryW( MAX_PATH, path );
176 lstrcatW( path, backslash );
177 lstrcatW( path, save_path );
180 lstrcpyW( path, save_path );
182 db->path = strdupW( path );
184 if( TRACE_ON( msi ) )
185 enum_stream_names( stg );
190 db->deletefile = strdupW( szDBPath );
192 db->deletefile = NULL;
193 list_init( &db->tables );
194 list_init( &db->transforms );
196 db->strings = msi_load_string_table( stg, &db->bytes_per_strref );
200 msi_table_set_strref( db->bytes_per_strref );
203 msiobj_addref( &db->hdr );
204 IStorage_AddRef( stg );
209 msiobj_release( &db->hdr );
211 IStorage_Release( stg );
216 UINT WINAPI MsiOpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIHANDLE *phDB)
221 TRACE("%s %s %p\n",debugstr_w(szDBPath),debugstr_w(szPersist), phDB);
223 ret = MSI_OpenDatabaseW( szDBPath, szPersist, &db );
224 if( ret == ERROR_SUCCESS )
226 *phDB = alloc_msihandle( &db->hdr );
228 ret = ERROR_NOT_ENOUGH_MEMORY;
229 msiobj_release( &db->hdr );
235 UINT WINAPI MsiOpenDatabaseA(LPCSTR szDBPath, LPCSTR szPersist, MSIHANDLE *phDB)
237 HRESULT r = ERROR_FUNCTION_FAILED;
238 LPWSTR szwDBPath = NULL, szwPersist = NULL;
240 TRACE("%s %s %p\n", debugstr_a(szDBPath), debugstr_a(szPersist), phDB);
244 szwDBPath = strdupAtoW( szDBPath );
249 if( HIWORD(szPersist) )
251 szwPersist = strdupAtoW( szPersist );
256 szwPersist = (LPWSTR)(DWORD_PTR)szPersist;
258 r = MsiOpenDatabaseW( szwDBPath, szwPersist, phDB );
261 if( HIWORD(szPersist) )
262 msi_free( szwPersist );
263 msi_free( szwDBPath );
268 static LPWSTR msi_read_text_archive(LPCWSTR path)
273 DWORD read, size = 0;
275 file = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
276 if (file == INVALID_HANDLE_VALUE)
279 size = GetFileSize( file, NULL );
280 data = msi_alloc( size + 1 );
284 if (!ReadFile( file, data, size, &read, NULL ))
288 wdata = strdupAtoW( data );
296 static void msi_parse_line(LPWSTR *line, LPWSTR **entries, DWORD *num_entries)
298 LPWSTR ptr = *line, save;
303 /* stay on this line */
304 while (*ptr && *ptr != '\n')
306 /* entries are separated by tabs */
313 *entries = msi_alloc(count * sizeof(LPWSTR));
317 /* store pointers into the data */
318 for (i = 0, ptr = *line; i < count; i++)
322 while (*ptr && *ptr != '\t' && *ptr != '\n') ptr++;
324 /* NULL-separate the data */
328 (*entries)[i] = save;
331 /* move to the next line if there's more, else EOF */
335 *num_entries = count;
338 static LPWSTR msi_build_createsql_prelude(LPWSTR table)
343 static const WCHAR create_fmt[] = {'C','R','E','A','T','E',' ','T','A','B','L','E',' ','`','%','s','`',' ','(',' ',0};
345 size = sizeof(create_fmt) + lstrlenW(table) - 2;
346 prelude = msi_alloc(size * sizeof(WCHAR));
350 sprintfW(prelude, create_fmt, table);
354 static LPWSTR msi_build_createsql_columns(LPWSTR *columns_data, LPWSTR *types, DWORD num_columns)
358 DWORD sql_size = 1, i, len;
359 WCHAR expanded[128], *ptr;
360 WCHAR size[10], comma[2], extra[30];
362 static const WCHAR column_fmt[] = {'`','%','s','`',' ','%','s','%','s','%','s','%','s',' ',0};
363 static const WCHAR size_fmt[] = {'(','%','s',')',0};
364 static const WCHAR type_char[] = {'C','H','A','R',0};
365 static const WCHAR type_int[] = {'I','N','T',0};
366 static const WCHAR type_long[] = {'L','O','N','G',0};
367 static const WCHAR type_notnull[] = {' ','N','O','T',' ','N','U','L','L',0};
368 static const WCHAR localizable[] = {' ','L','O','C','A','L','I','Z','A','B','L','E',0};
370 columns = msi_alloc_zero(sql_size * sizeof(WCHAR));
374 for (i = 0; i < num_columns; i++)
377 comma[1] = size[0] = extra[0] = '\0';
379 if (i == num_columns - 1)
391 lstrcpyW(extra, type_notnull);
393 lstrcatW(extra, localizable);
395 sprintfW(size, size_fmt, ptr);
398 lstrcpyW(extra, type_notnull);
401 sprintfW(size, size_fmt, ptr);
404 lstrcpyW(extra, type_notnull);
413 sprintfW(expanded, column_fmt, columns_data[i], type, size, extra, comma);
414 sql_size += lstrlenW(expanded);
416 columns = msi_realloc(columns, sql_size * sizeof(WCHAR));
420 lstrcatW(columns, expanded);
426 static LPWSTR msi_build_createsql_postlude(LPWSTR *primary_keys, DWORD num_keys)
428 LPWSTR postlude, keys, ptr;
429 DWORD size, key_size, i;
431 static const WCHAR key_fmt[] = {'`','%','s','`',',',' ',0};
432 static const WCHAR postlude_fmt[] = {'P','R','I','M','A','R','Y',' ','K','E','Y',' ','%','s',')',0};
434 for (i = 0, size = 1; i < num_keys; i++)
435 size += lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) - 2;
437 keys = msi_alloc(size * sizeof(WCHAR));
441 for (i = 0, ptr = keys; i < num_keys; i++)
443 key_size = lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) -2;
444 sprintfW(ptr, key_fmt, primary_keys[i]);
448 /* remove final ', ' */
451 size = lstrlenW(postlude_fmt) + size - 1;
452 postlude = msi_alloc(size * sizeof(WCHAR));
456 sprintfW(postlude, postlude_fmt, keys);
463 static UINT msi_add_table_to_db(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types, LPWSTR *labels, DWORD num_labels, DWORD num_columns)
469 LPWSTR prelude, columns_sql, postlude;
471 prelude = msi_build_createsql_prelude(labels[0]);
472 columns_sql = msi_build_createsql_columns(columns, types, num_columns);
473 postlude = msi_build_createsql_postlude(labels + 1, num_labels - 1); /* skip over table name */
475 if (!prelude || !columns_sql || !postlude)
476 return ERROR_OUTOFMEMORY;
478 size = lstrlenW(prelude) + lstrlenW(columns_sql) + lstrlenW(postlude) + 1;
479 create_sql = msi_alloc(size * sizeof(WCHAR));
481 return ERROR_OUTOFMEMORY;
483 lstrcpyW(create_sql, prelude);
484 lstrcatW(create_sql, columns_sql);
485 lstrcatW(create_sql, postlude);
488 msi_free(columns_sql);
491 r = MSI_DatabaseOpenViewW( db, create_sql, &view );
492 msi_free(create_sql);
494 if (r != ERROR_SUCCESS)
497 r = MSI_ViewExecute(view, NULL);
499 msiobj_release(&view->hdr);
504 static LPWSTR msi_build_insertsql_prelude(LPWSTR table)
509 static const WCHAR insert_fmt[] = {'I','N','S','E','R','T',' ','I','N','T','O',' ','`','%','s','`',' ','(',' ',0};
511 size = sizeof(insert_fmt) + lstrlenW(table) - 2;
512 prelude = msi_alloc(size * sizeof(WCHAR));
516 sprintfW(prelude, insert_fmt, table);
520 static LPWSTR msi_build_insertsql_columns(LPWSTR *columns_data, LPWSTR *types, DWORD num_columns)
523 DWORD sql_size = 1, i;
526 static const WCHAR column_fmt[] = {'`','%','s','`',',',' ',0};
528 columns = msi_alloc_zero(sql_size * sizeof(WCHAR));
532 for (i = 0; i < num_columns; i++)
534 sprintfW(expanded, column_fmt, columns_data[i]);
535 sql_size += lstrlenW(expanded);
537 if (i == num_columns - 1)
540 expanded[lstrlenW(expanded) - 2] = '\0';
543 columns = msi_realloc(columns, sql_size * sizeof(WCHAR));
547 lstrcatW(columns, expanded);
553 static LPWSTR msi_build_insertsql_data(LPWSTR **records, LPWSTR *types, DWORD num_columns, DWORD irec)
556 DWORD sql_size = 1, i;
559 static const WCHAR str_fmt[] = {'\'','%','s','\'',',',' ',0};
560 static const WCHAR int_fmt[] = {'%','s',',',' ',0};
561 static const WCHAR empty[] = {'\'','\'',',',' ',0};
563 columns = msi_alloc_zero(sql_size * sizeof(WCHAR));
567 for (i = 0; i < num_columns; i++)
571 case 'L': case 'l': case 'S': case 's':
572 sprintfW(expanded, str_fmt, records[irec][i]);
576 sprintfW(expanded, int_fmt, records[irec][i]);
578 lstrcpyW(expanded, empty);
584 if (i == num_columns - 1)
585 expanded[lstrlenW(expanded) - 2] = '\0';
587 sql_size += lstrlenW(expanded);
588 columns = msi_realloc(columns, sql_size * sizeof(WCHAR));
592 lstrcatW(columns, expanded);
598 static UINT msi_add_records_to_table(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types,
599 LPWSTR *labels, LPWSTR **records,
600 int num_columns, int num_records)
605 UINT r = ERROR_SUCCESS;
607 static const WCHAR mid[] = {' ',')',' ','V','A','L','U','E','S',' ','(',' ',0};
608 static const WCHAR end[] = {' ',')',0};
610 LPWSTR prelude = msi_build_insertsql_prelude(labels[0]);
611 LPWSTR columns_sql = msi_build_insertsql_columns(columns, types, num_columns);
613 for (i = 0; i < num_records; i++)
615 LPWSTR data = msi_build_insertsql_data(records, types, num_columns, i);
617 size = lstrlenW(prelude) + lstrlenW(columns_sql) + sizeof(mid) + lstrlenW(data) + sizeof(end) - 1;
618 insert_sql = msi_alloc(size * sizeof(WCHAR));
620 return ERROR_OUTOFMEMORY;
622 lstrcpyW(insert_sql, prelude);
623 lstrcatW(insert_sql, columns_sql);
624 lstrcatW(insert_sql, mid);
625 lstrcatW(insert_sql, data);
626 lstrcatW(insert_sql, end);
630 r = MSI_DatabaseOpenViewW( db, insert_sql, &view );
631 msi_free(insert_sql);
633 if (r != ERROR_SUCCESS)
636 r = MSI_ViewExecute(view, NULL);
638 msiobj_release(&view->hdr);
643 msi_free(columns_sql);
648 UINT MSI_DatabaseImport(MSIDATABASE *db, LPCWSTR folder, LPCWSTR file)
653 DWORD num_columns, num_records = 0;
654 LPWSTR *columns, *types, *labels;
655 LPWSTR path, ptr, data;
658 static const WCHAR backslash[] = {'\\',0};
660 TRACE("%p %s %s\n", db, debugstr_w(folder), debugstr_w(file) );
662 if( folder == NULL || file == NULL )
663 return ERROR_INVALID_PARAMETER;
665 len = lstrlenW(folder) + lstrlenW(backslash) + lstrlenW(file) + 1;
666 path = msi_alloc( len * sizeof(WCHAR) );
668 return ERROR_OUTOFMEMORY;
670 lstrcpyW( path, folder );
671 lstrcatW( path, backslash );
672 lstrcatW( path, file );
674 data = msi_read_text_archive( path );
677 msi_parse_line( &ptr, &columns, &num_columns );
678 msi_parse_line( &ptr, &types, NULL );
679 msi_parse_line( &ptr, &labels, &num_labels );
681 records = msi_alloc(sizeof(LPWSTR *));
683 return ERROR_OUTOFMEMORY;
685 /* read in the table records */
688 msi_parse_line( &ptr, &records[num_records], NULL );
691 records = msi_realloc(records, (num_records + 1) * sizeof(LPWSTR *));
693 return ERROR_OUTOFMEMORY;
696 r = msi_add_table_to_db( db, columns, types, labels, num_labels, num_columns );
697 if (r != ERROR_SUCCESS)
700 r = msi_add_records_to_table( db, columns, types, labels, records, num_columns, num_records );
709 for (i = 0; i < num_records; i++)
710 msi_free(records[i]);
717 UINT WINAPI MsiDatabaseImportW(MSIHANDLE handle, LPCWSTR szFolder, LPCWSTR szFilename)
722 TRACE("%lx %s %s\n",handle,debugstr_w(szFolder), debugstr_w(szFilename));
724 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
727 IWineMsiRemoteDatabase *remote_database;
729 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
730 if ( !remote_database )
731 return ERROR_INVALID_HANDLE;
733 IWineMsiRemoteDatabase_Release( remote_database );
734 WARN("MsiDatabaseImport not allowed during a custom action!\n");
736 return ERROR_SUCCESS;
739 r = MSI_DatabaseImport( db, szFolder, szFilename );
740 msiobj_release( &db->hdr );
744 UINT WINAPI MsiDatabaseImportA( MSIHANDLE handle,
745 LPCSTR szFolder, LPCSTR szFilename )
747 LPWSTR path = NULL, file = NULL;
748 UINT r = ERROR_OUTOFMEMORY;
750 TRACE("%lx %s %s\n", handle, debugstr_a(szFolder), debugstr_a(szFilename));
754 path = strdupAtoW( szFolder );
761 file = strdupAtoW( szFilename );
766 r = MsiDatabaseImportW( handle, path, file );
775 static UINT msi_export_record( HANDLE handle, MSIRECORD *row, UINT start )
777 UINT i, count, len, r = ERROR_SUCCESS;
783 buffer = msi_alloc( len );
785 return ERROR_OUTOFMEMORY;
787 count = MSI_RecordGetFieldCount( row );
788 for ( i=start; i<=count; i++ )
791 r = MSI_RecordGetStringA( row, i, buffer, &sz );
792 if (r == ERROR_MORE_DATA)
794 char *p = msi_realloc( buffer, sz + 1 );
801 r = MSI_RecordGetStringA( row, i, buffer, &sz );
802 if (r != ERROR_SUCCESS)
805 if (!WriteFile( handle, buffer, sz, &sz, NULL ))
807 r = ERROR_FUNCTION_FAILED;
811 sep = (i < count) ? "\t" : "\r\n";
812 if (!WriteFile( handle, sep, strlen(sep), &sz, NULL ))
814 r = ERROR_FUNCTION_FAILED;
822 static UINT msi_export_row( MSIRECORD *row, void *arg )
824 return msi_export_record( arg, row, 1 );
827 UINT MSI_DatabaseExport( MSIDATABASE *db, LPCWSTR table,
828 LPCWSTR folder, LPCWSTR file )
830 static const WCHAR query[] = {
831 's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','%','s',0 };
832 static const WCHAR szbs[] = { '\\', 0 };
833 MSIRECORD *rec = NULL;
834 MSIQUERY *view = NULL;
839 TRACE("%p %s %s %s\n", db, debugstr_w(table),
840 debugstr_w(folder), debugstr_w(file) );
842 if( folder == NULL || file == NULL )
843 return ERROR_INVALID_PARAMETER;
845 len = lstrlenW(folder) + lstrlenW(file) + 2;
846 filename = msi_alloc(len * sizeof (WCHAR));
848 return ERROR_OUTOFMEMORY;
850 lstrcpyW( filename, folder );
851 lstrcatW( filename, szbs );
852 lstrcatW( filename, file );
854 handle = CreateFileW( filename, GENERIC_READ | GENERIC_WRITE, 0,
855 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
856 msi_free( filename );
857 if (handle == INVALID_HANDLE_VALUE)
858 return ERROR_FUNCTION_FAILED;
860 r = MSI_OpenQuery( db, &view, query, table );
861 if (r == ERROR_SUCCESS)
863 /* write out row 1, the column names */
864 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
865 if (r == ERROR_SUCCESS)
867 msi_export_record( handle, rec, 1 );
868 msiobj_release( &rec->hdr );
871 /* write out row 2, the column types */
872 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
873 if (r == ERROR_SUCCESS)
875 msi_export_record( handle, rec, 1 );
876 msiobj_release( &rec->hdr );
879 /* write out row 3, the table name + keys */
880 r = MSI_DatabaseGetPrimaryKeys( db, table, &rec );
881 if (r == ERROR_SUCCESS)
883 MSI_RecordSetStringW( rec, 0, table );
884 msi_export_record( handle, rec, 0 );
885 msiobj_release( &rec->hdr );
888 /* write out row 4 onwards, the data */
889 r = MSI_IterateRecords( view, 0, msi_export_row, handle );
890 msiobj_release( &view->hdr );
893 CloseHandle( handle );
898 /***********************************************************************
899 * MsiExportDatabaseW [MSI.@]
901 * Writes a file containing the table data as tab separated ASCII.
903 * The format is as follows:
905 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
906 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
907 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
909 * Followed by the data, starting at row 1 with one row per line
911 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
913 UINT WINAPI MsiDatabaseExportW( MSIHANDLE handle, LPCWSTR szTable,
914 LPCWSTR szFolder, LPCWSTR szFilename )
919 TRACE("%lx %s %s %s\n", handle, debugstr_w(szTable),
920 debugstr_w(szFolder), debugstr_w(szFilename));
922 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
925 IWineMsiRemoteDatabase *remote_database;
927 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
928 if ( !remote_database )
929 return ERROR_INVALID_HANDLE;
931 IWineMsiRemoteDatabase_Release( remote_database );
932 WARN("MsiDatabaseExport not allowed during a custom action!\n");
934 return ERROR_SUCCESS;
937 r = MSI_DatabaseExport( db, szTable, szFolder, szFilename );
938 msiobj_release( &db->hdr );
942 UINT WINAPI MsiDatabaseExportA( MSIHANDLE handle, LPCSTR szTable,
943 LPCSTR szFolder, LPCSTR szFilename )
945 LPWSTR path = NULL, file = NULL, table = NULL;
946 UINT r = ERROR_OUTOFMEMORY;
948 TRACE("%lx %s %s %s\n", handle, debugstr_a(szTable),
949 debugstr_a(szFolder), debugstr_a(szFilename));
953 table = strdupAtoW( szTable );
960 path = strdupAtoW( szFolder );
967 file = strdupAtoW( szFilename );
972 r = MsiDatabaseExportW( handle, table, path, file );
982 MSIDBSTATE WINAPI MsiGetDatabaseState( MSIHANDLE handle )
984 MSIDBSTATE ret = MSIDBSTATE_READ;
987 TRACE("%ld\n", handle);
989 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
992 IWineMsiRemoteDatabase *remote_database;
994 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
995 if ( !remote_database )
996 return MSIDBSTATE_ERROR;
998 IWineMsiRemoteDatabase_Release( remote_database );
999 WARN("MsiGetDatabaseState not allowed during a custom action!\n");
1001 return MSIDBSTATE_READ;
1004 if (db->mode != MSIDBOPEN_READONLY )
1005 ret = MSIDBSTATE_WRITE;
1006 msiobj_release( &db->hdr );
1011 typedef struct _msi_remote_database_impl {
1012 const IWineMsiRemoteDatabaseVtbl *lpVtbl;
1015 } msi_remote_database_impl;
1017 static inline msi_remote_database_impl* mrd_from_IWineMsiRemoteDatabase( IWineMsiRemoteDatabase* iface )
1019 return (msi_remote_database_impl *)iface;
1022 static HRESULT WINAPI mrd_QueryInterface( IWineMsiRemoteDatabase *iface,
1023 REFIID riid,LPVOID *ppobj)
1025 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1026 IsEqualCLSID( riid, &IID_IWineMsiRemoteDatabase ) )
1028 IUnknown_AddRef( iface );
1033 return E_NOINTERFACE;
1036 static ULONG WINAPI mrd_AddRef( IWineMsiRemoteDatabase *iface )
1038 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1040 return InterlockedIncrement( &This->refs );
1043 static ULONG WINAPI mrd_Release( IWineMsiRemoteDatabase *iface )
1045 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1048 r = InterlockedDecrement( &This->refs );
1051 MsiCloseHandle( This->database );
1057 HRESULT WINAPI mrd_IsTablePersistent( IWineMsiRemoteDatabase *iface,
1058 BSTR table, MSICONDITION *persistent )
1060 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1061 *persistent = MsiDatabaseIsTablePersistentW(This->database, (LPWSTR)table);
1065 HRESULT WINAPI mrd_GetPrimaryKeys( IWineMsiRemoteDatabase *iface,
1066 BSTR table, MSIHANDLE *keys )
1068 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1069 UINT r = MsiDatabaseGetPrimaryKeysW(This->database, (LPWSTR)table, keys);
1070 return HRESULT_FROM_WIN32(r);
1073 HRESULT WINAPI mrd_GetSummaryInformation( IWineMsiRemoteDatabase *iface,
1074 UINT updatecount, MSIHANDLE *suminfo )
1076 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1077 UINT r = MsiGetSummaryInformationW(This->database, NULL, updatecount, suminfo);
1078 return HRESULT_FROM_WIN32(r);
1081 HRESULT WINAPI mrd_OpenView( IWineMsiRemoteDatabase *iface,
1082 BSTR query, MSIHANDLE *view )
1084 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1085 UINT r = MsiDatabaseOpenViewW(This->database, (LPWSTR)query, view);
1086 return HRESULT_FROM_WIN32(r);
1089 static HRESULT WINAPI mrd_SetMsiHandle( IWineMsiRemoteDatabase *iface, MSIHANDLE handle )
1091 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1092 This->database = handle;
1096 static const IWineMsiRemoteDatabaseVtbl msi_remote_database_vtbl =
1101 mrd_IsTablePersistent,
1103 mrd_GetSummaryInformation,
1108 HRESULT create_msi_remote_database( IUnknown *pOuter, LPVOID *ppObj )
1110 msi_remote_database_impl *This;
1112 This = msi_alloc( sizeof *This );
1114 return E_OUTOFMEMORY;
1116 This->lpVtbl = &msi_remote_database_vtbl;