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 IStream IStream_iface
;
54 static inline ISHFileStream
*impl_from_IStream(IStream
*iface
)
56 return CONTAINING_RECORD(iface
, ISHFileStream
, IStream_iface
);
59 static HRESULT WINAPI
IStream_fnCommit(IStream
*,DWORD
);
62 /**************************************************************************
63 * IStream_fnQueryInterface
65 static HRESULT WINAPI
IStream_fnQueryInterface(IStream
*iface
, REFIID riid
, LPVOID
*ppvObj
)
67 ISHFileStream
*This
= impl_from_IStream(iface
);
69 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(riid
), ppvObj
);
73 if(IsEqualIID(riid
, &IID_IUnknown
) ||
74 IsEqualIID(riid
, &IID_IStream
))
77 IStream_AddRef(iface
);
83 /**************************************************************************
86 static ULONG WINAPI
IStream_fnAddRef(IStream
*iface
)
88 ISHFileStream
*This
= impl_from_IStream(iface
);
89 ULONG refCount
= InterlockedIncrement(&This
->ref
);
91 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
96 /**************************************************************************
99 static ULONG WINAPI
IStream_fnRelease(IStream
*iface
)
101 ISHFileStream
*This
= impl_from_IStream(iface
);
102 ULONG refCount
= InterlockedDecrement(&This
->ref
);
104 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
108 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
109 LocalFree(This
->lpszPath
);
110 CloseHandle(This
->hFile
);
111 HeapFree(GetProcessHeap(), 0, This
);
117 /**************************************************************************
120 static HRESULT WINAPI
IStream_fnRead(IStream
*iface
, void* pv
, ULONG cb
, ULONG
* pcbRead
)
122 ISHFileStream
*This
= impl_from_IStream(iface
);
125 TRACE("(%p,%p,0x%08x,%p)\n", This
, pv
, cb
, pcbRead
);
127 if (!ReadFile(This
->hFile
, pv
, cb
, &dwRead
, NULL
))
129 WARN("error %d reading file\n", GetLastError());
137 /**************************************************************************
140 static HRESULT WINAPI
IStream_fnWrite(IStream
*iface
, const void* pv
, ULONG cb
, ULONG
* pcbWritten
)
142 ISHFileStream
*This
= impl_from_IStream(iface
);
145 TRACE("(%p,%p,0x%08x,%p)\n", This
, pv
, cb
, pcbWritten
);
147 switch (STGM_ACCESS_MODE(This
->dwMode
))
153 return STG_E_ACCESSDENIED
;
156 if (!WriteFile(This
->hFile
, pv
, cb
, &dwWritten
, NULL
))
157 return HRESULT_FROM_WIN32(GetLastError());
160 *pcbWritten
= dwWritten
;
164 /**************************************************************************
167 static HRESULT WINAPI
IStream_fnSeek(IStream
*iface
, LARGE_INTEGER dlibMove
,
168 DWORD dwOrigin
, ULARGE_INTEGER
* pNewPos
)
170 ISHFileStream
*This
= impl_from_IStream(iface
);
173 TRACE("(%p,%d,%d,%p)\n", This
, dlibMove
.u
.LowPart
, dwOrigin
, pNewPos
);
175 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
176 dwPos
= SetFilePointer(This
->hFile
, dlibMove
.u
.LowPart
, NULL
, dwOrigin
);
177 if( dwPos
== INVALID_SET_FILE_POINTER
)
178 return HRESULT_FROM_WIN32(GetLastError());
182 pNewPos
->u
.HighPart
= 0;
183 pNewPos
->u
.LowPart
= dwPos
;
188 /**************************************************************************
191 static HRESULT WINAPI
IStream_fnSetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
193 ISHFileStream
*This
= impl_from_IStream(iface
);
195 TRACE("(%p,%d)\n", This
, libNewSize
.u
.LowPart
);
197 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
198 if( ! SetFilePointer( This
->hFile
, libNewSize
.QuadPart
, NULL
, FILE_BEGIN
) )
201 if( ! SetEndOfFile( This
->hFile
) )
207 /**************************************************************************
210 static HRESULT WINAPI
IStream_fnCopyTo(IStream
*iface
, IStream
* pstm
, ULARGE_INTEGER cb
,
211 ULARGE_INTEGER
* pcbRead
, ULARGE_INTEGER
* pcbWritten
)
213 ISHFileStream
*This
= impl_from_IStream(iface
);
218 TRACE("(%p,%p,%d,%p,%p)\n", This
, pstm
, cb
.u
.LowPart
, pcbRead
, pcbWritten
);
221 pcbRead
->QuadPart
= 0;
223 pcbWritten
->QuadPart
= 0;
228 IStream_fnCommit(iface
, 0); /* If ever buffered, this will be needed */
231 ulSize
= cb
.QuadPart
;
236 ulLeft
= ulSize
> sizeof(copyBuff
) ? sizeof(copyBuff
) : ulSize
;
239 hRet
= IStream_fnRead(iface
, copyBuff
, ulLeft
, &ulAmt
);
241 pcbRead
->QuadPart
+= ulAmt
;
242 if (FAILED(hRet
) || ulAmt
!= ulLeft
)
246 hRet
= IStream_fnWrite(pstm
, copyBuff
, ulLeft
, &ulAmt
);
248 pcbWritten
->QuadPart
+= ulAmt
;
249 if (FAILED(hRet
) || ulAmt
!= ulLeft
)
257 /**************************************************************************
260 static HRESULT WINAPI
IStream_fnCommit(IStream
*iface
, DWORD grfCommitFlags
)
262 ISHFileStream
*This
= impl_from_IStream(iface
);
264 TRACE("(%p,%d)\n", This
, grfCommitFlags
);
265 /* Currently unbuffered: This function is not needed */
269 /**************************************************************************
272 static HRESULT WINAPI
IStream_fnRevert(IStream
*iface
)
274 ISHFileStream
*This
= impl_from_IStream(iface
);
276 TRACE("(%p)\n", This
);
280 /**************************************************************************
281 * IStream_fnLockUnlockRegion
283 static HRESULT WINAPI
IStream_fnLockUnlockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
,
284 ULARGE_INTEGER cb
, DWORD dwLockType
)
286 ISHFileStream
*This
= impl_from_IStream(iface
);
287 TRACE("(%p,%d,%d,%d)\n", This
, libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
291 /*************************************************************************
294 static HRESULT WINAPI
IStream_fnStat(IStream
*iface
, STATSTG
* lpStat
,
297 ISHFileStream
*This
= impl_from_IStream(iface
);
298 BY_HANDLE_FILE_INFORMATION fi
;
301 TRACE("(%p,%p,%d)\n", This
, lpStat
, grfStatFlag
);
304 hRet
= STG_E_INVALIDPOINTER
;
307 memset(&fi
, 0, sizeof(fi
));
308 GetFileInformationByHandle(This
->hFile
, &fi
);
310 if (grfStatFlag
& STATFLAG_NONAME
)
311 lpStat
->pwcsName
= NULL
;
313 lpStat
->pwcsName
= StrDupW(This
->lpszPath
);
314 lpStat
->type
= This
->type
;
315 lpStat
->cbSize
.u
.LowPart
= fi
.nFileSizeLow
;
316 lpStat
->cbSize
.u
.HighPart
= fi
.nFileSizeHigh
;
317 lpStat
->mtime
= fi
.ftLastWriteTime
;
318 lpStat
->ctime
= fi
.ftCreationTime
;
319 lpStat
->atime
= fi
.ftLastAccessTime
;
320 lpStat
->grfMode
= This
->dwMode
;
321 lpStat
->grfLocksSupported
= 0;
322 memcpy(&lpStat
->clsid
, &IID_IStream
, sizeof(CLSID
));
323 lpStat
->grfStateBits
= This
->grfStateBits
;
324 lpStat
->reserved
= 0;
329 /*************************************************************************
332 static HRESULT WINAPI
IStream_fnClone(IStream
*iface
, IStream
** ppstm
)
334 ISHFileStream
*This
= impl_from_IStream(iface
);
336 TRACE("(%p)\n",This
);
342 static const IStreamVtbl SHLWAPI_fsVTable
=
344 IStream_fnQueryInterface
,
354 IStream_fnLockUnlockRegion
,
355 IStream_fnLockUnlockRegion
,
360 /**************************************************************************
363 * Internal helper: Create and initialise a new file stream object.
365 static IStream
*IStream_Create(LPCWSTR lpszPath
, HANDLE hFile
, DWORD dwMode
)
367 ISHFileStream
* fileStream
;
369 fileStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream
));
373 fileStream
->IStream_iface
.lpVtbl
= &SHLWAPI_fsVTable
;
375 fileStream
->hFile
= hFile
;
376 fileStream
->dwMode
= dwMode
;
377 fileStream
->lpszPath
= StrDupW(lpszPath
);
378 fileStream
->type
= 0; /* FIXME */
379 fileStream
->grfStateBits
= 0; /* FIXME */
381 TRACE ("Returning %p\n", fileStream
);
382 return &fileStream
->IStream_iface
;
385 /*************************************************************************
386 * SHCreateStreamOnFileEx [SHLWAPI.@]
388 * Create a stream on a file.
391 * lpszPath [I] Path of file to create stream on
392 * dwMode [I] Mode to create stream in
393 * dwAttributes [I] Attributes of the file
394 * bCreate [I] Whether to create the file if it doesn't exist
395 * lpTemplate [I] Reserved, must be NULL
396 * lppStream [O] Destination for created stream
399 * Success: S_OK. lppStream contains the new stream object
400 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
403 * This function is available in Unicode only.
405 HRESULT WINAPI
SHCreateStreamOnFileEx(LPCWSTR lpszPath
, DWORD dwMode
,
406 DWORD dwAttributes
, BOOL bCreate
,
407 IStream
*lpTemplate
, IStream
**lppStream
)
409 DWORD dwAccess
, dwShare
, dwCreate
;
412 TRACE("(%s,%d,0x%08X,%d,%p,%p)\n", debugstr_w(lpszPath
), dwMode
,
413 dwAttributes
, bCreate
, lpTemplate
, lppStream
);
415 if (!lpszPath
|| !lppStream
|| lpTemplate
)
421 switch (STGM_ACCESS_MODE(dwMode
))
425 dwAccess
= GENERIC_READ
|GENERIC_WRITE
;
428 dwAccess
= GENERIC_READ
;
435 switch (STGM_SHARE_MODE(dwMode
))
438 case STGM_SHARE_DENY_NONE
:
439 dwShare
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
441 case STGM_SHARE_DENY_READ
:
442 dwShare
= FILE_SHARE_WRITE
;
444 case STGM_SHARE_DENY_WRITE
:
445 dwShare
= FILE_SHARE_READ
;
447 case STGM_SHARE_EXCLUSIVE
:
454 switch(STGM_CREATE_MODE(dwMode
))
456 case STGM_FAILIFTHERE
:
457 dwCreate
= bCreate
? CREATE_NEW
: OPEN_EXISTING
;
460 dwCreate
= CREATE_ALWAYS
;
466 /* Open HANDLE to file */
467 hFile
= CreateFileW(lpszPath
, dwAccess
, dwShare
, NULL
, dwCreate
,
470 if(hFile
== INVALID_HANDLE_VALUE
)
471 return HRESULT_FROM_WIN32(GetLastError());
473 *lppStream
= IStream_Create(lpszPath
, hFile
, dwMode
);
478 return E_OUTOFMEMORY
;
483 /*************************************************************************
484 * SHCreateStreamOnFileW [SHLWAPI.@]
486 * See SHCreateStreamOnFileA.
488 HRESULT WINAPI
SHCreateStreamOnFileW(LPCWSTR lpszPath
, DWORD dwMode
,
491 TRACE("(%s,%d,%p)\n", debugstr_w(lpszPath
), dwMode
, lppStream
);
493 if (!lpszPath
|| !lppStream
)
496 if ((dwMode
& (STGM_CONVERT
|STGM_DELETEONRELEASE
|STGM_TRANSACTED
)) != 0)
499 return SHCreateStreamOnFileEx(lpszPath
, dwMode
, 0, FALSE
, NULL
, lppStream
);
502 /*************************************************************************
503 * SHCreateStreamOnFileA [SHLWAPI.@]
505 * Create a stream on a file.
508 * lpszPath [I] Path of file to create stream on
509 * dwMode [I] Mode to create stream in
510 * lppStream [O] Destination for created IStream object
513 * Success: S_OK. lppStream contains the new IStream object
514 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
516 HRESULT WINAPI
SHCreateStreamOnFileA(LPCSTR lpszPath
, DWORD dwMode
,
519 WCHAR szPath
[MAX_PATH
];
521 TRACE("(%s,%d,%p)\n", debugstr_a(lpszPath
), dwMode
, lppStream
);
524 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
526 MultiByteToWideChar(CP_ACP
, 0, lpszPath
, -1, szPath
, MAX_PATH
);
527 return SHCreateStreamOnFileW(szPath
, dwMode
, lppStream
);
530 /*************************************************************************
533 * Call IStream_Read() on a stream.
536 * lpStream [I] IStream object
537 * lpvDest [O] Destination for data read
538 * ulSize [I] Size of data to read
541 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
542 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
543 * number of bytes read does not match.
545 HRESULT WINAPI
SHIStream_Read(IStream
*lpStream
, LPVOID lpvDest
, ULONG ulSize
)
550 TRACE("(%p,%p,%d)\n", lpStream
, lpvDest
, ulSize
);
552 hRet
= IStream_Read(lpStream
, lpvDest
, ulSize
, &ulRead
);
554 if (SUCCEEDED(hRet
) && ulRead
!= ulSize
)
559 /*************************************************************************
562 * Determine if a stream has 0 length.
565 * lpStream [I] IStream object
568 * TRUE: If the stream has 0 length
571 BOOL WINAPI
SHIsEmptyStream(IStream
*lpStream
)
576 TRACE("(%p)\n", lpStream
);
578 memset(&statstg
, 0, sizeof(statstg
));
580 if(SUCCEEDED(IStream_Stat(lpStream
, &statstg
, 1)))
582 if(statstg
.cbSize
.QuadPart
)
583 bRet
= FALSE
; /* Non-Zero */
589 /* Try to read from the stream */
590 if(SUCCEEDED(SHIStream_Read(lpStream
, &dwDummy
, sizeof(dwDummy
))))
595 IStream_Seek(lpStream
, zero
, 0, NULL
);
596 bRet
= FALSE
; /* Non-Zero */
602 /*************************************************************************
605 * Call IStream_Write() on a stream.
608 * lpStream [I] IStream object
609 * lpvSrc [I] Source for data to write
610 * ulSize [I] Size of data
613 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
614 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
615 * number of bytes written does not match.
617 HRESULT WINAPI
SHIStream_Write(IStream
*lpStream
, LPCVOID lpvSrc
, ULONG ulSize
)
622 TRACE("(%p,%p,%d)\n", lpStream
, lpvSrc
, ulSize
);
624 hRet
= IStream_Write(lpStream
, lpvSrc
, ulSize
, &ulWritten
);
626 if (SUCCEEDED(hRet
) && ulWritten
!= ulSize
)
632 /*************************************************************************
635 * Seek to the start of a stream.
638 * lpStream [I] IStream object
641 * Success: S_OK. The current position within the stream is updated
642 * Failure: An HRESULT error code.
644 HRESULT WINAPI
IStream_Reset(IStream
*lpStream
)
647 TRACE("(%p)\n", lpStream
);
649 return IStream_Seek(lpStream
, zero
, 0, NULL
);
652 /*************************************************************************
655 * Get the size of a stream.
658 * lpStream [I] IStream object
659 * lpulSize [O] Destination for size
662 * Success: S_OK. lpulSize contains the size of the stream.
663 * Failure: An HRESULT error code.
665 HRESULT WINAPI
IStream_Size(IStream
*lpStream
, ULARGE_INTEGER
* lpulSize
)
670 TRACE("(%p,%p)\n", lpStream
, lpulSize
);
672 memset(&statstg
, 0, sizeof(statstg
));
674 hRet
= IStream_Stat(lpStream
, &statstg
, 1);
676 if (SUCCEEDED(hRet
) && lpulSize
)
677 *lpulSize
= statstg
.cbSize
;