advapi32: Add some tests for RegDeleteTreeA.
[wine/multimedia.git] / dlls / msi / record.c
blobbf01b209ec89221d17bf057614a1ea96878d866e
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-2004 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
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winerror.h"
29 #include "wine/debug.h"
30 #include "msi.h"
31 #include "msiquery.h"
32 #include "msipriv.h"
33 #include "objidl.h"
34 #include "winnls.h"
35 #include "ole2.h"
37 #include "winreg.h"
38 #include "shlwapi.h"
40 #include "query.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
44 #define MSIFIELD_NULL 0
45 #define MSIFIELD_INT 1
46 #define MSIFIELD_WSTR 3
47 #define MSIFIELD_STREAM 4
49 static void MSI_FreeField( MSIFIELD *field )
51 switch( field->type )
53 case MSIFIELD_NULL:
54 case MSIFIELD_INT:
55 break;
56 case MSIFIELD_WSTR:
57 msi_free( field->u.szwVal);
58 break;
59 case MSIFIELD_STREAM:
60 IStream_Release( field->u.stream );
61 break;
62 default:
63 ERR("Invalid field type %d\n", field->type);
67 static void MSI_CloseRecord( MSIOBJECTHDR *arg )
69 MSIRECORD *rec = (MSIRECORD *) arg;
70 UINT i;
72 for( i=0; i<=rec->count; i++ )
73 MSI_FreeField( &rec->fields[i] );
76 MSIRECORD *MSI_CreateRecord( unsigned int cParams )
78 MSIRECORD *rec;
79 UINT len;
81 TRACE("%d\n", cParams);
83 if( cParams>65535 )
84 return NULL;
86 len = sizeof (MSIRECORD) + sizeof (MSIFIELD)*cParams;
87 rec = alloc_msiobject( MSIHANDLETYPE_RECORD, len, MSI_CloseRecord );
88 if( rec )
89 rec->count = cParams;
90 return rec;
93 MSIHANDLE WINAPI MsiCreateRecord( unsigned int cParams )
95 MSIRECORD *rec;
96 MSIHANDLE ret = 0;
98 TRACE("%d\n", cParams);
100 rec = MSI_CreateRecord( cParams );
101 if( rec )
103 ret = alloc_msihandle( &rec->hdr );
104 msiobj_release( &rec->hdr );
106 return ret;
109 unsigned int MSI_RecordGetFieldCount( MSIRECORD *rec )
111 return rec->count;
114 unsigned int WINAPI MsiRecordGetFieldCount( MSIHANDLE handle )
116 MSIRECORD *rec;
117 UINT ret;
119 TRACE("%ld\n", handle );
121 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
122 if( !rec )
123 return -1;
125 msiobj_lock( &rec->hdr );
126 ret = MSI_RecordGetFieldCount( rec );
127 msiobj_unlock( &rec->hdr );
128 msiobj_release( &rec->hdr );
130 return ret;
133 static BOOL string2intW( LPCWSTR str, int *out )
135 int x = 0;
136 LPCWSTR p = str;
138 if( *p == '-' ) /* skip the minus sign */
139 p++;
140 while ( *p )
142 if( (*p < '0') || (*p > '9') )
143 return FALSE;
144 x *= 10;
145 x += (*p - '0');
146 p++;
149 if( str[0] == '-' ) /* check if it's negative */
150 x = -x;
151 *out = x;
153 return TRUE;
156 UINT MSI_RecordCopyField( MSIRECORD *in_rec, unsigned int in_n,
157 MSIRECORD *out_rec, unsigned int out_n )
159 UINT r = ERROR_SUCCESS;
161 msiobj_lock( &in_rec->hdr );
163 if ( in_n > in_rec->count || out_n > out_rec->count )
164 r = ERROR_FUNCTION_FAILED;
165 else if ( in_rec != out_rec || in_n != out_n )
167 LPWSTR str;
168 MSIFIELD *in, *out;
170 in = &in_rec->fields[in_n];
171 out = &out_rec->fields[out_n];
173 switch ( in->type )
175 case MSIFIELD_NULL:
176 break;
177 case MSIFIELD_INT:
178 out->u.iVal = in->u.iVal;
179 break;
180 case MSIFIELD_WSTR:
181 str = strdupW( in->u.szwVal );
182 if ( !str )
183 r = ERROR_OUTOFMEMORY;
184 else
185 out->u.szwVal = str;
186 break;
187 case MSIFIELD_STREAM:
188 IStream_AddRef( in->u.stream );
189 out->u.stream = in->u.stream;
190 break;
191 default:
192 ERR("invalid field type %d\n", in->type);
194 if (r == ERROR_SUCCESS)
195 out->type = in->type;
198 msiobj_unlock( &in_rec->hdr );
200 return r;
203 int MSI_RecordGetInteger( MSIRECORD *rec, unsigned int iField)
205 int ret = 0;
207 TRACE("%p %d\n", rec, iField );
209 if( iField > rec->count )
210 return MSI_NULL_INTEGER;
212 switch( rec->fields[iField].type )
214 case MSIFIELD_INT:
215 return rec->fields[iField].u.iVal;
216 case MSIFIELD_WSTR:
217 if( string2intW( rec->fields[iField].u.szwVal, &ret ) )
218 return ret;
219 return MSI_NULL_INTEGER;
220 default:
221 break;
224 return MSI_NULL_INTEGER;
227 int WINAPI MsiRecordGetInteger( MSIHANDLE handle, unsigned int iField)
229 MSIRECORD *rec;
230 UINT ret;
232 TRACE("%ld %d\n", handle, iField );
234 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
235 if( !rec )
236 return MSI_NULL_INTEGER;
238 msiobj_lock( &rec->hdr );
239 ret = MSI_RecordGetInteger( rec, iField );
240 msiobj_unlock( &rec->hdr );
241 msiobj_release( &rec->hdr );
243 return ret;
246 UINT WINAPI MsiRecordClearData( MSIHANDLE handle )
248 MSIRECORD *rec;
249 UINT i;
251 TRACE("%ld\n", handle );
253 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
254 if( !rec )
255 return ERROR_INVALID_HANDLE;
257 msiobj_lock( &rec->hdr );
258 for( i=0; i<=rec->count; i++)
260 MSI_FreeField( &rec->fields[i] );
261 rec->fields[i].type = MSIFIELD_NULL;
262 rec->fields[i].u.iVal = 0;
264 msiobj_unlock( &rec->hdr );
265 msiobj_release( &rec->hdr );
267 return ERROR_SUCCESS;
270 UINT MSI_RecordSetInteger( MSIRECORD *rec, unsigned int iField, int iVal )
272 TRACE("%p %u %d\n", rec, iField, iVal);
274 if( iField > rec->count )
275 return ERROR_INVALID_PARAMETER;
277 MSI_FreeField( &rec->fields[iField] );
278 rec->fields[iField].type = MSIFIELD_INT;
279 rec->fields[iField].u.iVal = iVal;
281 return ERROR_SUCCESS;
284 UINT WINAPI MsiRecordSetInteger( MSIHANDLE handle, unsigned int iField, int iVal )
286 MSIRECORD *rec;
287 UINT ret;
289 TRACE("%ld %u %d\n", handle, iField, iVal);
291 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
292 if( !rec )
293 return ERROR_INVALID_HANDLE;
295 msiobj_lock( &rec->hdr );
296 ret = MSI_RecordSetInteger( rec, iField, iVal );
297 msiobj_unlock( &rec->hdr );
298 msiobj_release( &rec->hdr );
299 return ret;
302 BOOL MSI_RecordIsNull( MSIRECORD *rec, unsigned int iField )
304 BOOL r = TRUE;
306 TRACE("%p %d\n", rec, iField );
308 r = ( iField > rec->count ) ||
309 ( rec->fields[iField].type == MSIFIELD_NULL );
311 return r;
314 BOOL WINAPI MsiRecordIsNull( MSIHANDLE handle, unsigned int iField )
316 MSIRECORD *rec;
317 UINT ret;
319 TRACE("%ld %d\n", handle, iField );
321 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
322 if( !rec )
323 return 0;
324 msiobj_lock( &rec->hdr );
325 ret = MSI_RecordIsNull( rec, iField );
326 msiobj_unlock( &rec->hdr );
327 msiobj_release( &rec->hdr );
328 return ret;
332 UINT MSI_RecordGetStringA(MSIRECORD *rec, unsigned int iField,
333 LPSTR szValue, DWORD *pcchValue)
335 UINT len=0, ret;
336 CHAR buffer[16];
338 TRACE("%p %d %p %p\n", rec, iField, szValue, pcchValue);
340 if( iField > rec->count )
341 return ERROR_INVALID_PARAMETER;
343 ret = ERROR_SUCCESS;
344 switch( rec->fields[iField].type )
346 case MSIFIELD_INT:
347 wsprintfA(buffer, "%d", rec->fields[iField].u.iVal);
348 len = lstrlenA( buffer );
349 if (szValue)
350 lstrcpynA(szValue, buffer, *pcchValue);
351 break;
352 case MSIFIELD_WSTR:
353 len = WideCharToMultiByte( CP_ACP, 0, rec->fields[iField].u.szwVal, -1,
354 NULL, 0 , NULL, NULL);
355 WideCharToMultiByte( CP_ACP, 0, rec->fields[iField].u.szwVal, -1,
356 szValue, *pcchValue, NULL, NULL);
357 if( szValue && *pcchValue && len>*pcchValue )
358 szValue[*pcchValue-1] = 0;
359 if( len )
360 len--;
361 break;
362 case MSIFIELD_NULL:
363 if( *pcchValue > 0 )
364 szValue[0] = 0;
365 break;
366 default:
367 ret = ERROR_INVALID_PARAMETER;
368 break;
371 if( szValue && *pcchValue <= len )
372 ret = ERROR_MORE_DATA;
373 *pcchValue = len;
375 return ret;
378 UINT WINAPI MsiRecordGetStringA(MSIHANDLE handle, unsigned int iField,
379 LPSTR szValue, DWORD *pcchValue)
381 MSIRECORD *rec;
382 UINT ret;
384 TRACE("%ld %d %p %p\n", handle, iField, szValue, pcchValue);
386 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
387 if( !rec )
388 return ERROR_INVALID_HANDLE;
389 msiobj_lock( &rec->hdr );
390 ret = MSI_RecordGetStringA( rec, iField, szValue, pcchValue);
391 msiobj_unlock( &rec->hdr );
392 msiobj_release( &rec->hdr );
393 return ret;
396 const WCHAR *MSI_RecordGetString( MSIRECORD *rec, unsigned int iField )
398 if( iField > rec->count )
399 return NULL;
401 if( rec->fields[iField].type != MSIFIELD_WSTR )
402 return NULL;
404 return rec->fields[iField].u.szwVal;
407 UINT MSI_RecordGetStringW(MSIRECORD *rec, unsigned int iField,
408 LPWSTR szValue, DWORD *pcchValue)
410 UINT len=0, ret;
411 WCHAR buffer[16];
412 static const WCHAR szFormat[] = { '%','d',0 };
414 TRACE("%p %d %p %p\n", rec, iField, szValue, pcchValue);
416 if( iField > rec->count )
417 return ERROR_INVALID_PARAMETER;
419 ret = ERROR_SUCCESS;
420 switch( rec->fields[iField].type )
422 case MSIFIELD_INT:
423 wsprintfW(buffer, szFormat, rec->fields[iField].u.iVal);
424 len = lstrlenW( buffer );
425 if (szValue)
426 lstrcpynW(szValue, buffer, *pcchValue);
427 break;
428 case MSIFIELD_WSTR:
429 len = lstrlenW( rec->fields[iField].u.szwVal );
430 if (szValue)
431 lstrcpynW(szValue, rec->fields[iField].u.szwVal, *pcchValue);
432 break;
433 case MSIFIELD_NULL:
434 len = 1;
435 if( szValue && *pcchValue > 0 )
436 szValue[0] = 0;
437 default:
438 break;
441 if( szValue && *pcchValue <= len )
442 ret = ERROR_MORE_DATA;
443 *pcchValue = len;
445 return ret;
448 UINT WINAPI MsiRecordGetStringW(MSIHANDLE handle, unsigned int iField,
449 LPWSTR szValue, DWORD *pcchValue)
451 MSIRECORD *rec;
452 UINT ret;
454 TRACE("%ld %d %p %p\n", handle, iField, szValue, pcchValue);
456 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
457 if( !rec )
458 return ERROR_INVALID_HANDLE;
460 msiobj_lock( &rec->hdr );
461 ret = MSI_RecordGetStringW( rec, iField, szValue, pcchValue );
462 msiobj_unlock( &rec->hdr );
463 msiobj_release( &rec->hdr );
464 return ret;
467 static UINT msi_get_stream_size( IStream *stm )
469 STATSTG stat;
470 HRESULT r;
472 r = IStream_Stat( stm, &stat, STATFLAG_NONAME );
473 if( FAILED(r) )
474 return 0;
475 return stat.cbSize.QuadPart;
478 UINT MSI_RecordDataSize(MSIRECORD *rec, unsigned int iField)
480 TRACE("%p %d\n", rec, iField);
482 if( iField > rec->count )
483 return 0;
485 switch( rec->fields[iField].type )
487 case MSIFIELD_INT:
488 return sizeof (INT);
489 case MSIFIELD_WSTR:
490 return lstrlenW( rec->fields[iField].u.szwVal );
491 case MSIFIELD_NULL:
492 break;
493 case MSIFIELD_STREAM:
494 return msi_get_stream_size( rec->fields[iField].u.stream );
496 return 0;
499 UINT WINAPI MsiRecordDataSize(MSIHANDLE handle, unsigned int iField)
501 MSIRECORD *rec;
502 UINT ret;
504 TRACE("%ld %d\n", handle, iField);
506 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
507 if( !rec )
508 return 0;
509 msiobj_lock( &rec->hdr );
510 ret = MSI_RecordDataSize( rec, iField);
511 msiobj_unlock( &rec->hdr );
512 msiobj_release( &rec->hdr );
513 return ret;
516 UINT MSI_RecordSetStringA( MSIRECORD *rec, unsigned int iField, LPCSTR szValue )
518 LPWSTR str;
520 TRACE("%p %d %s\n", rec, iField, debugstr_a(szValue));
522 if( iField > rec->count )
523 return ERROR_INVALID_FIELD;
525 MSI_FreeField( &rec->fields[iField] );
526 if( szValue && szValue[0] )
528 str = strdupAtoW( szValue );
529 rec->fields[iField].type = MSIFIELD_WSTR;
530 rec->fields[iField].u.szwVal = str;
532 else
534 rec->fields[iField].type = MSIFIELD_NULL;
535 rec->fields[iField].u.szwVal = NULL;
538 return 0;
541 UINT WINAPI MsiRecordSetStringA( MSIHANDLE handle, unsigned int iField, LPCSTR szValue )
543 MSIRECORD *rec;
544 UINT ret;
546 TRACE("%ld %d %s\n", handle, iField, debugstr_a(szValue));
548 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
549 if( !rec )
550 return ERROR_INVALID_HANDLE;
551 msiobj_lock( &rec->hdr );
552 ret = MSI_RecordSetStringA( rec, iField, szValue );
553 msiobj_unlock( &rec->hdr );
554 msiobj_release( &rec->hdr );
555 return ret;
558 UINT MSI_RecordSetStringW( MSIRECORD *rec, unsigned int iField, LPCWSTR szValue )
560 LPWSTR str;
562 TRACE("%p %d %s\n", rec, iField, debugstr_w(szValue));
564 if( iField > rec->count )
565 return ERROR_INVALID_FIELD;
567 MSI_FreeField( &rec->fields[iField] );
569 if( szValue && szValue[0] )
571 str = strdupW( szValue );
572 rec->fields[iField].type = MSIFIELD_WSTR;
573 rec->fields[iField].u.szwVal = str;
575 else
577 rec->fields[iField].type = MSIFIELD_NULL;
578 rec->fields[iField].u.szwVal = NULL;
581 return 0;
584 UINT WINAPI MsiRecordSetStringW( MSIHANDLE handle, unsigned int iField, LPCWSTR szValue )
586 MSIRECORD *rec;
587 UINT ret;
589 TRACE("%ld %d %s\n", handle, iField, debugstr_w(szValue));
591 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
592 if( !rec )
593 return ERROR_INVALID_HANDLE;
595 msiobj_lock( &rec->hdr );
596 ret = MSI_RecordSetStringW( rec, iField, szValue );
597 msiobj_unlock( &rec->hdr );
598 msiobj_release( &rec->hdr );
599 return ret;
602 /* read the data in a file into an IStream */
603 static UINT RECORD_StreamFromFile(LPCWSTR szFile, IStream **pstm)
605 DWORD sz, szHighWord = 0, read;
606 HANDLE handle;
607 HGLOBAL hGlob = 0;
608 HRESULT hr;
609 ULARGE_INTEGER ulSize;
611 TRACE("reading %s\n", debugstr_w(szFile));
613 /* read the file into memory */
614 handle = CreateFileW(szFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
615 if( handle == INVALID_HANDLE_VALUE )
616 return GetLastError();
617 sz = GetFileSize(handle, &szHighWord);
618 if( sz != INVALID_FILE_SIZE && szHighWord == 0 )
620 hGlob = GlobalAlloc(GMEM_FIXED, sz);
621 if( hGlob )
623 BOOL r = ReadFile(handle, hGlob, sz, &read, NULL);
624 if( !r )
626 GlobalFree(hGlob);
627 hGlob = 0;
631 CloseHandle(handle);
632 if( !hGlob )
633 return ERROR_FUNCTION_FAILED;
635 /* make a stream out of it, and set the correct file size */
636 hr = CreateStreamOnHGlobal(hGlob, TRUE, pstm);
637 if( FAILED( hr ) )
639 GlobalFree(hGlob);
640 return ERROR_FUNCTION_FAILED;
643 /* set the correct size - CreateStreamOnHGlobal screws it up */
644 ulSize.QuadPart = sz;
645 IStream_SetSize(*pstm, ulSize);
647 TRACE("read %s, %d bytes into IStream %p\n", debugstr_w(szFile), sz, *pstm);
649 return ERROR_SUCCESS;
652 UINT MSI_RecordSetStreamW(MSIRECORD *rec, unsigned int iField, LPCWSTR szFilename)
654 IStream *stm = NULL;
655 HRESULT r;
657 if( (iField == 0) || (iField > rec->count) )
658 return ERROR_INVALID_PARAMETER;
660 /* no filename means we should seek back to the start of the stream */
661 if( !szFilename )
663 LARGE_INTEGER ofs;
664 ULARGE_INTEGER cur;
666 if( rec->fields[iField].type != MSIFIELD_STREAM )
667 return ERROR_INVALID_FIELD;
669 stm = rec->fields[iField].u.stream;
670 if( !stm )
671 return ERROR_INVALID_FIELD;
673 ofs.QuadPart = 0;
674 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
675 if( FAILED( r ) )
676 return ERROR_FUNCTION_FAILED;
678 else
680 /* read the file into a stream and save the stream in the record */
681 r = RECORD_StreamFromFile(szFilename, &stm);
682 if( r != ERROR_SUCCESS )
683 return r;
685 /* if all's good, store it in the record */
686 MSI_FreeField( &rec->fields[iField] );
687 rec->fields[iField].type = MSIFIELD_STREAM;
688 rec->fields[iField].u.stream = stm;
691 return ERROR_SUCCESS;
694 UINT WINAPI MsiRecordSetStreamA(MSIHANDLE hRecord, unsigned int iField, LPCSTR szFilename)
696 LPWSTR wstr = NULL;
697 UINT ret;
699 TRACE("%ld %d %s\n", hRecord, iField, debugstr_a(szFilename));
701 if( szFilename )
703 wstr = strdupAtoW( szFilename );
704 if( !wstr )
705 return ERROR_OUTOFMEMORY;
707 ret = MsiRecordSetStreamW(hRecord, iField, wstr);
708 msi_free(wstr);
710 return ret;
713 UINT WINAPI MsiRecordSetStreamW(MSIHANDLE handle, unsigned int iField, LPCWSTR szFilename)
715 MSIRECORD *rec;
716 UINT ret;
718 TRACE("%ld %d %s\n", handle, iField, debugstr_w(szFilename));
720 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
721 if( !rec )
722 return ERROR_INVALID_HANDLE;
724 msiobj_lock( &rec->hdr );
725 ret = MSI_RecordSetStreamW( rec, iField, szFilename );
726 msiobj_unlock( &rec->hdr );
727 msiobj_release( &rec->hdr );
728 return ret;
731 UINT MSI_RecordReadStream(MSIRECORD *rec, unsigned int iField, char *buf, DWORD *sz)
733 ULONG count;
734 HRESULT r;
735 IStream *stm;
737 TRACE("%p %d %p %p\n", rec, iField, buf, sz);
739 if( !sz )
740 return ERROR_INVALID_PARAMETER;
742 if( iField > rec->count)
743 return ERROR_INVALID_PARAMETER;
745 if( rec->fields[iField].type != MSIFIELD_STREAM )
746 return ERROR_INVALID_DATATYPE;
748 stm = rec->fields[iField].u.stream;
749 if( !stm )
750 return ERROR_INVALID_PARAMETER;
752 /* if there's no buffer pointer, calculate the length to the end */
753 if( !buf )
755 LARGE_INTEGER ofs;
756 ULARGE_INTEGER end, cur;
758 ofs.QuadPart = cur.QuadPart = 0;
759 end.QuadPart = 0;
760 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
761 IStream_Seek( stm, ofs, STREAM_SEEK_END, &end );
762 ofs.QuadPart = cur.QuadPart;
763 IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
764 *sz = end.QuadPart - cur.QuadPart;
766 return ERROR_SUCCESS;
769 /* read the data */
770 count = 0;
771 r = IStream_Read( stm, buf, *sz, &count );
772 if( FAILED( r ) )
774 *sz = 0;
775 return ERROR_FUNCTION_FAILED;
778 *sz = count;
780 return ERROR_SUCCESS;
783 UINT WINAPI MsiRecordReadStream(MSIHANDLE handle, unsigned int iField, char *buf, DWORD *sz)
785 MSIRECORD *rec;
786 UINT ret;
788 TRACE("%ld %d %p %p\n", handle, iField, buf, sz);
790 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
791 if( !rec )
792 return ERROR_INVALID_HANDLE;
793 msiobj_lock( &rec->hdr );
794 ret = MSI_RecordReadStream( rec, iField, buf, sz );
795 msiobj_unlock( &rec->hdr );
796 msiobj_release( &rec->hdr );
797 return ret;
800 UINT MSI_RecordSetIStream( MSIRECORD *rec, unsigned int iField, IStream *stm )
802 TRACE("%p %d %p\n", rec, iField, stm);
804 if( iField > rec->count )
805 return ERROR_INVALID_FIELD;
807 MSI_FreeField( &rec->fields[iField] );
809 rec->fields[iField].type = MSIFIELD_STREAM;
810 rec->fields[iField].u.stream = stm;
811 IStream_AddRef( stm );
813 return ERROR_SUCCESS;
816 UINT MSI_RecordGetIStream( MSIRECORD *rec, unsigned int iField, IStream **pstm)
818 TRACE("%p %d %p\n", rec, iField, pstm);
820 if( iField > rec->count )
821 return ERROR_INVALID_FIELD;
823 if( rec->fields[iField].type != MSIFIELD_STREAM )
824 return ERROR_INVALID_FIELD;
826 *pstm = rec->fields[iField].u.stream;
827 IStream_AddRef( *pstm );
829 return ERROR_SUCCESS;
832 static UINT msi_dump_stream_to_file( IStream *stm, LPCWSTR name )
834 ULARGE_INTEGER size;
835 LARGE_INTEGER pos;
836 IStream *out;
837 DWORD stgm;
838 HRESULT r;
840 stgm = STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_FAILIFTHERE;
841 r = SHCreateStreamOnFileW( name, stgm, &out );
842 if( FAILED( r ) )
843 return ERROR_FUNCTION_FAILED;
845 pos.QuadPart = 0;
846 r = IStream_Seek( stm, pos, STREAM_SEEK_END, &size );
847 if( FAILED( r ) )
848 goto end;
850 pos.QuadPart = 0;
851 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
852 if( FAILED( r ) )
853 goto end;
855 r = IStream_CopyTo( stm, out, size, NULL, NULL );
857 end:
858 IStream_Release( out );
859 if( FAILED( r ) )
860 return ERROR_FUNCTION_FAILED;
861 return ERROR_SUCCESS;
864 UINT MSI_RecordStreamToFile( MSIRECORD *rec, unsigned int iField, LPCWSTR name )
866 IStream *stm = NULL;
867 UINT r;
869 TRACE("%p %u %s\n", rec, iField, debugstr_w(name));
871 msiobj_lock( &rec->hdr );
873 r = MSI_RecordGetIStream( rec, iField, &stm );
874 if( r == ERROR_SUCCESS )
876 r = msi_dump_stream_to_file( stm, name );
877 IStream_Release( stm );
880 msiobj_unlock( &rec->hdr );
882 return r;