2 * this class implements a pure IStream object
3 * and can be used for many purposes
5 * the main reason for implementing this was
6 * a cleaner implementation of IShellLink which
7 * needs to be able to load lnk's from a IStream
8 * interface so it was obvious to capsule the file
9 * access in a IStream to.
11 * Copyright 1999 Juergen Schmied
12 * Copyright 2003 Mike McCormack for CodeWeavers
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 #include "wine/debug.h"
41 #include "shell32_main.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
45 static const IStreamVtbl stvt
;
49 const IStreamVtbl
*lpvtst
;
54 /**************************************************************************
55 * CreateStreamOnFile()
57 * similar to CreateStreamOnHGlobal
59 HRESULT
CreateStreamOnFile (LPCWSTR pszFilename
, DWORD grfMode
, IStream
** ppstm
)
63 DWORD access
= GENERIC_READ
, creat
;
65 if( grfMode
& STGM_TRANSACTED
)
68 if( grfMode
& STGM_WRITE
)
69 access
|= GENERIC_WRITE
;
70 if( grfMode
& STGM_READWRITE
)
71 access
= GENERIC_WRITE
| GENERIC_READ
;
73 if( grfMode
& STGM_CREATE
)
74 creat
= CREATE_ALWAYS
;
76 creat
= OPEN_EXISTING
;
78 TRACE("Opening %s\n", debugstr_w(pszFilename
) );
80 handle
= CreateFileW( pszFilename
, access
, FILE_SHARE_READ
, NULL
, creat
, 0, NULL
);
81 if( handle
== INVALID_HANDLE_VALUE
)
82 return HRESULT_FROM_WIN32(GetLastError());
84 fstr
= (ISHFileStream
*)HeapAlloc(GetProcessHeap(),
85 HEAP_ZERO_MEMORY
,sizeof(ISHFileStream
));
90 fstr
->handle
= handle
;
92 (*ppstm
) = (IStream
*)fstr
;
97 /**************************************************************************
98 * IStream_fnQueryInterface
100 static HRESULT WINAPI
IStream_fnQueryInterface(IStream
*iface
, REFIID riid
, LPVOID
*ppvObj
)
102 ISHFileStream
*This
= (ISHFileStream
*)iface
;
104 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This
,debugstr_guid(riid
),ppvObj
);
108 if(IsEqualIID(riid
, &IID_IUnknown
) ||
109 IsEqualIID(riid
, &IID_IStream
))
116 IStream_AddRef((IStream
*)*ppvObj
);
117 TRACE("-- Interface: (%p)->(%p)\n",ppvObj
,*ppvObj
);
120 TRACE("-- Interface: E_NOINTERFACE\n");
121 return E_NOINTERFACE
;
124 /**************************************************************************
127 static ULONG WINAPI
IStream_fnAddRef(IStream
*iface
)
129 ISHFileStream
*This
= (ISHFileStream
*)iface
;
131 TRACE("(%p)->(count=%lu)\n",This
, This
->ref
);
133 return ++(This
->ref
);
136 /**************************************************************************
139 static ULONG WINAPI
IStream_fnRelease(IStream
*iface
)
141 ISHFileStream
*This
= (ISHFileStream
*)iface
;
143 TRACE("(%p)->()\n",This
);
147 TRACE(" destroying SHFileStream (%p)\n",This
);
148 CloseHandle(This
->handle
);
149 HeapFree(GetProcessHeap(),0,This
);
154 static HRESULT WINAPI
IStream_fnRead (IStream
* iface
, void* pv
, ULONG cb
, ULONG
* pcbRead
)
156 ISHFileStream
*This
= (ISHFileStream
*)iface
;
158 TRACE("(%p)->(%p,0x%08lx,%p)\n",This
, pv
, cb
, pcbRead
);
161 return STG_E_INVALIDPOINTER
;
163 if ( ! ReadFile( This
->handle
, pv
, cb
, pcbRead
, NULL
) )
169 static HRESULT WINAPI
IStream_fnWrite (IStream
* iface
, const void* pv
, ULONG cb
, ULONG
* pcbWritten
)
172 ISHFileStream
*This
= (ISHFileStream
*)iface
;
174 TRACE("(%p)\n",This
);
177 return STG_E_INVALIDPOINTER
;
179 /* WriteFile() doesn't allow to specify NULL as write count pointer */
181 pcbWritten
= &dummy_count
;
183 if( ! WriteFile( This
->handle
, pv
, cb
, pcbWritten
, NULL
) )
189 static HRESULT WINAPI
IStream_fnSeek (IStream
* iface
, LARGE_INTEGER dlibMove
, DWORD dwOrigin
, ULARGE_INTEGER
* plibNewPosition
)
191 DWORD pos
, newposlo
, newposhi
;
193 ISHFileStream
*This
= (ISHFileStream
*)iface
;
195 TRACE("(%p)\n",This
);
197 pos
= dlibMove
.QuadPart
; /* FIXME: truncates */
199 newposlo
= SetFilePointer( This
->handle
, pos
, &newposhi
, dwOrigin
);
200 if( newposlo
== INVALID_SET_FILE_POINTER
)
203 plibNewPosition
->QuadPart
= newposlo
| ( (LONGLONG
)newposhi
<<32);
208 static HRESULT WINAPI
IStream_fnSetSize (IStream
* iface
, ULARGE_INTEGER libNewSize
)
210 ISHFileStream
*This
= (ISHFileStream
*)iface
;
212 TRACE("(%p)\n",This
);
214 if( ! SetFilePointer( This
->handle
, libNewSize
.QuadPart
, NULL
, FILE_BEGIN
) )
217 if( ! SetEndOfFile( This
->handle
) )
222 static HRESULT WINAPI
IStream_fnCopyTo (IStream
* iface
, IStream
* pstm
, ULARGE_INTEGER cb
, ULARGE_INTEGER
* pcbRead
, ULARGE_INTEGER
* pcbWritten
)
224 ISHFileStream
*This
= (ISHFileStream
*)iface
;
226 TRACE("(%p)\n",This
);
230 static HRESULT WINAPI
IStream_fnCommit (IStream
* iface
, DWORD grfCommitFlags
)
232 ISHFileStream
*This
= (ISHFileStream
*)iface
;
234 TRACE("(%p)\n",This
);
238 static HRESULT WINAPI
IStream_fnRevert (IStream
* iface
)
240 ISHFileStream
*This
= (ISHFileStream
*)iface
;
242 TRACE("(%p)\n",This
);
246 static HRESULT WINAPI
IStream_fnLockRegion (IStream
* iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
248 ISHFileStream
*This
= (ISHFileStream
*)iface
;
250 TRACE("(%p)\n",This
);
254 static HRESULT WINAPI
IStream_fnUnlockRegion (IStream
* iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
256 ISHFileStream
*This
= (ISHFileStream
*)iface
;
258 TRACE("(%p)\n",This
);
262 static HRESULT WINAPI
IStream_fnStat (IStream
* iface
, STATSTG
* pstatstg
, DWORD grfStatFlag
)
264 ISHFileStream
*This
= (ISHFileStream
*)iface
;
266 TRACE("(%p)\n",This
);
270 static HRESULT WINAPI
IStream_fnClone (IStream
* iface
, IStream
** ppstm
)
272 ISHFileStream
*This
= (ISHFileStream
*)iface
;
274 TRACE("(%p)\n",This
);
279 static const IStreamVtbl stvt
=
281 IStream_fnQueryInterface
,
291 IStream_fnLockRegion
,
292 IStream_fnUnlockRegion
,