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
24 #define NONAMELESSUNION
30 #define NO_SHLWAPI_REG
31 #define NO_SHLWAPI_PATH
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
37 #define STGM_ACCESS_MODE(stgm) ((stgm)&0x0000f)
38 #define STGM_SHARE_MODE(stgm) ((stgm)&0x000f0)
39 #define STGM_CREATE_MODE(stgm) ((stgm)&0x0f000)
41 /* Layout of ISHFileStream object */
44 IStream IStream_iface
;
53 static inline ISHFileStream
*impl_from_IStream(IStream
*iface
)
55 return CONTAINING_RECORD(iface
, ISHFileStream
, IStream_iface
);
58 /**************************************************************************
59 * IStream_fnQueryInterface
61 static HRESULT WINAPI
IStream_fnQueryInterface(IStream
*iface
, REFIID riid
, LPVOID
*ppvObj
)
63 ISHFileStream
*This
= impl_from_IStream(iface
);
65 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(riid
), ppvObj
);
69 if(IsEqualIID(riid
, &IID_IUnknown
) ||
70 IsEqualIID(riid
, &IID_IStream
))
72 IStream_AddRef(iface
);
79 /**************************************************************************
82 static ULONG WINAPI
IStream_fnAddRef(IStream
*iface
)
84 ISHFileStream
*This
= impl_from_IStream(iface
);
85 ULONG refCount
= InterlockedIncrement(&This
->ref
);
87 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
92 /**************************************************************************
95 static ULONG WINAPI
IStream_fnRelease(IStream
*iface
)
97 ISHFileStream
*This
= impl_from_IStream(iface
);
98 ULONG refCount
= InterlockedDecrement(&This
->ref
);
100 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
104 IStream_Commit(iface
, 0); /* If ever buffered, this will be needed */
105 LocalFree(This
->lpszPath
);
106 CloseHandle(This
->hFile
);
107 HeapFree(GetProcessHeap(), 0, This
);
113 /**************************************************************************
116 static HRESULT WINAPI
IStream_fnRead(IStream
*iface
, void* pv
, ULONG cb
, ULONG
* pcbRead
)
118 ISHFileStream
*This
= impl_from_IStream(iface
);
121 TRACE("(%p,%p,%u,%p)\n", This
, pv
, cb
, pcbRead
);
123 if (!ReadFile(This
->hFile
, pv
, cb
, &dwRead
, NULL
))
125 WARN("error %d reading file\n", GetLastError());
130 return dwRead
== cb
? S_OK
: S_FALSE
;
133 /**************************************************************************
136 static HRESULT WINAPI
IStream_fnWrite(IStream
*iface
, const void* pv
, ULONG cb
, ULONG
* pcbWritten
)
138 ISHFileStream
*This
= impl_from_IStream(iface
);
141 TRACE("(%p,%p,%u,%p)\n", This
, pv
, cb
, pcbWritten
);
143 switch (STGM_ACCESS_MODE(This
->dwMode
))
149 return STG_E_ACCESSDENIED
;
152 if (!WriteFile(This
->hFile
, pv
, cb
, &dwWritten
, NULL
))
153 return HRESULT_FROM_WIN32(GetLastError());
156 *pcbWritten
= dwWritten
;
160 /**************************************************************************
163 static HRESULT WINAPI
IStream_fnSeek(IStream
*iface
, LARGE_INTEGER dlibMove
,
164 DWORD dwOrigin
, ULARGE_INTEGER
* pNewPos
)
166 ISHFileStream
*This
= impl_from_IStream(iface
);
169 TRACE("(%p,%s,%d,%p)\n", This
, wine_dbgstr_longlong(dlibMove
.QuadPart
), dwOrigin
, pNewPos
);
171 IStream_Commit(iface
, 0); /* If ever buffered, this will be needed */
172 dwPos
= SetFilePointer(This
->hFile
, dlibMove
.u
.LowPart
, NULL
, dwOrigin
);
173 if( dwPos
== INVALID_SET_FILE_POINTER
)
174 return HRESULT_FROM_WIN32(GetLastError());
178 pNewPos
->u
.HighPart
= 0;
179 pNewPos
->u
.LowPart
= dwPos
;
184 /**************************************************************************
187 static HRESULT WINAPI
IStream_fnSetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
189 ISHFileStream
*This
= impl_from_IStream(iface
);
191 TRACE("(%p,%s)\n", This
, wine_dbgstr_longlong(libNewSize
.QuadPart
));
193 IStream_Commit(iface
, 0); /* If ever buffered, this will be needed */
194 if( ! SetFilePointer( This
->hFile
, libNewSize
.QuadPart
, NULL
, FILE_BEGIN
) )
197 if( ! SetEndOfFile( This
->hFile
) )
203 /**************************************************************************
206 static HRESULT WINAPI
IStream_fnCopyTo(IStream
*iface
, IStream
* pstm
, ULARGE_INTEGER cb
,
207 ULARGE_INTEGER
* pcbRead
, ULARGE_INTEGER
* pcbWritten
)
209 ISHFileStream
*This
= impl_from_IStream(iface
);
214 TRACE("(%p,%p,%s,%p,%p)\n", This
, pstm
, wine_dbgstr_longlong(cb
.QuadPart
), pcbRead
, pcbWritten
);
217 pcbRead
->QuadPart
= 0;
219 pcbWritten
->QuadPart
= 0;
224 IStream_Commit(iface
, 0); /* If ever buffered, this will be needed */
227 ulSize
= cb
.QuadPart
;
230 ULONG ulLeft
, ulRead
, ulWritten
;
232 ulLeft
= ulSize
> sizeof(copyBuff
) ? sizeof(copyBuff
) : ulSize
;
235 hRet
= IStream_Read(iface
, copyBuff
, ulLeft
, &ulRead
);
236 if (FAILED(hRet
) || ulRead
== 0)
239 pcbRead
->QuadPart
+= ulRead
;
242 hRet
= IStream_Write(pstm
, copyBuff
, ulRead
, &ulWritten
);
244 pcbWritten
->QuadPart
+= ulWritten
;
245 if (FAILED(hRet
) || ulWritten
!= ulLeft
)
253 /**************************************************************************
256 static HRESULT WINAPI
IStream_fnCommit(IStream
*iface
, DWORD grfCommitFlags
)
258 ISHFileStream
*This
= impl_from_IStream(iface
);
260 TRACE("(%p,%d)\n", This
, grfCommitFlags
);
261 /* Currently unbuffered: This function is not needed */
265 /**************************************************************************
268 static HRESULT WINAPI
IStream_fnRevert(IStream
*iface
)
270 ISHFileStream
*This
= impl_from_IStream(iface
);
272 TRACE("(%p)\n", This
);
276 /**************************************************************************
277 * IStream_fnLockRegion
279 static HRESULT WINAPI
IStream_fnLockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
,
280 ULARGE_INTEGER cb
, DWORD dwLockType
)
282 ISHFileStream
*This
= impl_from_IStream(iface
);
283 TRACE("(%p,%s,%s,%d)\n", This
, wine_dbgstr_longlong(libOffset
.QuadPart
), wine_dbgstr_longlong(cb
.QuadPart
), dwLockType
);
287 /**************************************************************************
288 * IStream_fnUnlockRegion
290 static HRESULT WINAPI
IStream_fnUnlockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
,
291 ULARGE_INTEGER cb
, DWORD dwLockType
)
293 ISHFileStream
*This
= impl_from_IStream(iface
);
294 TRACE("(%p,%s,%s,%d)\n", This
, wine_dbgstr_longlong(libOffset
.QuadPart
), wine_dbgstr_longlong(cb
.QuadPart
), dwLockType
);
298 /*************************************************************************
301 static HRESULT WINAPI
IStream_fnStat(IStream
*iface
, STATSTG
* lpStat
,
304 ISHFileStream
*This
= impl_from_IStream(iface
);
305 BY_HANDLE_FILE_INFORMATION fi
;
307 TRACE("(%p,%p,%d)\n", This
, lpStat
, grfStatFlag
);
310 return STG_E_INVALIDPOINTER
;
312 memset(&fi
, 0, sizeof(fi
));
313 GetFileInformationByHandle(This
->hFile
, &fi
);
315 if (grfStatFlag
& STATFLAG_NONAME
)
316 lpStat
->pwcsName
= NULL
;
318 lpStat
->pwcsName
= StrDupW(This
->lpszPath
);
319 lpStat
->type
= This
->type
;
320 lpStat
->cbSize
.u
.LowPart
= fi
.nFileSizeLow
;
321 lpStat
->cbSize
.u
.HighPart
= fi
.nFileSizeHigh
;
322 lpStat
->mtime
= fi
.ftLastWriteTime
;
323 lpStat
->ctime
= fi
.ftCreationTime
;
324 lpStat
->atime
= fi
.ftLastAccessTime
;
325 lpStat
->grfMode
= This
->dwMode
;
326 lpStat
->grfLocksSupported
= 0;
327 memcpy(&lpStat
->clsid
, &IID_IStream
, sizeof(CLSID
));
328 lpStat
->grfStateBits
= This
->grfStateBits
;
329 lpStat
->reserved
= 0;
334 /*************************************************************************
337 static HRESULT WINAPI
IStream_fnClone(IStream
*iface
, IStream
** ppstm
)
339 ISHFileStream
*This
= impl_from_IStream(iface
);
341 TRACE("(%p,%p)\n", This
, ppstm
);
347 static const IStreamVtbl SHLWAPI_fsVTable
=
349 IStream_fnQueryInterface
,
359 IStream_fnLockRegion
,
360 IStream_fnUnlockRegion
,
365 /**************************************************************************
368 * Internal helper: Create and initialise a new file stream object.
370 static IStream
*IStream_Create(LPCWSTR lpszPath
, HANDLE hFile
, DWORD dwMode
)
372 ISHFileStream
*fileStream
;
374 fileStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream
));
375 if (!fileStream
) return NULL
;
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
))
429 dwAccess
= GENERIC_READ
|GENERIC_WRITE
;
432 dwAccess
= GENERIC_READ
;
439 switch (STGM_SHARE_MODE(dwMode
))
442 case STGM_SHARE_DENY_NONE
:
443 dwShare
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
445 case STGM_SHARE_DENY_READ
:
446 dwShare
= FILE_SHARE_WRITE
;
448 case STGM_SHARE_DENY_WRITE
:
449 dwShare
= FILE_SHARE_READ
;
451 case STGM_SHARE_EXCLUSIVE
:
458 switch(STGM_CREATE_MODE(dwMode
))
460 case STGM_FAILIFTHERE
:
461 dwCreate
= bCreate
? CREATE_NEW
: OPEN_EXISTING
;
464 dwCreate
= CREATE_ALWAYS
;
470 /* Open HANDLE to file */
471 hFile
= CreateFileW(lpszPath
, dwAccess
, dwShare
, NULL
, dwCreate
,
474 if(hFile
== INVALID_HANDLE_VALUE
)
475 return HRESULT_FROM_WIN32(GetLastError());
477 *lppStream
= IStream_Create(lpszPath
, hFile
, dwMode
);
482 return E_OUTOFMEMORY
;
487 /*************************************************************************
488 * SHCreateStreamOnFileW [SHLWAPI.@]
490 * See SHCreateStreamOnFileA.
492 HRESULT WINAPI
SHCreateStreamOnFileW(LPCWSTR lpszPath
, DWORD dwMode
,
495 TRACE("(%s,%d,%p)\n", debugstr_w(lpszPath
), dwMode
, lppStream
);
497 if (!lpszPath
|| !lppStream
)
500 if ((dwMode
& (STGM_CONVERT
|STGM_DELETEONRELEASE
|STGM_TRANSACTED
)) != 0)
503 return SHCreateStreamOnFileEx(lpszPath
, dwMode
, 0, FALSE
, NULL
, lppStream
);
506 /*************************************************************************
507 * SHCreateStreamOnFileA [SHLWAPI.@]
509 * Create a stream on a file.
512 * lpszPath [I] Path of file to create stream on
513 * dwMode [I] Mode to create stream in
514 * lppStream [O] Destination for created IStream object
517 * Success: S_OK. lppStream contains the new IStream object
518 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
520 HRESULT WINAPI
SHCreateStreamOnFileA(LPCSTR lpszPath
, DWORD dwMode
,
523 WCHAR szPath
[MAX_PATH
];
525 TRACE("(%s,%d,%p)\n", debugstr_a(lpszPath
), dwMode
, lppStream
);
528 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
530 MultiByteToWideChar(CP_ACP
, 0, lpszPath
, -1, szPath
, MAX_PATH
);
531 return SHCreateStreamOnFileW(szPath
, dwMode
, lppStream
);
534 /*************************************************************************
537 * Call IStream_Read() on a stream.
540 * lpStream [I] IStream object
541 * lpvDest [O] Destination for data read
542 * ulSize [I] Size of data to read
545 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
546 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
547 * number of bytes read does not match.
549 HRESULT WINAPI
SHIStream_Read(IStream
*lpStream
, LPVOID lpvDest
, ULONG ulSize
)
554 TRACE("(%p,%p,%u)\n", lpStream
, lpvDest
, ulSize
);
556 hRet
= IStream_Read(lpStream
, lpvDest
, ulSize
, &ulRead
);
558 if (SUCCEEDED(hRet
) && ulRead
!= ulSize
)
563 /*************************************************************************
566 * Determine if a stream has 0 length.
569 * lpStream [I] IStream object
572 * TRUE: If the stream has 0 length
575 BOOL WINAPI
SHIsEmptyStream(IStream
*lpStream
)
580 TRACE("(%p)\n", lpStream
);
582 memset(&statstg
, 0, sizeof(statstg
));
584 if(SUCCEEDED(IStream_Stat(lpStream
, &statstg
, 1)))
586 if(statstg
.cbSize
.QuadPart
)
587 bRet
= FALSE
; /* Non-Zero */
593 /* Try to read from the stream */
594 if(SUCCEEDED(SHIStream_Read(lpStream
, &dwDummy
, sizeof(dwDummy
))))
599 IStream_Seek(lpStream
, zero
, 0, NULL
);
600 bRet
= FALSE
; /* Non-Zero */
606 /*************************************************************************
609 * Call IStream_Write() on a stream.
612 * lpStream [I] IStream object
613 * lpvSrc [I] Source for data to write
614 * ulSize [I] Size of data
617 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
618 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
619 * number of bytes written does not match.
621 HRESULT WINAPI
SHIStream_Write(IStream
*lpStream
, LPCVOID lpvSrc
, ULONG ulSize
)
626 TRACE("(%p,%p,%d)\n", lpStream
, lpvSrc
, ulSize
);
628 hRet
= IStream_Write(lpStream
, lpvSrc
, ulSize
, &ulWritten
);
630 if (SUCCEEDED(hRet
) && ulWritten
!= ulSize
)
636 /*************************************************************************
639 * Seek to the start of a stream.
642 * lpStream [I] IStream object
645 * Success: S_OK. The current position within the stream is updated
646 * Failure: An HRESULT error code.
648 HRESULT WINAPI
IStream_Reset(IStream
*lpStream
)
651 TRACE("(%p)\n", lpStream
);
653 return IStream_Seek(lpStream
, zero
, 0, NULL
);
656 /*************************************************************************
659 * Get the size of a stream.
662 * lpStream [I] IStream object
663 * lpulSize [O] Destination for size
666 * Success: S_OK. lpulSize contains the size of the stream.
667 * Failure: An HRESULT error code.
669 HRESULT WINAPI
IStream_Size(IStream
*lpStream
, ULARGE_INTEGER
* lpulSize
)
674 TRACE("(%p,%p)\n", lpStream
, lpulSize
);
676 memset(&statstg
, 0, sizeof(statstg
));
678 hRet
= IStream_Stat(lpStream
, &statstg
, 1);
680 if (SUCCEEDED(hRet
) && lpulSize
)
681 *lpulSize
= statstg
.cbSize
;