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
);
412 ERR("Unknown type: %c\n", types
[i
][0]);
417 sprintfW(expanded
, column_fmt
, columns_data
[i
], type
, size
, extra
, comma
);
418 sql_size
+= lstrlenW(expanded
);
420 p
= msi_realloc(columns
, sql_size
* sizeof(WCHAR
));
428 lstrcatW(columns
, expanded
);
434 static LPWSTR
msi_build_createsql_postlude(LPWSTR
*primary_keys
, DWORD num_keys
)
436 LPWSTR postlude
, keys
, ptr
;
437 DWORD size
, key_size
, i
;
439 static const WCHAR key_fmt
[] = {'`','%','s','`',',',' ',0};
440 static const WCHAR postlude_fmt
[] = {'P','R','I','M','A','R','Y',' ','K','E','Y',' ','%','s',')',0};
442 for (i
= 0, size
= 1; i
< num_keys
; i
++)
443 size
+= lstrlenW(key_fmt
) + lstrlenW(primary_keys
[i
]) - 2;
445 keys
= msi_alloc(size
* sizeof(WCHAR
));
449 for (i
= 0, ptr
= keys
; i
< num_keys
; i
++)
451 key_size
= lstrlenW(key_fmt
) + lstrlenW(primary_keys
[i
]) -2;
452 sprintfW(ptr
, key_fmt
, primary_keys
[i
]);
456 /* remove final ', ' */
459 size
= lstrlenW(postlude_fmt
) + size
- 1;
460 postlude
= msi_alloc(size
* sizeof(WCHAR
));
464 sprintfW(postlude
, postlude_fmt
, keys
);
471 static UINT
msi_add_table_to_db(MSIDATABASE
*db
, LPWSTR
*columns
, LPWSTR
*types
, LPWSTR
*labels
, DWORD num_labels
, DWORD num_columns
)
477 LPWSTR prelude
, columns_sql
, postlude
;
479 prelude
= msi_build_createsql_prelude(labels
[0]);
480 columns_sql
= msi_build_createsql_columns(columns
, types
, num_columns
);
481 postlude
= msi_build_createsql_postlude(labels
+ 1, num_labels
- 1); /* skip over table name */
483 if (!prelude
|| !columns_sql
|| !postlude
)
484 return ERROR_OUTOFMEMORY
;
486 size
= lstrlenW(prelude
) + lstrlenW(columns_sql
) + lstrlenW(postlude
) + 1;
487 create_sql
= msi_alloc(size
* sizeof(WCHAR
));
489 return ERROR_OUTOFMEMORY
;
491 lstrcpyW(create_sql
, prelude
);
492 lstrcatW(create_sql
, columns_sql
);
493 lstrcatW(create_sql
, postlude
);
496 msi_free(columns_sql
);
499 r
= MSI_DatabaseOpenViewW( db
, create_sql
, &view
);
500 msi_free(create_sql
);
502 if (r
!= ERROR_SUCCESS
)
505 r
= MSI_ViewExecute(view
, NULL
);
507 msiobj_release(&view
->hdr
);
512 static LPWSTR
msi_build_insertsql_prelude(LPWSTR table
)
517 static const WCHAR insert_fmt
[] = {'I','N','S','E','R','T',' ','I','N','T','O',' ','`','%','s','`',' ','(',' ',0};
519 size
= sizeof(insert_fmt
) + lstrlenW(table
) - 2;
520 prelude
= msi_alloc(size
* sizeof(WCHAR
));
524 sprintfW(prelude
, insert_fmt
, table
);
528 static LPWSTR
msi_build_insertsql_columns(LPWSTR
*columns_data
, LPWSTR
*types
, DWORD num_columns
)
531 DWORD sql_size
= 1, i
;
534 static const WCHAR column_fmt
[] = {'`','%','s','`',',',' ',0};
536 columns
= msi_alloc_zero(sql_size
* sizeof(WCHAR
));
540 for (i
= 0; i
< num_columns
; i
++)
542 sprintfW(expanded
, column_fmt
, columns_data
[i
]);
543 sql_size
+= lstrlenW(expanded
);
545 if (i
== num_columns
- 1)
548 expanded
[lstrlenW(expanded
) - 2] = '\0';
551 p
= msi_realloc(columns
, sql_size
* sizeof(WCHAR
));
559 lstrcatW(columns
, expanded
);
565 static LPWSTR
msi_build_insertsql_data(LPWSTR
**records
, LPWSTR
*types
, DWORD num_columns
, DWORD irec
)
567 LPWSTR columns
, temp_columns
;
568 DWORD sql_size
= 1, i
;
571 static const WCHAR str_fmt
[] = {'\'','%','s','\'',',',' ',0};
572 static const WCHAR int_fmt
[] = {'%','s',',',' ',0};
573 static const WCHAR empty
[] = {'\'','\'',',',' ',0};
575 columns
= msi_alloc_zero(sql_size
* sizeof(WCHAR
));
579 for (i
= 0; i
< num_columns
; i
++)
583 case 'L': case 'l': case 'S': case 's':
584 sprintfW(expanded
, str_fmt
, records
[irec
][i
]);
588 sprintfW(expanded
, int_fmt
, records
[irec
][i
]);
590 lstrcpyW(expanded
, empty
);
593 HeapFree( GetProcessHeap(), 0, columns
);
597 if (i
== num_columns
- 1)
598 expanded
[lstrlenW(expanded
) - 2] = '\0';
600 sql_size
+= lstrlenW(expanded
);
601 temp_columns
= msi_realloc(columns
, sql_size
* sizeof(WCHAR
));
604 HeapFree( GetProcessHeap(), 0, columns
);
607 columns
= temp_columns
;
609 lstrcatW(columns
, expanded
);
615 static UINT
msi_add_records_to_table(MSIDATABASE
*db
, LPWSTR
*columns
, LPWSTR
*types
,
616 LPWSTR
*labels
, LPWSTR
**records
,
617 int num_columns
, int num_records
)
622 UINT r
= ERROR_SUCCESS
;
624 static const WCHAR mid
[] = {' ',')',' ','V','A','L','U','E','S',' ','(',' ',0};
625 static const WCHAR end
[] = {' ',')',0};
627 LPWSTR prelude
= msi_build_insertsql_prelude(labels
[0]);
628 LPWSTR columns_sql
= msi_build_insertsql_columns(columns
, types
, num_columns
);
630 for (i
= 0; i
< num_records
; i
++)
632 LPWSTR data
= msi_build_insertsql_data(records
, types
, num_columns
, i
);
634 size
= lstrlenW(prelude
) + lstrlenW(columns_sql
) + sizeof(mid
) + lstrlenW(data
) + sizeof(end
) - 1;
635 insert_sql
= msi_alloc(size
* sizeof(WCHAR
));
637 return ERROR_OUTOFMEMORY
;
639 lstrcpyW(insert_sql
, prelude
);
640 lstrcatW(insert_sql
, columns_sql
);
641 lstrcatW(insert_sql
, mid
);
642 lstrcatW(insert_sql
, data
);
643 lstrcatW(insert_sql
, end
);
647 r
= MSI_DatabaseOpenViewW( db
, insert_sql
, &view
);
648 msi_free(insert_sql
);
650 if (r
!= ERROR_SUCCESS
)
653 r
= MSI_ViewExecute(view
, NULL
);
655 msiobj_release(&view
->hdr
);
660 msi_free(columns_sql
);
665 UINT
MSI_DatabaseImport(MSIDATABASE
*db
, LPCWSTR folder
, LPCWSTR file
)
670 DWORD num_columns
, num_records
= 0;
671 LPWSTR
*columns
, *types
, *labels
;
672 LPWSTR path
, ptr
, data
;
674 LPWSTR
**temp_records
;
676 static const WCHAR backslash
[] = {'\\',0};
678 TRACE("%p %s %s\n", db
, debugstr_w(folder
), debugstr_w(file
) );
680 if( folder
== NULL
|| file
== NULL
)
681 return ERROR_INVALID_PARAMETER
;
683 len
= lstrlenW(folder
) + lstrlenW(backslash
) + lstrlenW(file
) + 1;
684 path
= msi_alloc( len
* sizeof(WCHAR
) );
686 return ERROR_OUTOFMEMORY
;
688 lstrcpyW( path
, folder
);
689 lstrcatW( path
, backslash
);
690 lstrcatW( path
, file
);
692 data
= msi_read_text_archive( path
);
695 msi_parse_line( &ptr
, &columns
, &num_columns
);
696 msi_parse_line( &ptr
, &types
, NULL
);
697 msi_parse_line( &ptr
, &labels
, &num_labels
);
699 records
= msi_alloc(sizeof(LPWSTR
*));
702 r
= ERROR_OUTOFMEMORY
;
706 /* read in the table records */
709 msi_parse_line( &ptr
, &records
[num_records
], NULL
);
712 temp_records
= msi_realloc(records
, (num_records
+ 1) * sizeof(LPWSTR
*));
715 r
= ERROR_OUTOFMEMORY
;
718 records
= temp_records
;
721 r
= msi_add_table_to_db( db
, columns
, types
, labels
, num_labels
, num_columns
);
722 if (r
!= ERROR_SUCCESS
)
725 r
= msi_add_records_to_table( db
, columns
, types
, labels
, records
, num_columns
, num_records
);
734 for (i
= 0; i
< num_records
; i
++)
735 msi_free(records
[i
]);
742 UINT WINAPI
MsiDatabaseImportW(MSIHANDLE handle
, LPCWSTR szFolder
, LPCWSTR szFilename
)
747 TRACE("%lx %s %s\n",handle
,debugstr_w(szFolder
), debugstr_w(szFilename
));
749 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
752 IWineMsiRemoteDatabase
*remote_database
;
754 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( handle
);
755 if ( !remote_database
)
756 return ERROR_INVALID_HANDLE
;
758 IWineMsiRemoteDatabase_Release( remote_database
);
759 WARN("MsiDatabaseImport not allowed during a custom action!\n");
761 return ERROR_SUCCESS
;
764 r
= MSI_DatabaseImport( db
, szFolder
, szFilename
);
765 msiobj_release( &db
->hdr
);
769 UINT WINAPI
MsiDatabaseImportA( MSIHANDLE handle
,
770 LPCSTR szFolder
, LPCSTR szFilename
)
772 LPWSTR path
= NULL
, file
= NULL
;
773 UINT r
= ERROR_OUTOFMEMORY
;
775 TRACE("%lx %s %s\n", handle
, debugstr_a(szFolder
), debugstr_a(szFilename
));
779 path
= strdupAtoW( szFolder
);
786 file
= strdupAtoW( szFilename
);
791 r
= MsiDatabaseImportW( handle
, path
, file
);
800 static UINT
msi_export_record( HANDLE handle
, MSIRECORD
*row
, UINT start
)
802 UINT i
, count
, len
, r
= ERROR_SUCCESS
;
808 buffer
= msi_alloc( len
);
810 return ERROR_OUTOFMEMORY
;
812 count
= MSI_RecordGetFieldCount( row
);
813 for ( i
=start
; i
<=count
; i
++ )
816 r
= MSI_RecordGetStringA( row
, i
, buffer
, &sz
);
817 if (r
== ERROR_MORE_DATA
)
819 char *p
= msi_realloc( buffer
, sz
+ 1 );
826 r
= MSI_RecordGetStringA( row
, i
, buffer
, &sz
);
827 if (r
!= ERROR_SUCCESS
)
830 if (!WriteFile( handle
, buffer
, sz
, &sz
, NULL
))
832 r
= ERROR_FUNCTION_FAILED
;
836 sep
= (i
< count
) ? "\t" : "\r\n";
837 if (!WriteFile( handle
, sep
, strlen(sep
), &sz
, NULL
))
839 r
= ERROR_FUNCTION_FAILED
;
847 static UINT
msi_export_row( MSIRECORD
*row
, void *arg
)
849 return msi_export_record( arg
, row
, 1 );
852 static UINT
msi_export_forcecodepage( HANDLE handle
)
856 static const char data
[] = "\r\n\r\n0\t_ForceCodepage\r\n";
858 FIXME("Read the codepage from the strings table!\n");
860 sz
= lstrlenA(data
) + 1;
861 if (!WriteFile(handle
, data
, sz
, &sz
, NULL
))
862 return ERROR_FUNCTION_FAILED
;
864 return ERROR_SUCCESS
;
867 UINT
MSI_DatabaseExport( MSIDATABASE
*db
, LPCWSTR table
,
868 LPCWSTR folder
, LPCWSTR file
)
870 static const WCHAR query
[] = {
871 's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','%','s',0 };
872 static const WCHAR szbs
[] = { '\\', 0 };
873 static const WCHAR forcecodepage
[] = {
874 '_','F','o','r','c','e','C','o','d','e','p','a','g','e',0 };
875 MSIRECORD
*rec
= NULL
;
876 MSIQUERY
*view
= NULL
;
881 TRACE("%p %s %s %s\n", db
, debugstr_w(table
),
882 debugstr_w(folder
), debugstr_w(file
) );
884 if( folder
== NULL
|| file
== NULL
)
885 return ERROR_INVALID_PARAMETER
;
887 len
= lstrlenW(folder
) + lstrlenW(file
) + 2;
888 filename
= msi_alloc(len
* sizeof (WCHAR
));
890 return ERROR_OUTOFMEMORY
;
892 lstrcpyW( filename
, folder
);
893 lstrcatW( filename
, szbs
);
894 lstrcatW( filename
, file
);
896 handle
= CreateFileW( filename
, GENERIC_READ
| GENERIC_WRITE
, 0,
897 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
898 msi_free( filename
);
899 if (handle
== INVALID_HANDLE_VALUE
)
900 return ERROR_FUNCTION_FAILED
;
902 if (!lstrcmpW( table
, forcecodepage
))
904 r
= msi_export_forcecodepage( handle
);
908 r
= MSI_OpenQuery( db
, &view
, query
, table
);
909 if (r
== ERROR_SUCCESS
)
911 /* write out row 1, the column names */
912 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_NAMES
, &rec
);
913 if (r
== ERROR_SUCCESS
)
915 msi_export_record( handle
, rec
, 1 );
916 msiobj_release( &rec
->hdr
);
919 /* write out row 2, the column types */
920 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_TYPES
, &rec
);
921 if (r
== ERROR_SUCCESS
)
923 msi_export_record( handle
, rec
, 1 );
924 msiobj_release( &rec
->hdr
);
927 /* write out row 3, the table name + keys */
928 r
= MSI_DatabaseGetPrimaryKeys( db
, table
, &rec
);
929 if (r
== ERROR_SUCCESS
)
931 MSI_RecordSetStringW( rec
, 0, table
);
932 msi_export_record( handle
, rec
, 0 );
933 msiobj_release( &rec
->hdr
);
936 /* write out row 4 onwards, the data */
937 r
= MSI_IterateRecords( view
, 0, msi_export_row
, handle
);
938 msiobj_release( &view
->hdr
);
942 CloseHandle( handle
);
946 /***********************************************************************
947 * MsiExportDatabaseW [MSI.@]
949 * Writes a file containing the table data as tab separated ASCII.
951 * The format is as follows:
953 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
954 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
955 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
957 * Followed by the data, starting at row 1 with one row per line
959 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
961 UINT WINAPI
MsiDatabaseExportW( MSIHANDLE handle
, LPCWSTR szTable
,
962 LPCWSTR szFolder
, LPCWSTR szFilename
)
967 TRACE("%lx %s %s %s\n", handle
, debugstr_w(szTable
),
968 debugstr_w(szFolder
), debugstr_w(szFilename
));
970 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
973 IWineMsiRemoteDatabase
*remote_database
;
975 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( handle
);
976 if ( !remote_database
)
977 return ERROR_INVALID_HANDLE
;
979 IWineMsiRemoteDatabase_Release( remote_database
);
980 WARN("MsiDatabaseExport not allowed during a custom action!\n");
982 return ERROR_SUCCESS
;
985 r
= MSI_DatabaseExport( db
, szTable
, szFolder
, szFilename
);
986 msiobj_release( &db
->hdr
);
990 UINT WINAPI
MsiDatabaseExportA( MSIHANDLE handle
, LPCSTR szTable
,
991 LPCSTR szFolder
, LPCSTR szFilename
)
993 LPWSTR path
= NULL
, file
= NULL
, table
= NULL
;
994 UINT r
= ERROR_OUTOFMEMORY
;
996 TRACE("%lx %s %s %s\n", handle
, debugstr_a(szTable
),
997 debugstr_a(szFolder
), debugstr_a(szFilename
));
1001 table
= strdupAtoW( szTable
);
1008 path
= strdupAtoW( szFolder
);
1015 file
= strdupAtoW( szFilename
);
1020 r
= MsiDatabaseExportW( handle
, table
, path
, file
);
1030 MSIDBSTATE WINAPI
MsiGetDatabaseState( MSIHANDLE handle
)
1032 MSIDBSTATE ret
= MSIDBSTATE_READ
;
1035 TRACE("%ld\n", handle
);
1037 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
1040 IWineMsiRemoteDatabase
*remote_database
;
1042 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( handle
);
1043 if ( !remote_database
)
1044 return MSIDBSTATE_ERROR
;
1046 IWineMsiRemoteDatabase_Release( remote_database
);
1047 WARN("MsiGetDatabaseState not allowed during a custom action!\n");
1049 return MSIDBSTATE_READ
;
1052 if (db
->mode
!= MSIDBOPEN_READONLY
)
1053 ret
= MSIDBSTATE_WRITE
;
1054 msiobj_release( &db
->hdr
);
1059 typedef struct _msi_remote_database_impl
{
1060 const IWineMsiRemoteDatabaseVtbl
*lpVtbl
;
1063 } msi_remote_database_impl
;
1065 static inline msi_remote_database_impl
* mrd_from_IWineMsiRemoteDatabase( IWineMsiRemoteDatabase
* iface
)
1067 return (msi_remote_database_impl
*)iface
;
1070 static HRESULT WINAPI
mrd_QueryInterface( IWineMsiRemoteDatabase
*iface
,
1071 REFIID riid
,LPVOID
*ppobj
)
1073 if( IsEqualCLSID( riid
, &IID_IUnknown
) ||
1074 IsEqualCLSID( riid
, &IID_IWineMsiRemoteDatabase
) )
1076 IUnknown_AddRef( iface
);
1081 return E_NOINTERFACE
;
1084 static ULONG WINAPI
mrd_AddRef( IWineMsiRemoteDatabase
*iface
)
1086 msi_remote_database_impl
* This
= mrd_from_IWineMsiRemoteDatabase( iface
);
1088 return InterlockedIncrement( &This
->refs
);
1091 static ULONG WINAPI
mrd_Release( IWineMsiRemoteDatabase
*iface
)
1093 msi_remote_database_impl
* This
= mrd_from_IWineMsiRemoteDatabase( iface
);
1096 r
= InterlockedDecrement( &This
->refs
);
1099 MsiCloseHandle( This
->database
);
1105 HRESULT WINAPI
mrd_IsTablePersistent( IWineMsiRemoteDatabase
*iface
,
1106 BSTR table
, MSICONDITION
*persistent
)
1108 msi_remote_database_impl
*This
= mrd_from_IWineMsiRemoteDatabase( iface
);
1109 *persistent
= MsiDatabaseIsTablePersistentW(This
->database
, (LPWSTR
)table
);
1113 HRESULT WINAPI
mrd_GetPrimaryKeys( IWineMsiRemoteDatabase
*iface
,
1114 BSTR table
, MSIHANDLE
*keys
)
1116 msi_remote_database_impl
*This
= mrd_from_IWineMsiRemoteDatabase( iface
);
1117 UINT r
= MsiDatabaseGetPrimaryKeysW(This
->database
, (LPWSTR
)table
, keys
);
1118 return HRESULT_FROM_WIN32(r
);
1121 HRESULT WINAPI
mrd_GetSummaryInformation( IWineMsiRemoteDatabase
*iface
,
1122 UINT updatecount
, MSIHANDLE
*suminfo
)
1124 msi_remote_database_impl
*This
= mrd_from_IWineMsiRemoteDatabase( iface
);
1125 UINT r
= MsiGetSummaryInformationW(This
->database
, NULL
, updatecount
, suminfo
);
1126 return HRESULT_FROM_WIN32(r
);
1129 HRESULT WINAPI
mrd_OpenView( IWineMsiRemoteDatabase
*iface
,
1130 BSTR query
, MSIHANDLE
*view
)
1132 msi_remote_database_impl
*This
= mrd_from_IWineMsiRemoteDatabase( iface
);
1133 UINT r
= MsiDatabaseOpenViewW(This
->database
, (LPWSTR
)query
, view
);
1134 return HRESULT_FROM_WIN32(r
);
1137 static HRESULT WINAPI
mrd_SetMsiHandle( IWineMsiRemoteDatabase
*iface
, MSIHANDLE handle
)
1139 msi_remote_database_impl
* This
= mrd_from_IWineMsiRemoteDatabase( iface
);
1140 This
->database
= handle
;
1144 static const IWineMsiRemoteDatabaseVtbl msi_remote_database_vtbl
=
1149 mrd_IsTablePersistent
,
1151 mrd_GetSummaryInformation
,
1156 HRESULT
create_msi_remote_database( IUnknown
*pOuter
, LPVOID
*ppObj
)
1158 msi_remote_database_impl
*This
;
1160 This
= msi_alloc( sizeof *This
);
1162 return E_OUTOFMEMORY
;
1164 This
->lpVtbl
= &msi_remote_database_vtbl
;