2 * SHLWAPI IStream functions
4 * Copyright 2002 Jon Griffiths
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
20 #define WIN32_NO_STATUS
22 #define COM_NO_WINDOWS_H
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
33 //#include "winerror.h"
35 #define NO_SHLWAPI_REG
36 #define NO_SHLWAPI_PATH
38 #include <wine/debug.h>
40 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
42 #define STGM_ACCESS_MODE(stgm) ((stgm)&0x0000f)
43 #define STGM_SHARE_MODE(stgm) ((stgm)&0x000f0)
44 #define STGM_CREATE_MODE(stgm) ((stgm)&0x0f000)
46 /* Layout of ISHFileStream object */
49 IStream IStream_iface
;
58 static inline ISHFileStream
*impl_from_IStream(IStream
*iface
)
60 return CONTAINING_RECORD(iface
, ISHFileStream
, IStream_iface
);
63 static HRESULT WINAPI
IStream_fnCommit(IStream
*,DWORD
);
66 /**************************************************************************
67 * IStream_fnQueryInterface
69 static HRESULT WINAPI
IStream_fnQueryInterface(IStream
*iface
, REFIID riid
, LPVOID
*ppvObj
)
71 ISHFileStream
*This
= impl_from_IStream(iface
);
73 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(riid
), ppvObj
);
77 if(IsEqualIID(riid
, &IID_IUnknown
) ||
78 IsEqualIID(riid
, &IID_IStream
))
81 IStream_AddRef(iface
);
87 /**************************************************************************
90 static ULONG WINAPI
IStream_fnAddRef(IStream
*iface
)
92 ISHFileStream
*This
= impl_from_IStream(iface
);
93 ULONG refCount
= InterlockedIncrement(&This
->ref
);
95 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
100 /**************************************************************************
103 static ULONG WINAPI
IStream_fnRelease(IStream
*iface
)
105 ISHFileStream
*This
= impl_from_IStream(iface
);
106 ULONG refCount
= InterlockedDecrement(&This
->ref
);
108 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
112 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
113 LocalFree(This
->lpszPath
);
114 CloseHandle(This
->hFile
);
115 HeapFree(GetProcessHeap(), 0, This
);
121 /**************************************************************************
124 static HRESULT WINAPI
IStream_fnRead(IStream
*iface
, void* pv
, ULONG cb
, ULONG
* pcbRead
)
126 ISHFileStream
*This
= impl_from_IStream(iface
);
129 TRACE("(%p,%p,0x%08x,%p)\n", This
, pv
, cb
, pcbRead
);
131 if (!ReadFile(This
->hFile
, pv
, cb
, &dwRead
, NULL
))
133 WARN("error %d reading file\n", GetLastError());
141 /**************************************************************************
144 static HRESULT WINAPI
IStream_fnWrite(IStream
*iface
, const void* pv
, ULONG cb
, ULONG
* pcbWritten
)
146 ISHFileStream
*This
= impl_from_IStream(iface
);
149 TRACE("(%p,%p,0x%08x,%p)\n", This
, pv
, cb
, pcbWritten
);
151 switch (STGM_ACCESS_MODE(This
->dwMode
))
157 return STG_E_ACCESSDENIED
;
160 if (!WriteFile(This
->hFile
, pv
, cb
, &dwWritten
, NULL
))
161 return HRESULT_FROM_WIN32(GetLastError());
164 *pcbWritten
= dwWritten
;
168 /**************************************************************************
171 static HRESULT WINAPI
IStream_fnSeek(IStream
*iface
, LARGE_INTEGER dlibMove
,
172 DWORD dwOrigin
, ULARGE_INTEGER
* pNewPos
)
174 ISHFileStream
*This
= impl_from_IStream(iface
);
177 TRACE("(%p,%d,%d,%p)\n", This
, dlibMove
.u
.LowPart
, dwOrigin
, pNewPos
);
179 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
180 dwPos
= SetFilePointer(This
->hFile
, dlibMove
.u
.LowPart
, NULL
, dwOrigin
);
181 if( dwPos
== INVALID_SET_FILE_POINTER
)
182 return HRESULT_FROM_WIN32(GetLastError());
186 pNewPos
->u
.HighPart
= 0;
187 pNewPos
->u
.LowPart
= dwPos
;
192 /**************************************************************************
195 static HRESULT WINAPI
IStream_fnSetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
197 ISHFileStream
*This
= impl_from_IStream(iface
);
199 TRACE("(%p,%d)\n", This
, libNewSize
.u
.LowPart
);
201 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
202 if( ! SetFilePointer( This
->hFile
, libNewSize
.QuadPart
, NULL
, FILE_BEGIN
) )
205 if( ! SetEndOfFile( This
->hFile
) )
211 /**************************************************************************
214 static HRESULT WINAPI
IStream_fnCopyTo(IStream
*iface
, IStream
* pstm
, ULARGE_INTEGER cb
,
215 ULARGE_INTEGER
* pcbRead
, ULARGE_INTEGER
* pcbWritten
)
217 ISHFileStream
*This
= impl_from_IStream(iface
);
222 TRACE("(%p,%p,%d,%p,%p)\n", This
, pstm
, cb
.u
.LowPart
, pcbRead
, pcbWritten
);
225 pcbRead
->QuadPart
= 0;
227 pcbWritten
->QuadPart
= 0;
232 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
235 ulSize
= cb
.QuadPart
;
240 ulLeft
= ulSize
> sizeof(copyBuff
) ? sizeof(copyBuff
) : ulSize
;
243 hRet
= IStream_fnRead(iface
, copyBuff
, ulLeft
, &ulAmt
);
245 pcbRead
->QuadPart
+= ulAmt
;
246 if (FAILED(hRet
) || ulAmt
!= ulLeft
)
250 hRet
= IStream_fnWrite(pstm
, copyBuff
, ulLeft
, &ulAmt
);
252 pcbWritten
->QuadPart
+= ulAmt
;
253 if (FAILED(hRet
) || ulAmt
!= ulLeft
)
261 /**************************************************************************
264 static HRESULT WINAPI
IStream_fnCommit(IStream
*iface
, DWORD grfCommitFlags
)
266 ISHFileStream
*This
= impl_from_IStream(iface
);
268 TRACE("(%p,%d)\n", This
, grfCommitFlags
);
269 /* Currently unbuffered: This function is not needed */
273 /**************************************************************************
276 static HRESULT WINAPI
IStream_fnRevert(IStream
*iface
)
278 ISHFileStream
*This
= impl_from_IStream(iface
);
280 TRACE("(%p)\n", This
);
284 /**************************************************************************
285 * IStream_fnLockUnlockRegion
287 static HRESULT WINAPI
IStream_fnLockUnlockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
,
288 ULARGE_INTEGER cb
, DWORD dwLockType
)
290 ISHFileStream
*This
= impl_from_IStream(iface
);
291 TRACE("(%p,%d,%d,%d)\n", This
, libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
295 /*************************************************************************
298 static HRESULT WINAPI
IStream_fnStat(IStream
*iface
, STATSTG
* lpStat
,
301 ISHFileStream
*This
= impl_from_IStream(iface
);
302 BY_HANDLE_FILE_INFORMATION fi
;
305 TRACE("(%p,%p,%d)\n", This
, lpStat
, grfStatFlag
);
308 hRet
= STG_E_INVALIDPOINTER
;
311 memset(&fi
, 0, sizeof(fi
));
312 GetFileInformationByHandle(This
->hFile
, &fi
);
314 if (grfStatFlag
& STATFLAG_NONAME
)
315 lpStat
->pwcsName
= NULL
;
317 lpStat
->pwcsName
= StrDupW(This
->lpszPath
);
318 lpStat
->type
= This
->type
;
319 lpStat
->cbSize
.u
.LowPart
= fi
.nFileSizeLow
;
320 lpStat
->cbSize
.u
.HighPart
= fi
.nFileSizeHigh
;
321 lpStat
->mtime
= fi
.ftLastWriteTime
;
322 lpStat
->ctime
= fi
.ftCreationTime
;
323 lpStat
->atime
= fi
.ftLastAccessTime
;
324 lpStat
->grfMode
= This
->dwMode
;
325 lpStat
->grfLocksSupported
= 0;
326 memcpy(&lpStat
->clsid
, &IID_IStream
, sizeof(CLSID
));
327 lpStat
->grfStateBits
= This
->grfStateBits
;
328 lpStat
->reserved
= 0;
333 /*************************************************************************
336 static HRESULT WINAPI
IStream_fnClone(IStream
*iface
, IStream
** ppstm
)
338 ISHFileStream
*This
= impl_from_IStream(iface
);
340 TRACE("(%p)\n",This
);
346 static const IStreamVtbl SHLWAPI_fsVTable
=
348 IStream_fnQueryInterface
,
358 IStream_fnLockUnlockRegion
,
359 IStream_fnLockUnlockRegion
,
364 /**************************************************************************
367 * Internal helper: Create and initialise a new file stream object.
369 static IStream
*IStream_Create(LPCWSTR lpszPath
, HANDLE hFile
, DWORD dwMode
)
371 ISHFileStream
* fileStream
;
373 fileStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream
));
377 fileStream
->IStream_iface
.lpVtbl
= &SHLWAPI_fsVTable
;
379 fileStream
->hFile
= hFile
;
380 fileStream
->dwMode
= dwMode
;
381 fileStream
->lpszPath
= StrDupW(lpszPath
);
382 fileStream
->type
= 0; /* FIXME */
383 fileStream
->grfStateBits
= 0; /* FIXME */
385 TRACE ("Returning %p\n", fileStream
);
386 return &fileStream
->IStream_iface
;
389 /*************************************************************************
390 * SHCreateStreamOnFileEx [SHLWAPI.@]
392 * Create a stream on a file.
395 * lpszPath [I] Path of file to create stream on
396 * dwMode [I] Mode to create stream in
397 * dwAttributes [I] Attributes of the file
398 * bCreate [I] Whether to create the file if it doesn't exist
399 * lpTemplate [I] Reserved, must be NULL
400 * lppStream [O] Destination for created stream
403 * Success: S_OK. lppStream contains the new stream object
404 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
407 * This function is available in Unicode only.
409 HRESULT WINAPI
SHCreateStreamOnFileEx(LPCWSTR lpszPath
, DWORD dwMode
,
410 DWORD dwAttributes
, BOOL bCreate
,
411 IStream
*lpTemplate
, IStream
**lppStream
)
413 DWORD dwAccess
, dwShare
, dwCreate
;
416 TRACE("(%s,%d,0x%08X,%d,%p,%p)\n", debugstr_w(lpszPath
), dwMode
,
417 dwAttributes
, bCreate
, lpTemplate
, lppStream
);
419 if (!lpszPath
|| !lppStream
|| lpTemplate
)
425 switch (STGM_ACCESS_MODE(dwMode
))
428 dwAccess
= GENERIC_READ
|GENERIC_WRITE
;
431 dwAccess
= GENERIC_WRITE
;
434 dwAccess
= GENERIC_READ
;
441 switch (STGM_SHARE_MODE(dwMode
))
444 dwShare
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
446 case STGM_SHARE_DENY_READ
:
447 dwShare
= FILE_SHARE_WRITE
;
449 case STGM_SHARE_DENY_WRITE
:
450 dwShare
= FILE_SHARE_READ
;
452 case STGM_SHARE_EXCLUSIVE
:
455 case STGM_SHARE_DENY_NONE
:
456 dwShare
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
462 switch(STGM_CREATE_MODE(dwMode
))
464 case STGM_FAILIFTHERE
:
465 dwCreate
= bCreate
? CREATE_NEW
: OPEN_EXISTING
;
468 dwCreate
= CREATE_ALWAYS
;
474 /* Open HANDLE to file */
475 hFile
= CreateFileW(lpszPath
, dwAccess
, dwShare
, NULL
, dwCreate
,
478 if(hFile
== INVALID_HANDLE_VALUE
)
479 return HRESULT_FROM_WIN32(GetLastError());
481 *lppStream
= IStream_Create(lpszPath
, hFile
, dwMode
);
486 return E_OUTOFMEMORY
;
491 /*************************************************************************
492 * SHCreateStreamOnFileW [SHLWAPI.@]
494 * See SHCreateStreamOnFileA.
496 HRESULT WINAPI
SHCreateStreamOnFileW(LPCWSTR lpszPath
, DWORD dwMode
,
499 TRACE("(%s,%d,%p)\n", debugstr_w(lpszPath
), dwMode
, lppStream
);
501 if (!lpszPath
|| !lppStream
)
504 if ((dwMode
& (STGM_CONVERT
|STGM_DELETEONRELEASE
|STGM_TRANSACTED
)) != 0)
507 return SHCreateStreamOnFileEx(lpszPath
, dwMode
, 0, FALSE
, NULL
, lppStream
);
510 /*************************************************************************
511 * SHCreateStreamOnFileA [SHLWAPI.@]
513 * Create a stream on a file.
516 * lpszPath [I] Path of file to create stream on
517 * dwMode [I] Mode to create stream in
518 * lppStream [O] Destination for created IStream object
521 * Success: S_OK. lppStream contains the new IStream object
522 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
524 HRESULT WINAPI
SHCreateStreamOnFileA(LPCSTR lpszPath
, DWORD dwMode
,
527 WCHAR szPath
[MAX_PATH
];
529 TRACE("(%s,%d,%p)\n", debugstr_a(lpszPath
), dwMode
, lppStream
);
532 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
534 MultiByteToWideChar(CP_ACP
, 0, lpszPath
, -1, szPath
, MAX_PATH
);
535 return SHCreateStreamOnFileW(szPath
, dwMode
, lppStream
);
538 /*************************************************************************
541 * Call IStream_Read() on a stream.
544 * lpStream [I] IStream object
545 * lpvDest [O] Destination for data read
546 * ulSize [I] Size of data to read
549 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
550 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
551 * number of bytes read does not match.
553 HRESULT WINAPI
SHIStream_Read(IStream
*lpStream
, LPVOID lpvDest
, ULONG ulSize
)
558 TRACE("(%p,%p,%d)\n", lpStream
, lpvDest
, ulSize
);
560 hRet
= IStream_Read(lpStream
, lpvDest
, ulSize
, &ulRead
);
562 if (SUCCEEDED(hRet
) && ulRead
!= ulSize
)
567 /*************************************************************************
570 * Determine if a stream has 0 length.
573 * lpStream [I] IStream object
576 * TRUE: If the stream has 0 length
579 BOOL WINAPI
SHIsEmptyStream(IStream
*lpStream
)
584 TRACE("(%p)\n", lpStream
);
586 memset(&statstg
, 0, sizeof(statstg
));
588 if(SUCCEEDED(IStream_Stat(lpStream
, &statstg
, 1)))
590 if(statstg
.cbSize
.QuadPart
)
591 bRet
= FALSE
; /* Non-Zero */
597 /* Try to read from the stream */
598 if(SUCCEEDED(SHIStream_Read(lpStream
, &dwDummy
, sizeof(dwDummy
))))
603 IStream_Seek(lpStream
, zero
, 0, NULL
);
604 bRet
= FALSE
; /* Non-Zero */
610 /*************************************************************************
613 * Call IStream_Write() on a stream.
616 * lpStream [I] IStream object
617 * lpvSrc [I] Source for data to write
618 * ulSize [I] Size of data
621 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
622 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
623 * number of bytes written does not match.
625 HRESULT WINAPI
SHIStream_Write(IStream
*lpStream
, LPCVOID lpvSrc
, ULONG ulSize
)
630 TRACE("(%p,%p,%d)\n", lpStream
, lpvSrc
, ulSize
);
632 hRet
= IStream_Write(lpStream
, lpvSrc
, ulSize
, &ulWritten
);
634 if (SUCCEEDED(hRet
) && ulWritten
!= ulSize
)
640 /*************************************************************************
643 * Seek to the start of a stream.
646 * lpStream [I] IStream object
649 * Success: S_OK. The current position within the stream is updated
650 * Failure: An HRESULT error code.
652 HRESULT WINAPI
IStream_Reset(IStream
*lpStream
)
655 TRACE("(%p)\n", lpStream
);
657 return IStream_Seek(lpStream
, zero
, 0, NULL
);
660 /*************************************************************************
663 * Get the size of a stream.
666 * lpStream [I] IStream object
667 * lpulSize [O] Destination for size
670 * Success: S_OK. lpulSize contains the size of the stream.
671 * Failure: An HRESULT error code.
673 HRESULT WINAPI
IStream_Size(IStream
*lpStream
, ULARGE_INTEGER
* lpulSize
)
678 TRACE("(%p,%p)\n", lpStream
, lpulSize
);
680 memset(&statstg
, 0, sizeof(statstg
));
682 hRet
= IStream_Stat(lpStream
, &statstg
, 1);
684 if (SUCCEEDED(hRet
) && lpulSize
)
685 *lpulSize
= statstg
.cbSize
;