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
25 #define NONAMELESSSTRUCT
31 #define NO_SHLWAPI_REG
32 #define NO_SHLWAPI_PATH
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
38 #define STGM_ACCESS_MODE(stgm) ((stgm)&0x0000f)
39 #define STGM_SHARE_MODE(stgm) ((stgm)&0x000f0)
40 #define STGM_CREATE_MODE(stgm) ((stgm)&0x0f000)
42 /* Layout of ISHFileStream object */
45 const IStreamVtbl
*lpVtbl
;
54 static HRESULT WINAPI
IStream_fnCommit(IStream
*,DWORD
);
57 /**************************************************************************
58 * IStream_fnQueryInterface
60 static HRESULT WINAPI
IStream_fnQueryInterface(IStream
*iface
, REFIID riid
, LPVOID
*ppvObj
)
62 ISHFileStream
*This
= (ISHFileStream
*)iface
;
64 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(riid
), ppvObj
);
68 if(IsEqualIID(riid
, &IID_IUnknown
) ||
69 IsEqualIID(riid
, &IID_IStream
))
72 IStream_AddRef(iface
);
78 /**************************************************************************
81 static ULONG WINAPI
IStream_fnAddRef(IStream
*iface
)
83 ISHFileStream
*This
= (ISHFileStream
*)iface
;
84 ULONG refCount
= InterlockedIncrement(&This
->ref
);
86 TRACE("(%p)->(ref before=%lu)\n",This
, refCount
- 1);
91 /**************************************************************************
94 static ULONG WINAPI
IStream_fnRelease(IStream
*iface
)
96 ISHFileStream
*This
= (ISHFileStream
*)iface
;
97 ULONG refCount
= InterlockedDecrement(&This
->ref
);
99 TRACE("(%p)->(ref before=%lu)\n",This
, refCount
+ 1);
103 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
104 LocalFree((HLOCAL
)This
->lpszPath
);
105 CloseHandle(This
->hFile
);
106 HeapFree(GetProcessHeap(), 0, This
);
112 /**************************************************************************
115 static HRESULT WINAPI
IStream_fnRead(IStream
*iface
, void* pv
, ULONG cb
, ULONG
* pcbRead
)
117 ISHFileStream
*This
= (ISHFileStream
*)iface
;
120 TRACE("(%p,%p,0x%08lx,%p)\n", This
, pv
, cb
, pcbRead
);
123 return STG_E_INVALIDPOINTER
;
125 if (!ReadFile(This
->hFile
, pv
, cb
, &dwRead
, NULL
))
127 ERR("error %ld reading file\n", GetLastError());
128 return HRESULT_FROM_WIN32(GetLastError());
135 /**************************************************************************
138 static HRESULT WINAPI
IStream_fnWrite(IStream
*iface
, const void* pv
, ULONG cb
, ULONG
* pcbWritten
)
140 ISHFileStream
*This
= (ISHFileStream
*)iface
;
143 TRACE("(%p,%p,0x%08lx,%p)\n", This
, pv
, cb
, pcbWritten
);
146 return STG_E_INVALIDPOINTER
;
148 switch (STGM_ACCESS_MODE(This
->dwMode
))
157 if (!WriteFile(This
->hFile
, pv
, cb
, &dwWritten
, NULL
))
158 return HRESULT_FROM_WIN32(GetLastError());
161 *pcbWritten
= dwWritten
;
165 /**************************************************************************
168 static HRESULT WINAPI
IStream_fnSeek(IStream
*iface
, LARGE_INTEGER dlibMove
,
169 DWORD dwOrigin
, ULARGE_INTEGER
* pNewPos
)
171 ISHFileStream
*This
= (ISHFileStream
*)iface
;
174 TRACE("(%p,%ld,%ld,%p)\n", This
, dlibMove
.u
.LowPart
, dwOrigin
, pNewPos
);
176 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
177 dwPos
= SetFilePointer(This
->hFile
, dlibMove
.u
.LowPart
, NULL
, dwOrigin
);
178 if( dwPos
== INVALID_SET_FILE_POINTER
)
183 pNewPos
->u
.HighPart
= 0;
184 pNewPos
->u
.LowPart
= dwPos
;
189 /**************************************************************************
192 static HRESULT WINAPI
IStream_fnSetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
194 ISHFileStream
*This
= (ISHFileStream
*)iface
;
196 TRACE("(%p,%ld)\n", This
, libNewSize
.u
.LowPart
);
198 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
199 if( ! SetFilePointer( This
->hFile
, libNewSize
.QuadPart
, NULL
, FILE_BEGIN
) )
202 if( ! SetEndOfFile( This
->hFile
) )
208 /**************************************************************************
211 static HRESULT WINAPI
IStream_fnCopyTo(IStream
*iface
, IStream
* pstm
, ULARGE_INTEGER cb
,
212 ULARGE_INTEGER
* pcbRead
, ULARGE_INTEGER
* pcbWritten
)
214 ISHFileStream
*This
= (ISHFileStream
*)iface
;
219 TRACE("(%p,%p,%ld,%p,%p)\n", This
, pstm
, cb
.u
.LowPart
, pcbRead
, pcbWritten
);
222 pcbRead
->QuadPart
= 0;
224 pcbWritten
->QuadPart
= 0;
227 return STG_E_INVALIDPOINTER
;
229 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
232 ulSize
= cb
.QuadPart
;
237 ulLeft
= ulSize
> sizeof(copyBuff
) ? sizeof(copyBuff
) : ulSize
;
240 hRet
= IStream_fnRead(iface
, copyBuff
, ulLeft
, &ulAmt
);
242 pcbRead
->QuadPart
+= ulAmt
;
243 if (FAILED(hRet
) || ulAmt
!= ulLeft
)
247 hRet
= IStream_fnWrite(pstm
, copyBuff
, ulLeft
, &ulAmt
);
249 pcbWritten
->QuadPart
+= ulAmt
;
250 if (FAILED(hRet
) || ulAmt
!= ulLeft
)
258 /**************************************************************************
261 static HRESULT WINAPI
IStream_fnCommit(IStream
*iface
, DWORD grfCommitFlags
)
263 ISHFileStream
*This
= (ISHFileStream
*)iface
;
265 TRACE("(%p,%ld)\n", This
, grfCommitFlags
);
266 /* Currently unbuffered: This function is not needed */
270 /**************************************************************************
273 static HRESULT WINAPI
IStream_fnRevert(IStream
*iface
)
275 ISHFileStream
*This
= (ISHFileStream
*)iface
;
277 TRACE("(%p)\n", This
);
281 /**************************************************************************
282 * IStream_fnLockUnlockRegion
284 static HRESULT WINAPI
IStream_fnLockUnlockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
,
285 ULARGE_INTEGER cb
, DWORD dwLockType
)
287 ISHFileStream
*This
= (ISHFileStream
*)iface
;
288 TRACE("(%p,%ld,%ld,%ld)\n", This
, libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
292 /*************************************************************************
295 static HRESULT WINAPI
IStream_fnStat(IStream
*iface
, STATSTG
* lpStat
,
298 ISHFileStream
*This
= (ISHFileStream
*)iface
;
299 BY_HANDLE_FILE_INFORMATION fi
;
302 TRACE("(%p,%p,%ld)\n", This
, lpStat
, grfStatFlag
);
305 hRet
= STG_E_INVALIDPOINTER
;
308 memset(&fi
, 0, sizeof(fi
));
309 GetFileInformationByHandle(This
->hFile
, &fi
);
311 if (grfStatFlag
& STATFLAG_NONAME
)
312 lpStat
->pwcsName
= NULL
;
314 lpStat
->pwcsName
= StrDupW(This
->lpszPath
);
315 lpStat
->type
= This
->type
;
316 lpStat
->cbSize
.u
.LowPart
= fi
.nFileSizeLow
;
317 lpStat
->cbSize
.u
.HighPart
= fi
.nFileSizeHigh
;
318 lpStat
->mtime
= fi
.ftLastWriteTime
;
319 lpStat
->ctime
= fi
.ftCreationTime
;
320 lpStat
->atime
= fi
.ftLastAccessTime
;
321 lpStat
->grfMode
= This
->dwMode
;
322 lpStat
->grfLocksSupported
= 0;
323 memcpy(&lpStat
->clsid
, &IID_IStream
, sizeof(CLSID
));
324 lpStat
->grfStateBits
= This
->grfStateBits
;
325 lpStat
->reserved
= 0;
330 /*************************************************************************
333 static HRESULT WINAPI
IStream_fnClone(IStream
*iface
, IStream
** ppstm
)
335 ISHFileStream
*This
= (ISHFileStream
*)iface
;
337 TRACE("(%p)\n",This
);
343 static const IStreamVtbl SHLWAPI_fsVTable
=
345 IStream_fnQueryInterface
,
355 IStream_fnLockUnlockRegion
,
356 IStream_fnLockUnlockRegion
,
361 /**************************************************************************
364 * Internal helper: Create and initialise a new file stream object.
366 static IStream
*IStream_Create(LPCWSTR lpszPath
, HANDLE hFile
, DWORD dwMode
)
368 ISHFileStream
* fileStream
;
370 fileStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream
));
374 fileStream
->lpVtbl
= &SHLWAPI_fsVTable
;
376 fileStream
->hFile
= hFile
;
377 fileStream
->dwMode
= dwMode
;
378 fileStream
->lpszPath
= StrDupW(lpszPath
);
379 fileStream
->type
= 0; /* FIXME */
380 fileStream
->grfStateBits
= 0; /* FIXME */
382 TRACE ("Returning %p\n", fileStream
);
383 return (IStream
*)fileStream
;
386 /*************************************************************************
387 * SHCreateStreamOnFileEx [SHLWAPI.@]
389 * Create a stream on a file.
392 * lpszPath [I] Path of file to create stream on
393 * dwMode [I] Mode to create stream in
394 * dwAttributes [I] Attributes of the file
395 * bCreate [I] Whether to create the file if it doesn't exist
396 * lpTemplate [I] Reserved, must be NULL
397 * lppStream [O] Destination for created stream
400 * Success: S_OK. lppStream contains the new stream object
401 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
404 * This function is available in Unicode only.
406 HRESULT WINAPI
SHCreateStreamOnFileEx(LPCWSTR lpszPath
, DWORD dwMode
,
407 DWORD dwAttributes
, BOOL bCreate
,
408 IStream
*lpTemplate
, IStream
**lppStream
)
410 DWORD dwAccess
, dwShare
, dwCreate
;
413 TRACE("(%s,%ld,0x%08lX,%d,%p,%p)\n", debugstr_w(lpszPath
), dwMode
,
414 dwAttributes
, bCreate
, lpTemplate
, lppStream
);
416 if (!lpszPath
|| !lppStream
|| lpTemplate
)
421 if (dwMode
& STGM_TRANSACTED
)
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
))
443 case STGM_SHARE_DENY_READ
:
444 dwShare
= FILE_SHARE_WRITE
;
446 case STGM_SHARE_DENY_WRITE
:
447 dwShare
= FILE_SHARE_READ
;
449 case STGM_SHARE_EXCLUSIVE
:
452 case STGM_SHARE_DENY_NONE
:
453 dwShare
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
459 switch(STGM_CREATE_MODE(dwMode
))
461 case STGM_FAILIFTHERE
:
462 dwCreate
= OPEN_EXISTING
;
465 dwCreate
= CREATE_ALWAYS
;
471 /* Open HANDLE to file */
472 hFile
= CreateFileW(lpszPath
, dwAccess
, dwShare
, NULL
, dwCreate
,
475 if(hFile
== INVALID_HANDLE_VALUE
)
477 HRESULT hRet
= (HRESULT
)GetLastError();
479 hRet
= HRESULT_FROM_WIN32(hRet
);
483 *lppStream
= IStream_Create(lpszPath
, hFile
, dwMode
);
488 return E_OUTOFMEMORY
;
493 /*************************************************************************
494 * SHCreateStreamOnFileW [SHLWAPI.@]
496 * See SHCreateStreamOnFileA.
498 HRESULT WINAPI
SHCreateStreamOnFileW(LPCWSTR lpszPath
, DWORD dwMode
,
501 TRACE("(%s,%ld,%p)\n", debugstr_w(lpszPath
), dwMode
, lppStream
);
503 if (!lpszPath
|| !lppStream
)
506 return SHCreateStreamOnFileEx(lpszPath
, dwMode
, 0, FALSE
, NULL
, lppStream
);
509 /*************************************************************************
510 * SHCreateStreamOnFileA [SHLWAPI.@]
512 * Create a stream on a file.
515 * lpszPath [I] Path of file to create stream on
516 * dwMode [I] Mode to create stream in
517 * lppStream [O] Destination for created IStream object
520 * Success: S_OK. lppStream contains the new IStream object
521 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
523 HRESULT WINAPI
SHCreateStreamOnFileA(LPCSTR lpszPath
, DWORD dwMode
,
526 WCHAR szPath
[MAX_PATH
];
528 TRACE("(%s,%ld,%p)\n", debugstr_a(lpszPath
), dwMode
, lppStream
);
532 MultiByteToWideChar(0, 0, lpszPath
, -1, szPath
, MAX_PATH
);
533 return SHCreateStreamOnFileW(szPath
, dwMode
, lppStream
);
536 /*************************************************************************
539 * Call IStream_Read() on a stream.
542 * lpStream [I] IStream object
543 * lpvDest [O] Destination for data read
544 * ulSize [I] Size of data to read
547 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
548 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
549 * number of bytes read does not match.
551 HRESULT WINAPI
SHLWAPI_184(IStream
*lpStream
, LPVOID lpvDest
, ULONG ulSize
)
556 TRACE("(%p,%p,%ld)\n", lpStream
, lpvDest
, ulSize
);
558 hRet
= IStream_Read(lpStream
, lpvDest
, ulSize
, &ulRead
);
560 if (SUCCEEDED(hRet
) && ulRead
!= ulSize
)
565 /*************************************************************************
568 * Determine if a stream has 0 length.
571 * lpStream [I] IStream object
574 * TRUE: If the stream has 0 length
577 BOOL WINAPI
SHIsEmptyStream(IStream
*lpStream
)
582 TRACE("(%p)\n", lpStream
);
584 memset(&statstg
, 0, sizeof(statstg
));
586 if(SUCCEEDED(IStream_Stat(lpStream
, &statstg
, 1)))
588 if(statstg
.cbSize
.QuadPart
)
589 bRet
= FALSE
; /* Non-Zero */
595 /* Try to read from the stream */
596 if(SUCCEEDED(SHLWAPI_184(lpStream
, &dwDummy
, sizeof(dwDummy
))))
601 IStream_Seek(lpStream
, zero
, 0, NULL
);
602 bRet
= FALSE
; /* Non-Zero */
608 /*************************************************************************
611 * Call IStream_Write() on a stream.
614 * lpStream [I] IStream object
615 * lpvSrc [I] Source for data to write
616 * ulSize [I] Size of data
619 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
620 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
621 * number of bytes written does not match.
623 HRESULT WINAPI
SHLWAPI_212(IStream
*lpStream
, LPCVOID lpvSrc
, ULONG ulSize
)
628 TRACE("(%p,%p,%ld)\n", lpStream
, lpvSrc
, ulSize
);
630 hRet
= IStream_Write(lpStream
, lpvSrc
, ulSize
, &ulWritten
);
632 if (SUCCEEDED(hRet
) && ulWritten
!= ulSize
)
638 /*************************************************************************
641 * Seek to the start of a stream.
644 * lpStream [I] IStream object
647 * Success: S_OK. The current position within the stream is updated
648 * Failure: An HRESULT error code.
650 HRESULT WINAPI
IStream_Reset(IStream
*lpStream
)
653 TRACE("(%p)\n", lpStream
);
655 return IStream_Seek(lpStream
, zero
, 0, NULL
);
658 /*************************************************************************
661 * Get the size of a stream.
664 * lpStream [I] IStream object
665 * lpulSize [O] Destination for size
668 * Success: S_OK. lpulSize contains the size of the stream.
669 * Failure: An HRESULT error code.
671 HRESULT WINAPI
IStream_Size(IStream
*lpStream
, ULARGE_INTEGER
* lpulSize
)
676 TRACE("(%p,%p)\n", lpStream
, lpulSize
);
678 memset(&statstg
, 0, sizeof(statstg
));
680 hRet
= IStream_Stat(lpStream
, &statstg
, 1);
682 if (SUCCEEDED(hRet
) && lpulSize
)
683 *lpulSize
= statstg
.cbSize
;