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 p = msi_realloc(columns, sql_size * sizeof(WCHAR));
424 lstrcatW(columns, expanded);
430 static LPWSTR msi_build_createsql_postlude(LPWSTR *primary_keys, DWORD num_keys)
432 LPWSTR postlude, keys, ptr;
433 DWORD size, key_size, i;
435 static const WCHAR key_fmt[] = {'`','%','s','`',',',' ',0};
436 static const WCHAR postlude_fmt[] = {'P','R','I','M','A','R','Y',' ','K','E','Y',' ','%','s',')',0};
438 for (i = 0, size = 1; i < num_keys; i++)
439 size += lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) - 2;
441 keys = msi_alloc(size * sizeof(WCHAR));
445 for (i = 0, ptr = keys; i < num_keys; i++)
447 key_size = lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) -2;
448 sprintfW(ptr, key_fmt, primary_keys[i]);
452 /* remove final ', ' */
455 size = lstrlenW(postlude_fmt) + size - 1;
456 postlude = msi_alloc(size * sizeof(WCHAR));
460 sprintfW(postlude, postlude_fmt, keys);
467 static UINT msi_add_table_to_db(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types, LPWSTR *labels, DWORD num_labels, DWORD num_columns)
473 LPWSTR prelude, columns_sql, postlude;
475 prelude = msi_build_createsql_prelude(labels[0]);
476 columns_sql = msi_build_createsql_columns(columns, types, num_columns);
477 postlude = msi_build_createsql_postlude(labels + 1, num_labels - 1); /* skip over table name */
479 if (!prelude || !columns_sql || !postlude)
480 return ERROR_OUTOFMEMORY;
482 size = lstrlenW(prelude) + lstrlenW(columns_sql) + lstrlenW(postlude) + 1;
483 create_sql = msi_alloc(size * sizeof(WCHAR));
485 return ERROR_OUTOFMEMORY;
487 lstrcpyW(create_sql, prelude);
488 lstrcatW(create_sql, columns_sql);
489 lstrcatW(create_sql, postlude);
492 msi_free(columns_sql);
495 r = MSI_DatabaseOpenViewW( db, create_sql, &view );
496 msi_free(create_sql);
498 if (r != ERROR_SUCCESS)
501 r = MSI_ViewExecute(view, NULL);
503 msiobj_release(&view->hdr);
508 static LPWSTR msi_build_insertsql_prelude(LPWSTR table)
513 static const WCHAR insert_fmt[] = {'I','N','S','E','R','T',' ','I','N','T','O',' ','`','%','s','`',' ','(',' ',0};
515 size = sizeof(insert_fmt) + lstrlenW(table) - 2;
516 prelude = msi_alloc(size * sizeof(WCHAR));
520 sprintfW(prelude, insert_fmt, table);
524 static LPWSTR msi_build_insertsql_columns(LPWSTR *columns_data, LPWSTR *types, DWORD num_columns)
527 DWORD sql_size = 1, i;
530 static const WCHAR column_fmt[] = {'`','%','s','`',',',' ',0};
532 columns = msi_alloc_zero(sql_size * sizeof(WCHAR));
536 for (i = 0; i < num_columns; i++)
538 sprintfW(expanded, column_fmt, columns_data[i]);
539 sql_size += lstrlenW(expanded);
541 if (i == num_columns - 1)
544 expanded[lstrlenW(expanded) - 2] = '\0';
547 p = msi_realloc(columns, sql_size * sizeof(WCHAR));
555 lstrcatW(columns, expanded);
561 static LPWSTR msi_build_insertsql_data(LPWSTR **records, LPWSTR *types, DWORD num_columns, DWORD irec)
563 LPWSTR columns, temp_columns;
564 DWORD sql_size = 1, i;
567 static const WCHAR str_fmt[] = {'\'','%','s','\'',',',' ',0};
568 static const WCHAR int_fmt[] = {'%','s',',',' ',0};
569 static const WCHAR empty[] = {'\'','\'',',',' ',0};
571 columns = msi_alloc_zero(sql_size * sizeof(WCHAR));
575 for (i = 0; i < num_columns; i++)
579 case 'L': case 'l': case 'S': case 's':
580 sprintfW(expanded, str_fmt, records[irec][i]);
584 sprintfW(expanded, int_fmt, records[irec][i]);
586 lstrcpyW(expanded, empty);
589 HeapFree( GetProcessHeap(), 0, columns );
593 if (i == num_columns - 1)
594 expanded[lstrlenW(expanded) - 2] = '\0';
596 sql_size += lstrlenW(expanded);
597 temp_columns = msi_realloc(columns, sql_size * sizeof(WCHAR));
600 HeapFree( GetProcessHeap(), 0, columns);
603 columns = temp_columns;
605 lstrcatW(columns, expanded);
611 static UINT msi_add_records_to_table(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types,
612 LPWSTR *labels, LPWSTR **records,
613 int num_columns, int num_records)
618 UINT r = ERROR_SUCCESS;
620 static const WCHAR mid[] = {' ',')',' ','V','A','L','U','E','S',' ','(',' ',0};
621 static const WCHAR end[] = {' ',')',0};
623 LPWSTR prelude = msi_build_insertsql_prelude(labels[0]);
624 LPWSTR columns_sql = msi_build_insertsql_columns(columns, types, num_columns);
626 for (i = 0; i < num_records; i++)
628 LPWSTR data = msi_build_insertsql_data(records, types, num_columns, i);
630 size = lstrlenW(prelude) + lstrlenW(columns_sql) + sizeof(mid) + lstrlenW(data) + sizeof(end) - 1;
631 insert_sql = msi_alloc(size * sizeof(WCHAR));
633 return ERROR_OUTOFMEMORY;
635 lstrcpyW(insert_sql, prelude);
636 lstrcatW(insert_sql, columns_sql);
637 lstrcatW(insert_sql, mid);
638 lstrcatW(insert_sql, data);
639 lstrcatW(insert_sql, end);
643 r = MSI_DatabaseOpenViewW( db, insert_sql, &view );
644 msi_free(insert_sql);
646 if (r != ERROR_SUCCESS)
649 r = MSI_ViewExecute(view, NULL);
651 msiobj_release(&view->hdr);
656 msi_free(columns_sql);
661 UINT MSI_DatabaseImport(MSIDATABASE *db, LPCWSTR folder, LPCWSTR file)
666 DWORD num_columns, num_records = 0;
667 LPWSTR *columns, *types, *labels;
668 LPWSTR path, ptr, data;
670 LPWSTR **temp_records;
672 static const WCHAR backslash[] = {'\\',0};
674 TRACE("%p %s %s\n", db, debugstr_w(folder), debugstr_w(file) );
676 if( folder == NULL || file == NULL )
677 return ERROR_INVALID_PARAMETER;
679 len = lstrlenW(folder) + lstrlenW(backslash) + lstrlenW(file) + 1;
680 path = msi_alloc( len * sizeof(WCHAR) );
682 return ERROR_OUTOFMEMORY;
684 lstrcpyW( path, folder );
685 lstrcatW( path, backslash );
686 lstrcatW( path, file );
688 data = msi_read_text_archive( path );
691 msi_parse_line( &ptr, &columns, &num_columns );
692 msi_parse_line( &ptr, &types, NULL );
693 msi_parse_line( &ptr, &labels, &num_labels );
695 records = msi_alloc(sizeof(LPWSTR *));
698 r = ERROR_OUTOFMEMORY;
702 /* read in the table records */
705 msi_parse_line( &ptr, &records[num_records], NULL );
708 temp_records = msi_realloc(records, (num_records + 1) * sizeof(LPWSTR *));
711 r = ERROR_OUTOFMEMORY;
714 records = temp_records;
717 r = msi_add_table_to_db( db, columns, types, labels, num_labels, num_columns );
718 if (r != ERROR_SUCCESS)
721 r = msi_add_records_to_table( db, columns, types, labels, records, num_columns, num_records );
730 for (i = 0; i < num_records; i++)
731 msi_free(records[i]);
738 UINT WINAPI MsiDatabaseImportW(MSIHANDLE handle, LPCWSTR szFolder, LPCWSTR szFilename)
743 TRACE("%lx %s %s\n",handle,debugstr_w(szFolder), debugstr_w(szFilename));
745 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
748 IWineMsiRemoteDatabase *remote_database;
750 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
751 if ( !remote_database )
752 return ERROR_INVALID_HANDLE;
754 IWineMsiRemoteDatabase_Release( remote_database );
755 WARN("MsiDatabaseImport not allowed during a custom action!\n");
757 return ERROR_SUCCESS;
760 r = MSI_DatabaseImport( db, szFolder, szFilename );
761 msiobj_release( &db->hdr );
765 UINT WINAPI MsiDatabaseImportA( MSIHANDLE handle,
766 LPCSTR szFolder, LPCSTR szFilename )
768 LPWSTR path = NULL, file = NULL;
769 UINT r = ERROR_OUTOFMEMORY;
771 TRACE("%lx %s %s\n", handle, debugstr_a(szFolder), debugstr_a(szFilename));
775 path = strdupAtoW( szFolder );
782 file = strdupAtoW( szFilename );
787 r = MsiDatabaseImportW( handle, path, file );
796 static UINT msi_export_record( HANDLE handle, MSIRECORD *row, UINT start )
798 UINT i, count, len, r = ERROR_SUCCESS;
804 buffer = msi_alloc( len );
806 return ERROR_OUTOFMEMORY;
808 count = MSI_RecordGetFieldCount( row );
809 for ( i=start; i<=count; i++ )
812 r = MSI_RecordGetStringA( row, i, buffer, &sz );
813 if (r == ERROR_MORE_DATA)
815 char *p = msi_realloc( buffer, sz + 1 );
822 r = MSI_RecordGetStringA( row, i, buffer, &sz );
823 if (r != ERROR_SUCCESS)
826 if (!WriteFile( handle, buffer, sz, &sz, NULL ))
828 r = ERROR_FUNCTION_FAILED;
832 sep = (i < count) ? "\t" : "\r\n";
833 if (!WriteFile( handle, sep, strlen(sep), &sz, NULL ))
835 r = ERROR_FUNCTION_FAILED;
843 static UINT msi_export_row( MSIRECORD *row, void *arg )
845 return msi_export_record( arg, row, 1 );
848 UINT MSI_DatabaseExport( MSIDATABASE *db, LPCWSTR table,
849 LPCWSTR folder, LPCWSTR file )
851 static const WCHAR query[] = {
852 's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','%','s',0 };
853 static const WCHAR szbs[] = { '\\', 0 };
854 MSIRECORD *rec = NULL;
855 MSIQUERY *view = NULL;
860 TRACE("%p %s %s %s\n", db, debugstr_w(table),
861 debugstr_w(folder), debugstr_w(file) );
863 if( folder == NULL || file == NULL )
864 return ERROR_INVALID_PARAMETER;
866 len = lstrlenW(folder) + lstrlenW(file) + 2;
867 filename = msi_alloc(len * sizeof (WCHAR));
869 return ERROR_OUTOFMEMORY;
871 lstrcpyW( filename, folder );
872 lstrcatW( filename, szbs );
873 lstrcatW( filename, file );
875 handle = CreateFileW( filename, GENERIC_READ | GENERIC_WRITE, 0,
876 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
877 msi_free( filename );
878 if (handle == INVALID_HANDLE_VALUE)
879 return ERROR_FUNCTION_FAILED;
881 r = MSI_OpenQuery( db, &view, query, table );
882 if (r == ERROR_SUCCESS)
884 /* write out row 1, the column names */
885 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
886 if (r == ERROR_SUCCESS)
888 msi_export_record( handle, rec, 1 );
889 msiobj_release( &rec->hdr );
892 /* write out row 2, the column types */
893 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
894 if (r == ERROR_SUCCESS)
896 msi_export_record( handle, rec, 1 );
897 msiobj_release( &rec->hdr );
900 /* write out row 3, the table name + keys */
901 r = MSI_DatabaseGetPrimaryKeys( db, table, &rec );
902 if (r == ERROR_SUCCESS)
904 MSI_RecordSetStringW( rec, 0, table );
905 msi_export_record( handle, rec, 0 );
906 msiobj_release( &rec->hdr );
909 /* write out row 4 onwards, the data */
910 r = MSI_IterateRecords( view, 0, msi_export_row, handle );
911 msiobj_release( &view->hdr );
914 CloseHandle( handle );
919 /***********************************************************************
920 * MsiExportDatabaseW [MSI.@]
922 * Writes a file containing the table data as tab separated ASCII.
924 * The format is as follows:
926 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
927 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
928 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
930 * Followed by the data, starting at row 1 with one row per line
932 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
934 UINT WINAPI MsiDatabaseExportW( MSIHANDLE handle, LPCWSTR szTable,
935 LPCWSTR szFolder, LPCWSTR szFilename )
940 TRACE("%lx %s %s %s\n", handle, debugstr_w(szTable),
941 debugstr_w(szFolder), debugstr_w(szFilename));
943 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
946 IWineMsiRemoteDatabase *remote_database;
948 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
949 if ( !remote_database )
950 return ERROR_INVALID_HANDLE;
952 IWineMsiRemoteDatabase_Release( remote_database );
953 WARN("MsiDatabaseExport not allowed during a custom action!\n");
955 return ERROR_SUCCESS;
958 r = MSI_DatabaseExport( db, szTable, szFolder, szFilename );
959 msiobj_release( &db->hdr );
963 UINT WINAPI MsiDatabaseExportA( MSIHANDLE handle, LPCSTR szTable,
964 LPCSTR szFolder, LPCSTR szFilename )
966 LPWSTR path = NULL, file = NULL, table = NULL;
967 UINT r = ERROR_OUTOFMEMORY;
969 TRACE("%lx %s %s %s\n", handle, debugstr_a(szTable),
970 debugstr_a(szFolder), debugstr_a(szFilename));
974 table = strdupAtoW( szTable );
981 path = strdupAtoW( szFolder );
988 file = strdupAtoW( szFilename );
993 r = MsiDatabaseExportW( handle, table, path, file );
1003 MSIDBSTATE WINAPI MsiGetDatabaseState( MSIHANDLE handle )
1005 MSIDBSTATE ret = MSIDBSTATE_READ;
1008 TRACE("%ld\n", handle);
1010 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
1013 IWineMsiRemoteDatabase *remote_database;
1015 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
1016 if ( !remote_database )
1017 return MSIDBSTATE_ERROR;
1019 IWineMsiRemoteDatabase_Release( remote_database );
1020 WARN("MsiGetDatabaseState not allowed during a custom action!\n");
1022 return MSIDBSTATE_READ;
1025 if (db->mode != MSIDBOPEN_READONLY )
1026 ret = MSIDBSTATE_WRITE;
1027 msiobj_release( &db->hdr );
1032 typedef struct _msi_remote_database_impl {
1033 const IWineMsiRemoteDatabaseVtbl *lpVtbl;
1036 } msi_remote_database_impl;
1038 static inline msi_remote_database_impl* mrd_from_IWineMsiRemoteDatabase( IWineMsiRemoteDatabase* iface )
1040 return (msi_remote_database_impl *)iface;
1043 static HRESULT WINAPI mrd_QueryInterface( IWineMsiRemoteDatabase *iface,
1044 REFIID riid,LPVOID *ppobj)
1046 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1047 IsEqualCLSID( riid, &IID_IWineMsiRemoteDatabase ) )
1049 IUnknown_AddRef( iface );
1054 return E_NOINTERFACE;
1057 static ULONG WINAPI mrd_AddRef( IWineMsiRemoteDatabase *iface )
1059 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1061 return InterlockedIncrement( &This->refs );
1064 static ULONG WINAPI mrd_Release( IWineMsiRemoteDatabase *iface )
1066 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1069 r = InterlockedDecrement( &This->refs );
1072 MsiCloseHandle( This->database );
1078 HRESULT WINAPI mrd_IsTablePersistent( IWineMsiRemoteDatabase *iface,
1079 BSTR table, MSICONDITION *persistent )
1081 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1082 *persistent = MsiDatabaseIsTablePersistentW(This->database, (LPWSTR)table);
1086 HRESULT WINAPI mrd_GetPrimaryKeys( IWineMsiRemoteDatabase *iface,
1087 BSTR table, MSIHANDLE *keys )
1089 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1090 UINT r = MsiDatabaseGetPrimaryKeysW(This->database, (LPWSTR)table, keys);
1091 return HRESULT_FROM_WIN32(r);
1094 HRESULT WINAPI mrd_GetSummaryInformation( IWineMsiRemoteDatabase *iface,
1095 UINT updatecount, MSIHANDLE *suminfo )
1097 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1098 UINT r = MsiGetSummaryInformationW(This->database, NULL, updatecount, suminfo);
1099 return HRESULT_FROM_WIN32(r);
1102 HRESULT WINAPI mrd_OpenView( IWineMsiRemoteDatabase *iface,
1103 BSTR query, MSIHANDLE *view )
1105 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1106 UINT r = MsiDatabaseOpenViewW(This->database, (LPWSTR)query, view);
1107 return HRESULT_FROM_WIN32(r);
1110 static HRESULT WINAPI mrd_SetMsiHandle( IWineMsiRemoteDatabase *iface, MSIHANDLE handle )
1112 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1113 This->database = handle;
1117 static const IWineMsiRemoteDatabaseVtbl msi_remote_database_vtbl =
1122 mrd_IsTablePersistent,
1124 mrd_GetSummaryInformation,
1129 HRESULT create_msi_remote_database( IUnknown *pOuter, LPVOID *ppObj )
1131 msi_remote_database_impl *This;
1133 This = msi_alloc( sizeof *This );
1135 return E_OUTOFMEMORY;
1137 This->lpVtbl = &msi_remote_database_vtbl;