2 * Copyright 2002 Jon Griffiths
3 * Copyright 2016 Sebastian Lackner
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "shellscalingapi.h"
33 #include "wine/debug.h"
34 #include "wine/heap.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(shcore
);
38 static DWORD shcore_tls
;
39 static IUnknown
*process_ref
;
41 BOOL WINAPI
DllMain(HINSTANCE instance
, DWORD reason
, void *reserved
)
43 TRACE("(%p, %u, %p)\n", instance
, reason
, reserved
);
47 case DLL_PROCESS_ATTACH
:
48 DisableThreadLibraryCalls(instance
);
49 shcore_tls
= TlsAlloc();
51 case DLL_PROCESS_DETACH
:
53 if (shcore_tls
!= TLS_OUT_OF_INDEXES
)
61 HRESULT WINAPI
GetProcessDpiAwareness(HANDLE process
, PROCESS_DPI_AWARENESS
*value
)
63 if (GetProcessDpiAwarenessInternal( process
, (DPI_AWARENESS
*)value
)) return S_OK
;
64 return HRESULT_FROM_WIN32( GetLastError() );
67 HRESULT WINAPI
SetProcessDpiAwareness(PROCESS_DPI_AWARENESS value
)
69 if (SetProcessDpiAwarenessInternal( value
)) return S_OK
;
70 return HRESULT_FROM_WIN32( GetLastError() );
73 HRESULT WINAPI
GetDpiForMonitor(HMONITOR monitor
, MONITOR_DPI_TYPE type
, UINT
*x
, UINT
*y
)
75 if (GetDpiForMonitorInternal( monitor
, type
, x
, y
)) return S_OK
;
76 return HRESULT_FROM_WIN32( GetLastError() );
79 HRESULT WINAPI
GetScaleFactorForMonitor(HMONITOR monitor
, DEVICE_SCALE_FACTOR
*scale
)
81 FIXME("(%p %p): stub\n", monitor
, scale
);
83 *scale
= SCALE_100_PERCENT
;
87 DEVICE_SCALE_FACTOR WINAPI
GetScaleFactorForDevice(DISPLAY_DEVICE_TYPE device_type
)
89 FIXME("%d\n", device_type
);
91 return SCALE_100_PERCENT
;
94 HRESULT WINAPI
_IStream_Read(IStream
*stream
, void *dest
, ULONG size
)
99 TRACE("(%p, %p, %u)\n", stream
, dest
, size
);
101 hr
= IStream_Read(stream
, dest
, size
, &read
);
102 if (SUCCEEDED(hr
) && read
!= size
)
107 HRESULT WINAPI
IStream_Reset(IStream
*stream
)
109 static const LARGE_INTEGER zero
;
111 TRACE("(%p)\n", stream
);
113 return IStream_Seek(stream
, zero
, 0, NULL
);
116 HRESULT WINAPI
IStream_Size(IStream
*stream
, ULARGE_INTEGER
*size
)
121 TRACE("(%p, %p)\n", stream
, size
);
123 memset(&statstg
, 0, sizeof(statstg
));
125 hr
= IStream_Stat(stream
, &statstg
, STATFLAG_NONAME
);
127 if (SUCCEEDED(hr
) && size
)
128 *size
= statstg
.cbSize
;
132 HRESULT WINAPI
_IStream_Write(IStream
*stream
, const void *src
, ULONG size
)
137 TRACE("(%p, %p, %u)\n", stream
, src
, size
);
139 hr
= IStream_Write(stream
, src
, size
, &written
);
140 if (SUCCEEDED(hr
) && written
!= size
)
146 void WINAPI
IUnknown_AtomicRelease(IUnknown
**obj
)
148 TRACE("(%p)\n", obj
);
153 IUnknown_Release(*obj
);
157 HRESULT WINAPI
IUnknown_GetSite(IUnknown
*unk
, REFIID iid
, void **site
)
159 IObjectWithSite
*obj
= NULL
;
160 HRESULT hr
= E_INVALIDARG
;
162 TRACE("(%p, %s, %p)\n", unk
, debugstr_guid(iid
), site
);
164 if (unk
&& iid
&& site
)
166 hr
= IUnknown_QueryInterface(unk
, &IID_IObjectWithSite
, (void **)&obj
);
167 if (SUCCEEDED(hr
) && obj
)
169 hr
= IObjectWithSite_GetSite(obj
, iid
, site
);
170 IObjectWithSite_Release(obj
);
177 HRESULT WINAPI
IUnknown_QueryService(IUnknown
*obj
, REFGUID sid
, REFIID iid
, void **out
)
179 IServiceProvider
*provider
= NULL
;
190 hr
= IUnknown_QueryInterface(obj
, &IID_IServiceProvider
, (void **)&provider
);
191 if (hr
== S_OK
&& provider
)
193 TRACE("Using provider %p.\n", provider
);
195 hr
= IServiceProvider_QueryService(provider
, sid
, iid
, out
);
197 TRACE("Provider %p returned %p.\n", provider
, *out
);
199 IServiceProvider_Release(provider
);
205 void WINAPI
IUnknown_Set(IUnknown
**dest
, IUnknown
*src
)
207 TRACE("(%p, %p)\n", dest
, src
);
209 IUnknown_AtomicRelease(dest
);
213 IUnknown_AddRef(src
);
218 HRESULT WINAPI
IUnknown_SetSite(IUnknown
*obj
, IUnknown
*site
)
220 IInternetSecurityManager
*sec_manager
;
221 IObjectWithSite
*objwithsite
;
227 hr
= IUnknown_QueryInterface(obj
, &IID_IObjectWithSite
, (void **)&objwithsite
);
228 TRACE("ObjectWithSite %p, hr %#x.\n", objwithsite
, hr
);
231 hr
= IObjectWithSite_SetSite(objwithsite
, site
);
232 TRACE("SetSite() hr %#x.\n", hr
);
233 IObjectWithSite_Release(objwithsite
);
237 hr
= IUnknown_QueryInterface(obj
, &IID_IInternetSecurityManager
, (void **)&sec_manager
);
238 TRACE("InternetSecurityManager %p, hr %#x.\n", sec_manager
, hr
);
242 hr
= IInternetSecurityManager_SetSecuritySite(sec_manager
, (IInternetSecurityMgrSite
*)site
);
243 TRACE("SetSecuritySite() hr %#x.\n", hr
);
244 IInternetSecurityManager_Release(sec_manager
);
250 HRESULT WINAPI
SetCurrentProcessExplicitAppUserModelID(const WCHAR
*appid
)
252 FIXME("%s: stub\n", debugstr_w(appid
));
256 HRESULT WINAPI
GetCurrentProcessExplicitAppUserModelID(const WCHAR
**appid
)
258 FIXME("%p: stub\n", appid
);
263 /*************************************************************************
264 * CommandLineToArgvW [SHCORE.@]
266 * We must interpret the quotes in the command line to rebuild the argv
268 * - arguments are separated by spaces or tabs
269 * - quotes serve as optional argument delimiters
271 * - escaped quotes must be converted back to '"'
273 * - consecutive backslashes preceding a quote see their number halved with
274 * the remainder escaping the quote:
275 * 2n backslashes + quote -> n backslashes + quote as an argument delimiter
276 * 2n+1 backslashes + quote -> n backslashes + literal quote
277 * - backslashes that are not followed by a quote are copied literally:
280 * - in quoted strings, consecutive quotes see their number divided by three
281 * with the remainder modulo 3 deciding whether to close the string or not.
282 * Note that the opening quote must be counted in the consecutive quotes,
283 * that's the (1+) below:
284 * (1+) 3n quotes -> n quotes
285 * (1+) 3n+1 quotes -> n quotes plus closes the quoted string
286 * (1+) 3n+2 quotes -> n+1 quotes plus closes the quoted string
287 * - in unquoted strings, the first quote opens the quoted string and the
288 * remaining consecutive quotes follow the above rule.
290 WCHAR
** WINAPI
CommandLineToArgvW(const WCHAR
*cmdline
, int *numargs
)
300 SetLastError(ERROR_INVALID_PARAMETER
);
306 /* Return the path to the executable */
307 DWORD len
, deslen
= MAX_PATH
, size
;
309 size
= sizeof(WCHAR
*) * 2 + deslen
* sizeof(WCHAR
);
312 if (!(argv
= LocalAlloc(LMEM_FIXED
, size
))) return NULL
;
313 len
= GetModuleFileNameW(0, (WCHAR
*)(argv
+ 2), deslen
);
319 if (len
< deslen
) break;
321 size
= sizeof(WCHAR
*) * 2 + deslen
* sizeof(WCHAR
);
324 argv
[0] = (WCHAR
*)(argv
+ 2);
331 /* --- First count the arguments */
334 /* The first argument, the executable path, follows special rules */
337 /* The executable path ends at the next quote, no matter what */
345 /* The executable path ends at the next space, no matter what */
346 while (*s
&& *s
!= ' ' && *s
!= '\t')
349 /* skip to the first argument, if any */
350 while (*s
== ' ' || *s
== '\t')
355 /* Analyze the remaining arguments */
359 if ((*s
== ' ' || *s
== '\t') && qcount
== 0)
361 /* skip to the next argument and count it if any */
362 while (*s
== ' ' || *s
== '\t')
370 /* '\', count them */
377 if ((bcount
& 1) == 0)
378 qcount
++; /* unescaped '"' */
381 /* consecutive quotes, see comment in copying code below */
393 /* a regular character */
399 /* Allocate in a single lump, the string array, and the strings that go
400 * with it. This way the caller can make a single LocalFree() call to free
403 argv
= LocalAlloc(LMEM_FIXED
, (argc
+ 1) * sizeof(WCHAR
*) + (lstrlenW(cmdline
) + 1) * sizeof(WCHAR
));
407 /* --- Then split and copy the arguments */
408 argv
[0] = d
= lstrcpyW((WCHAR
*)(argv
+ argc
+ 1), cmdline
);
410 /* The first argument, the executable path, follows special rules */
413 /* The executable path ends at the next quote, no matter what */
427 /* The executable path ends at the next space, no matter what */
428 while (*d
&& *d
!= ' ' && *d
!= '\t')
434 /* close the executable path */
436 /* skip to the first argument and initialize it if any */
437 while (*s
== ' ' || *s
== '\t')
441 /* There are no parameters so we are all done */
447 /* Split and copy the remaining arguments */
452 if ((*s
== ' ' || *s
== '\t') && qcount
== 0)
454 /* close the argument */
458 /* skip to the next one and initialize it if any */
461 } while (*s
== ' ' || *s
== '\t');
472 if ((bcount
& 1) == 0)
474 /* Preceded by an even number of '\', this is half that
475 * number of '\', plus a quote which we erase.
482 /* Preceded by an odd number of '\', this is half that
483 * number of '\' followed by a '"'
485 d
= d
- bcount
/ 2 - 1;
490 /* Now count the number of consecutive quotes. Note that qcount
491 * already takes into account the opening quote if any, as well as
492 * the quote that lead us here.
508 /* a regular character */
522 IStream IStream_iface
;
545 static inline struct shstream
*impl_from_IStream(IStream
*iface
)
547 return CONTAINING_RECORD(iface
, struct shstream
, IStream_iface
);
550 static HRESULT WINAPI
shstream_QueryInterface(IStream
*iface
, REFIID riid
, void **out
)
552 struct shstream
*stream
= impl_from_IStream(iface
);
554 TRACE("(%p)->(%s, %p)\n", stream
, debugstr_guid(riid
), out
);
556 if (IsEqualIID(riid
, &IID_IUnknown
) ||
557 IsEqualIID(riid
, &IID_IStream
) ||
558 IsEqualIID(riid
, &IID_ISequentialStream
))
561 IStream_AddRef(iface
);
566 WARN("Unsupported interface %s.\n", debugstr_guid(riid
));
567 return E_NOINTERFACE
;
570 static ULONG WINAPI
shstream_AddRef(IStream
*iface
)
572 struct shstream
*stream
= impl_from_IStream(iface
);
573 ULONG refcount
= InterlockedIncrement(&stream
->refcount
);
575 TRACE("(%p)->(%u)\n", stream
, refcount
);
580 static ULONG WINAPI
memstream_Release(IStream
*iface
)
582 struct shstream
*stream
= impl_from_IStream(iface
);
583 ULONG refcount
= InterlockedDecrement(&stream
->refcount
);
585 TRACE("(%p)->(%u)\n", stream
, refcount
);
589 heap_free(stream
->u
.mem
.buffer
);
596 static HRESULT WINAPI
memstream_Read(IStream
*iface
, void *buff
, ULONG buff_size
, ULONG
*read_len
)
598 struct shstream
*stream
= impl_from_IStream(iface
);
601 TRACE("(%p)->(%p, %u, %p)\n", stream
, buff
, buff_size
, read_len
);
603 if (stream
->u
.mem
.position
>= stream
->u
.mem
.length
)
610 length
= stream
->u
.mem
.length
- stream
->u
.mem
.position
;
611 if (buff_size
< length
)
614 memmove(buff
, stream
->u
.mem
.buffer
+ stream
->u
.mem
.position
, length
);
615 stream
->u
.mem
.position
+= length
;
623 static HRESULT WINAPI
memstream_Write(IStream
*iface
, const void *buff
, ULONG buff_size
, ULONG
*written
)
625 struct shstream
*stream
= impl_from_IStream(iface
);
626 DWORD length
= stream
->u
.mem
.position
+ buff_size
;
628 TRACE("(%p)->(%p, %u, %p)\n", stream
, buff
, buff_size
, written
);
630 if (length
< stream
->u
.mem
.position
) /* overflow */
631 return STG_E_INSUFFICIENTMEMORY
;
633 if (length
> stream
->u
.mem
.length
)
635 BYTE
*buffer
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, stream
->u
.mem
.buffer
, length
);
637 return STG_E_INSUFFICIENTMEMORY
;
639 stream
->u
.mem
.length
= length
;
640 stream
->u
.mem
.buffer
= buffer
;
642 memmove(stream
->u
.mem
.buffer
+ stream
->u
.mem
.position
, buff
, buff_size
);
643 stream
->u
.mem
.position
+= buff_size
; /* adjust pointer */
646 *written
= buff_size
;
651 static HRESULT WINAPI
memstream_Seek(IStream
*iface
, LARGE_INTEGER move
, DWORD origin
, ULARGE_INTEGER
*new_pos
)
653 struct shstream
*stream
= impl_from_IStream(iface
);
656 TRACE("(%p)->(%s, %d, %p)\n", stream
, wine_dbgstr_longlong(move
.QuadPart
), origin
, new_pos
);
658 if (origin
== STREAM_SEEK_SET
)
660 else if (origin
== STREAM_SEEK_CUR
)
661 tmp
.QuadPart
= stream
->u
.mem
.position
+ move
.QuadPart
;
662 else if (origin
== STREAM_SEEK_END
)
663 tmp
.QuadPart
= stream
->u
.mem
.length
+ move
.QuadPart
;
665 return STG_E_INVALIDPARAMETER
;
667 if (tmp
.QuadPart
< 0)
668 return STG_E_INVALIDFUNCTION
;
670 /* we cut off the high part here */
671 stream
->u
.mem
.position
= tmp
.u
.LowPart
;
674 new_pos
->QuadPart
= stream
->u
.mem
.position
;
678 static HRESULT WINAPI
memstream_SetSize(IStream
*iface
, ULARGE_INTEGER new_size
)
680 struct shstream
*stream
= impl_from_IStream(iface
);
684 TRACE("(%p, %s)\n", stream
, wine_dbgstr_longlong(new_size
.QuadPart
));
686 /* we cut off the high part here */
687 length
= new_size
.u
.LowPart
;
688 buffer
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, stream
->u
.mem
.buffer
, length
);
690 return STG_E_INSUFFICIENTMEMORY
;
692 stream
->u
.mem
.buffer
= buffer
;
693 stream
->u
.mem
.length
= length
;
698 static HRESULT WINAPI
shstream_CopyTo(IStream
*iface
, IStream
*pstm
, ULARGE_INTEGER size
, ULARGE_INTEGER
*read_len
, ULARGE_INTEGER
*written
)
700 struct shstream
*stream
= impl_from_IStream(iface
);
702 TRACE("(%p)\n", stream
);
705 read_len
->QuadPart
= 0;
708 written
->QuadPart
= 0;
714 static HRESULT WINAPI
shstream_Commit(IStream
*iface
, DWORD flags
)
716 struct shstream
*stream
= impl_from_IStream(iface
);
718 TRACE("(%p, %#x)\n", stream
, flags
);
720 /* Commit is not supported by this stream */
724 static HRESULT WINAPI
shstream_Revert(IStream
*iface
)
726 struct shstream
*stream
= impl_from_IStream(iface
);
728 TRACE("(%p)\n", stream
);
730 /* revert not supported by this stream */
734 static HRESULT WINAPI
shstream_LockRegion(IStream
*iface
, ULARGE_INTEGER offset
, ULARGE_INTEGER size
, DWORD lock_type
)
736 struct shstream
*stream
= impl_from_IStream(iface
);
738 TRACE("(%p)\n", stream
);
740 /* lock/unlock not supported by this stream */
744 static HRESULT WINAPI
shstream_UnlockRegion(IStream
*iface
, ULARGE_INTEGER offset
, ULARGE_INTEGER size
, DWORD lock_type
)
746 struct shstream
*stream
= impl_from_IStream(iface
);
748 TRACE("(%p)\n", stream
);
750 /* lock/unlock not supported by this stream */
754 static HRESULT WINAPI
memstream_Stat(IStream
*iface
, STATSTG
*statstg
, DWORD flags
)
756 struct shstream
*stream
= impl_from_IStream(iface
);
758 TRACE("(%p, %p, %#x)\n", stream
, statstg
, flags
);
760 memset(statstg
, 0, sizeof(*statstg
));
761 statstg
->type
= STGTY_STREAM
;
762 statstg
->cbSize
.QuadPart
= stream
->u
.mem
.length
;
763 statstg
->grfMode
= STGM_READWRITE
;
768 static HRESULT WINAPI
shstream_Clone(IStream
*iface
, IStream
**dest
)
770 struct shstream
*stream
= impl_from_IStream(iface
);
772 TRACE("(%p, %p)\n", stream
, dest
);
776 /* clone not supported by this stream */
780 static const IStreamVtbl memstreamvtbl
=
782 shstream_QueryInterface
,
793 shstream_UnlockRegion
,
798 static struct shstream
*shstream_create(const IStreamVtbl
*vtbl
, const BYTE
*data
, UINT data_len
)
800 struct shstream
*stream
;
805 stream
= heap_alloc(sizeof(*stream
));
806 stream
->IStream_iface
.lpVtbl
= vtbl
;
807 stream
->refcount
= 1;
808 stream
->u
.mem
.buffer
= heap_alloc(data_len
);
809 if (!stream
->u
.mem
.buffer
)
814 memcpy(stream
->u
.mem
.buffer
, data
, data_len
);
815 stream
->u
.mem
.length
= data_len
;
816 stream
->u
.mem
.position
= 0;
821 /*************************************************************************
822 * SHCreateMemStream [SHCORE.@]
824 * Create an IStream object on a block of memory.
827 * data [I] Memory block to create the IStream object on
828 * data_len [I] Length of data block
831 * Success: A pointer to the IStream object.
832 * Failure: NULL, if any parameters are invalid or an error occurs.
835 * A copy of the memory block is made, it's freed when the stream is released.
837 IStream
* WINAPI
SHCreateMemStream(const BYTE
*data
, UINT data_len
)
839 struct shstream
*stream
;
841 TRACE("(%p, %u)\n", data
, data_len
);
843 stream
= shstream_create(&memstreamvtbl
, data
, data_len
);
844 return stream
? &stream
->IStream_iface
: NULL
;
847 static ULONG WINAPI
filestream_Release(IStream
*iface
)
849 struct shstream
*stream
= impl_from_IStream(iface
);
850 ULONG refcount
= InterlockedDecrement(&stream
->refcount
);
852 TRACE("(%p)->(%u)\n", stream
, refcount
);
856 CloseHandle(stream
->u
.file
.handle
);
857 heap_free(stream
->u
.file
.path
);
864 static HRESULT WINAPI
filestream_Read(IStream
*iface
, void *buff
, ULONG size
, ULONG
*read_len
)
866 struct shstream
*stream
= impl_from_IStream(iface
);
869 TRACE("(%p, %p, %u, %p)\n", stream
, buff
, size
, read_len
);
871 if (!ReadFile(stream
->u
.file
.handle
, buff
, size
, &read
, NULL
))
873 WARN("error %d reading file\n", GetLastError());
880 return read
== size
? S_OK
: S_FALSE
;
883 static HRESULT WINAPI
filestream_Write(IStream
*iface
, const void *buff
, ULONG size
, ULONG
*written
)
885 struct shstream
*stream
= impl_from_IStream(iface
);
886 DWORD written_len
= 0;
888 TRACE("(%p, %p, %u, %p)\n", stream
, buff
, size
, written
);
890 switch (stream
->u
.file
.mode
& 0xf)
896 return STG_E_ACCESSDENIED
;
899 if (!WriteFile(stream
->u
.file
.handle
, buff
, size
, &written_len
, NULL
))
900 return HRESULT_FROM_WIN32(GetLastError());
903 *written
= written_len
;
908 static HRESULT WINAPI
filestream_Seek(IStream
*iface
, LARGE_INTEGER move
, DWORD origin
, ULARGE_INTEGER
*new_pos
)
910 struct shstream
*stream
= impl_from_IStream(iface
);
913 TRACE("(%p, %s, %d, %p)\n", stream
, wine_dbgstr_longlong(move
.QuadPart
), origin
, new_pos
);
915 position
= SetFilePointer(stream
->u
.file
.handle
, move
.u
.LowPart
, NULL
, origin
);
916 if (position
== INVALID_SET_FILE_POINTER
)
917 return HRESULT_FROM_WIN32(GetLastError());
921 new_pos
->u
.HighPart
= 0;
922 new_pos
->u
.LowPart
= position
;
928 static HRESULT WINAPI
filestream_SetSize(IStream
*iface
, ULARGE_INTEGER size
)
930 struct shstream
*stream
= impl_from_IStream(iface
);
931 LARGE_INTEGER origin
, move
;
933 TRACE("(%p, %s)\n", stream
, wine_dbgstr_longlong(size
.QuadPart
));
936 if (!SetFilePointerEx(stream
->u
.file
.handle
, move
, &origin
, FILE_CURRENT
))
939 move
.QuadPart
= size
.QuadPart
;
940 if (!SetFilePointerEx(stream
->u
.file
.handle
, move
, NULL
, FILE_BEGIN
))
943 if (stream
->u
.file
.mode
!= STGM_READ
)
945 if (!SetEndOfFile(stream
->u
.file
.handle
))
947 if (!SetFilePointerEx(stream
->u
.file
.handle
, origin
, NULL
, FILE_BEGIN
))
954 static HRESULT WINAPI
filestream_CopyTo(IStream
*iface
, IStream
*dest
, ULARGE_INTEGER size
,
955 ULARGE_INTEGER
*read_len
, ULARGE_INTEGER
*written
)
957 struct shstream
*stream
= impl_from_IStream(iface
);
961 TRACE("(%p, %p, %s, %p, %p)\n", stream
, dest
, wine_dbgstr_longlong(size
.QuadPart
), read_len
, written
);
964 read_len
->QuadPart
= 0;
966 written
->QuadPart
= 0;
971 while (size
.QuadPart
)
973 ULONG left
, read_chunk
, written_chunk
;
975 left
= size
.QuadPart
> sizeof(buff
) ? sizeof(buff
) : size
.QuadPart
;
978 hr
= IStream_Read(iface
, buff
, left
, &read_chunk
);
979 if (FAILED(hr
) || read_chunk
== 0)
982 read_len
->QuadPart
+= read_chunk
;
985 hr
= IStream_Write(dest
, buff
, read_chunk
, &written_chunk
);
987 written
->QuadPart
+= written_chunk
;
988 if (FAILED(hr
) || written_chunk
!= left
)
991 size
.QuadPart
-= left
;
997 static HRESULT WINAPI
filestream_Commit(IStream
*iface
, DWORD flags
)
999 struct shstream
*stream
= impl_from_IStream(iface
);
1001 TRACE("(%p, %#x)\n", stream
, flags
);
1006 static HRESULT WINAPI
filestream_Stat(IStream
*iface
, STATSTG
*statstg
, DWORD flags
)
1008 struct shstream
*stream
= impl_from_IStream(iface
);
1009 BY_HANDLE_FILE_INFORMATION fi
;
1011 TRACE("(%p, %p, %#x)\n", stream
, statstg
, flags
);
1014 return STG_E_INVALIDPOINTER
;
1016 memset(&fi
, 0, sizeof(fi
));
1017 GetFileInformationByHandle(stream
->u
.file
.handle
, &fi
);
1019 if (flags
& STATFLAG_NONAME
)
1020 statstg
->pwcsName
= NULL
;
1023 int len
= lstrlenW(stream
->u
.file
.path
);
1024 if ((statstg
->pwcsName
= CoTaskMemAlloc((len
+ 1) * sizeof(WCHAR
))))
1025 memcpy(statstg
->pwcsName
, stream
->u
.file
.path
, (len
+ 1) * sizeof(WCHAR
));
1028 statstg
->cbSize
.u
.LowPart
= fi
.nFileSizeLow
;
1029 statstg
->cbSize
.u
.HighPart
= fi
.nFileSizeHigh
;
1030 statstg
->mtime
= fi
.ftLastWriteTime
;
1031 statstg
->ctime
= fi
.ftCreationTime
;
1032 statstg
->atime
= fi
.ftLastAccessTime
;
1033 statstg
->grfMode
= stream
->u
.file
.mode
;
1034 statstg
->grfLocksSupported
= 0;
1035 memcpy(&statstg
->clsid
, &IID_IStream
, sizeof(CLSID
));
1036 statstg
->grfStateBits
= 0;
1037 statstg
->reserved
= 0;
1042 static const IStreamVtbl filestreamvtbl
=
1044 shstream_QueryInterface
,
1054 shstream_LockRegion
,
1055 shstream_UnlockRegion
,
1060 /*************************************************************************
1061 * SHCreateStreamOnFileEx [SHCORE.@]
1063 HRESULT WINAPI
SHCreateStreamOnFileEx(const WCHAR
*path
, DWORD mode
, DWORD attributes
,
1064 BOOL create
, IStream
*template, IStream
**ret
)
1066 DWORD access
, share
, creation_disposition
, len
;
1067 struct shstream
*stream
;
1070 TRACE("(%s, %d, 0x%08X, %d, %p, %p)\n", debugstr_w(path
), mode
, attributes
,
1071 create
, template, ret
);
1073 if (!path
|| !ret
|| template)
1074 return E_INVALIDARG
;
1082 case STGM_READWRITE
:
1083 access
= GENERIC_READ
| GENERIC_WRITE
;
1086 access
= GENERIC_READ
;
1089 return E_INVALIDARG
;
1093 switch (mode
& 0xf0)
1096 case STGM_SHARE_DENY_NONE
:
1097 share
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1099 case STGM_SHARE_DENY_READ
:
1100 share
= FILE_SHARE_WRITE
;
1102 case STGM_SHARE_DENY_WRITE
:
1103 share
= FILE_SHARE_READ
;
1105 case STGM_SHARE_EXCLUSIVE
:
1109 return E_INVALIDARG
;
1112 switch (mode
& 0xf000)
1114 case STGM_FAILIFTHERE
:
1115 creation_disposition
= create
? CREATE_NEW
: OPEN_EXISTING
;
1118 creation_disposition
= CREATE_ALWAYS
;
1121 return E_INVALIDARG
;
1124 hFile
= CreateFileW(path
, access
, share
, NULL
, creation_disposition
, attributes
, 0);
1125 if (hFile
== INVALID_HANDLE_VALUE
)
1126 return HRESULT_FROM_WIN32(GetLastError());
1128 stream
= heap_alloc(sizeof(*stream
));
1129 stream
->IStream_iface
.lpVtbl
= &filestreamvtbl
;
1130 stream
->refcount
= 1;
1131 stream
->u
.file
.handle
= hFile
;
1132 stream
->u
.file
.mode
= mode
;
1134 len
= lstrlenW(path
);
1135 stream
->u
.file
.path
= heap_alloc((len
+ 1) * sizeof(WCHAR
));
1136 memcpy(stream
->u
.file
.path
, path
, (len
+ 1) * sizeof(WCHAR
));
1138 *ret
= &stream
->IStream_iface
;
1143 /*************************************************************************
1144 * SHCreateStreamOnFileW [SHCORE.@]
1146 HRESULT WINAPI
SHCreateStreamOnFileW(const WCHAR
*path
, DWORD mode
, IStream
**stream
)
1148 TRACE("(%s, %#x, %p)\n", debugstr_w(path
), mode
, stream
);
1150 if (!path
|| !stream
)
1151 return E_INVALIDARG
;
1153 if ((mode
& (STGM_CONVERT
| STGM_DELETEONRELEASE
| STGM_TRANSACTED
)) != 0)
1154 return E_INVALIDARG
;
1156 return SHCreateStreamOnFileEx(path
, mode
, 0, FALSE
, NULL
, stream
);
1159 /*************************************************************************
1160 * SHCreateStreamOnFileA [SHCORE.@]
1162 HRESULT WINAPI
SHCreateStreamOnFileA(const char *path
, DWORD mode
, IStream
**stream
)
1168 TRACE("(%s, %#x, %p)\n", debugstr_a(path
), mode
, stream
);
1171 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
1173 len
= MultiByteToWideChar(CP_ACP
, 0, path
, -1, NULL
, 0);
1174 pathW
= heap_alloc(len
* sizeof(WCHAR
));
1176 return E_OUTOFMEMORY
;
1178 MultiByteToWideChar(CP_ACP
, 0, path
, -1, pathW
, len
);
1179 hr
= SHCreateStreamOnFileW(pathW
, mode
, stream
);
1185 static ULONG WINAPI
regstream_Release(IStream
*iface
)
1187 struct shstream
*stream
= impl_from_IStream(iface
);
1188 ULONG refcount
= InterlockedDecrement(&stream
->refcount
);
1190 TRACE("(%p)->(%u)\n", stream
, refcount
);
1194 if (stream
->u
.mem
.hkey
)
1196 if (stream
->u
.mem
.length
)
1197 RegSetValueExW(stream
->u
.mem
.hkey
, stream
->u
.mem
.valuename
, 0, REG_BINARY
,
1198 (const BYTE
*)stream
->u
.mem
.buffer
, stream
->u
.mem
.length
);
1200 RegDeleteValueW(stream
->u
.mem
.hkey
, stream
->u
.mem
.valuename
);
1201 RegCloseKey(stream
->u
.mem
.hkey
);
1203 CoTaskMemFree(stream
->u
.mem
.valuename
);
1204 heap_free(stream
->u
.mem
.buffer
);
1211 static const IStreamVtbl regstreamvtbl
=
1213 shstream_QueryInterface
,
1223 shstream_LockRegion
,
1224 shstream_UnlockRegion
,
1229 /*************************************************************************
1230 * SHOpenRegStream2W [SHCORE.@]
1232 IStream
* WINAPI
SHOpenRegStream2W(HKEY hKey
, const WCHAR
*subkey
, const WCHAR
*value
, DWORD mode
)
1234 struct shstream
*stream
;
1235 HKEY hStrKey
= NULL
;
1240 TRACE("(%p, %s, %s, %#x)\n", hKey
, debugstr_w(subkey
), debugstr_w(value
), mode
);
1242 if (mode
== STGM_READ
)
1243 ret
= RegOpenKeyExW(hKey
, subkey
, 0, KEY_READ
, &hStrKey
);
1244 else /* in write mode we make sure the subkey exits */
1245 ret
= RegCreateKeyExW(hKey
, subkey
, 0, NULL
, 0, KEY_READ
| KEY_WRITE
, NULL
, &hStrKey
, NULL
);
1247 if (ret
== ERROR_SUCCESS
)
1249 if (mode
== STGM_READ
|| mode
== STGM_READWRITE
)
1251 /* read initial data */
1252 ret
= RegQueryValueExW(hStrKey
, value
, 0, 0, 0, &length
);
1253 if (ret
== ERROR_SUCCESS
&& length
)
1255 buff
= heap_alloc(length
);
1256 RegQueryValueExW(hStrKey
, value
, 0, 0, buff
, &length
);
1261 buff
= heap_alloc(length
);
1263 stream
= shstream_create(®streamvtbl
, buff
, length
);
1267 stream
->u
.mem
.hkey
= hStrKey
;
1268 SHStrDupW(value
, &stream
->u
.mem
.valuename
);
1269 return &stream
->IStream_iface
;
1274 RegCloseKey(hStrKey
);
1279 /*************************************************************************
1280 * SHOpenRegStream2A [SHCORE.@]
1282 IStream
* WINAPI
SHOpenRegStream2A(HKEY hKey
, const char *subkey
, const char *value
, DWORD mode
)
1284 WCHAR
*subkeyW
= NULL
, *valueW
= NULL
;
1287 TRACE("(%p, %s, %s, %#x)\n", hKey
, debugstr_a(subkey
), debugstr_a(value
), mode
);
1289 if (subkey
&& FAILED(SHStrDupA(subkey
, &subkeyW
)))
1291 if (value
&& FAILED(SHStrDupA(value
, &valueW
)))
1293 CoTaskMemFree(subkeyW
);
1297 stream
= SHOpenRegStream2W(hKey
, subkeyW
, valueW
, mode
);
1298 CoTaskMemFree(subkeyW
);
1299 CoTaskMemFree(valueW
);
1303 /*************************************************************************
1304 * SHOpenRegStreamA [SHCORE.@]
1306 IStream
* WINAPI
SHOpenRegStreamA(HKEY hkey
, const char *subkey
, const char *value
, DWORD mode
)
1308 WCHAR
*subkeyW
= NULL
, *valueW
= NULL
;
1311 TRACE("(%p, %s, %s, %#x)\n", hkey
, debugstr_a(subkey
), debugstr_a(value
), mode
);
1313 if (subkey
&& FAILED(SHStrDupA(subkey
, &subkeyW
)))
1315 if (value
&& FAILED(SHStrDupA(value
, &valueW
)))
1317 CoTaskMemFree(subkeyW
);
1321 stream
= SHOpenRegStreamW(hkey
, subkeyW
, valueW
, mode
);
1322 CoTaskMemFree(subkeyW
);
1323 CoTaskMemFree(valueW
);
1327 static ULONG WINAPI
dummystream_AddRef(IStream
*iface
)
1333 static ULONG WINAPI
dummystream_Release(IStream
*iface
)
1339 static HRESULT WINAPI
dummystream_Read(IStream
*iface
, void *buff
, ULONG buff_size
, ULONG
*read_len
)
1347 static const IStreamVtbl dummystreamvtbl
=
1349 shstream_QueryInterface
,
1351 dummystream_Release
,
1359 shstream_LockRegion
,
1360 shstream_UnlockRegion
,
1365 static struct shstream dummyregstream
= { { &dummystreamvtbl
} };
1367 /*************************************************************************
1368 * SHOpenRegStreamW [SHCORE.@]
1370 IStream
* WINAPI
SHOpenRegStreamW(HKEY hkey
, const WCHAR
*subkey
, const WCHAR
*value
, DWORD mode
)
1374 TRACE("(%p, %s, %s, %#x)\n", hkey
, debugstr_w(subkey
), debugstr_w(value
), mode
);
1375 stream
= SHOpenRegStream2W(hkey
, subkey
, value
, mode
);
1376 return stream
? stream
: &dummyregstream
.IStream_iface
;
1381 IUnknown IUnknown_iface
;
1385 static inline struct threadref
*threadref_impl_from_IUnknown(IUnknown
*iface
)
1387 return CONTAINING_RECORD(iface
, struct threadref
, IUnknown_iface
);
1390 static HRESULT WINAPI
threadref_QueryInterface(IUnknown
*iface
, REFIID riid
, void **out
)
1392 struct threadref
*threadref
= threadref_impl_from_IUnknown(iface
);
1394 TRACE("(%p, %s, %p)\n", threadref
, debugstr_guid(riid
), out
);
1399 if (IsEqualGUID(&IID_IUnknown
, riid
))
1402 IUnknown_AddRef(iface
);
1407 WARN("Interface %s not supported.\n", debugstr_guid(riid
));
1408 return E_NOINTERFACE
;
1411 static ULONG WINAPI
threadref_AddRef(IUnknown
*iface
)
1413 struct threadref
*threadref
= threadref_impl_from_IUnknown(iface
);
1414 LONG refcount
= InterlockedIncrement(threadref
->refcount
);
1416 TRACE("(%p, %d)\n", threadref
, refcount
);
1421 static ULONG WINAPI
threadref_Release(IUnknown
*iface
)
1423 struct threadref
*threadref
= threadref_impl_from_IUnknown(iface
);
1424 LONG refcount
= InterlockedDecrement(threadref
->refcount
);
1426 TRACE("(%p, %d)\n", threadref
, refcount
);
1429 heap_free(threadref
);
1434 static const IUnknownVtbl threadrefvtbl
=
1436 threadref_QueryInterface
,
1441 /*************************************************************************
1442 * SHCreateThreadRef [SHCORE.@]
1444 HRESULT WINAPI
SHCreateThreadRef(LONG
*refcount
, IUnknown
**out
)
1446 struct threadref
*threadref
;
1448 TRACE("(%p, %p)\n", refcount
, out
);
1450 if (!refcount
|| !out
)
1451 return E_INVALIDARG
;
1455 threadref
= heap_alloc(sizeof(*threadref
));
1457 return E_OUTOFMEMORY
;
1458 threadref
->IUnknown_iface
.lpVtbl
= &threadrefvtbl
;
1459 threadref
->refcount
= refcount
;
1462 *out
= &threadref
->IUnknown_iface
;
1464 TRACE("Created %p.\n", threadref
);
1468 /*************************************************************************
1469 * SHGetThreadRef [SHCORE.@]
1471 HRESULT WINAPI
SHGetThreadRef(IUnknown
**out
)
1473 TRACE("(%p)\n", out
);
1475 if (shcore_tls
== TLS_OUT_OF_INDEXES
)
1476 return E_NOINTERFACE
;
1478 *out
= TlsGetValue(shcore_tls
);
1480 return E_NOINTERFACE
;
1482 IUnknown_AddRef(*out
);
1486 /*************************************************************************
1487 * SHSetThreadRef [SHCORE.@]
1489 HRESULT WINAPI
SHSetThreadRef(IUnknown
*obj
)
1491 TRACE("(%p)\n", obj
);
1493 if (shcore_tls
== TLS_OUT_OF_INDEXES
)
1494 return E_NOINTERFACE
;
1496 TlsSetValue(shcore_tls
, obj
);
1500 /*************************************************************************
1501 * SHReleaseThreadRef [SHCORE.@]
1503 HRESULT WINAPI
SHReleaseThreadRef(void)
1505 FIXME("() - stub!\n");
1509 /*************************************************************************
1510 * GetProcessReference [SHCORE.@]
1512 HRESULT WINAPI
GetProcessReference(IUnknown
**obj
)
1514 TRACE("(%p)\n", obj
);
1522 IUnknown_AddRef(*obj
);
1527 /*************************************************************************
1528 * SetProcessReference [SHCORE.@]
1530 void WINAPI
SetProcessReference(IUnknown
*obj
)
1532 TRACE("(%p)\n", obj
);
1539 LPTHREAD_START_ROUTINE thread_proc
;
1540 LPTHREAD_START_ROUTINE callback
;
1544 IUnknown
*thread_ref
;
1545 IUnknown
*process_ref
;
1548 static DWORD WINAPI
shcore_thread_wrapper(void *data
)
1550 struct thread_data thread_data
;
1551 HRESULT hr
= E_FAIL
;
1554 TRACE("(%p)\n", data
);
1556 /* We are now executing in the context of the newly created thread.
1557 * So we copy the data passed to us (it is on the stack of the function
1558 * that called us, which is waiting for us to signal an event before
1560 thread_data
= *(struct thread_data
*)data
;
1562 if (thread_data
.flags
& CTF_COINIT
)
1564 hr
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
| COINIT_DISABLE_OLE1DDE
);
1566 hr
= CoInitializeEx(NULL
, COINIT_DISABLE_OLE1DDE
);
1569 if (thread_data
.callback
)
1570 thread_data
.callback(thread_data
.data
);
1572 /* Signal the thread that created us; it can return now. */
1573 SetEvent(thread_data
.hEvent
);
1575 /* Execute the callers start code. */
1576 retval
= thread_data
.thread_proc(thread_data
.data
);
1578 /* Release thread and process references. */
1579 if (thread_data
.thread_ref
)
1580 IUnknown_Release(thread_data
.thread_ref
);
1582 if (thread_data
.process_ref
)
1583 IUnknown_Release(thread_data
.process_ref
);
1591 /*************************************************************************
1592 * SHCreateThread [SHCORE.@]
1594 BOOL WINAPI
SHCreateThread(LPTHREAD_START_ROUTINE thread_proc
, void *data
, DWORD flags
, LPTHREAD_START_ROUTINE callback
)
1596 struct thread_data thread_data
;
1597 BOOL called
= FALSE
;
1599 TRACE("(%p, %p, %#x, %p)\n", thread_proc
, data
, flags
, callback
);
1601 thread_data
.thread_proc
= thread_proc
;
1602 thread_data
.callback
= callback
;
1603 thread_data
.data
= data
;
1604 thread_data
.flags
= flags
;
1605 thread_data
.hEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1607 if (flags
& CTF_THREAD_REF
)
1608 SHGetThreadRef(&thread_data
.thread_ref
);
1610 thread_data
.thread_ref
= NULL
;
1612 if (flags
& CTF_PROCESS_REF
)
1613 GetProcessReference(&thread_data
.process_ref
);
1615 thread_data
.process_ref
= NULL
;
1617 /* Create the thread */
1618 if (thread_data
.hEvent
)
1623 hThread
= CreateThread(NULL
, 0, shcore_thread_wrapper
, &thread_data
, 0, &retval
);
1626 /* Wait for the thread to signal us to continue */
1627 WaitForSingleObject(thread_data
.hEvent
, INFINITE
);
1628 CloseHandle(hThread
);
1631 CloseHandle(thread_data
.hEvent
);
1636 if (!thread_data
.callback
&& flags
& CTF_INSIST
)
1638 /* Couldn't call, call synchronously */
1639 thread_data
.thread_proc(data
);
1644 if (thread_data
.thread_ref
)
1645 IUnknown_Release(thread_data
.thread_ref
);
1647 if (thread_data
.process_ref
)
1648 IUnknown_Release(thread_data
.process_ref
);
1655 /*************************************************************************
1656 * SHStrDupW [SHCORE.@]
1658 HRESULT WINAPI
SHStrDupW(const WCHAR
*src
, WCHAR
**dest
)
1662 TRACE("(%s, %p)\n", debugstr_w(src
), dest
);
1667 return E_INVALIDARG
;
1669 len
= (lstrlenW(src
) + 1) * sizeof(WCHAR
);
1670 *dest
= CoTaskMemAlloc(len
);
1672 return E_OUTOFMEMORY
;
1674 memcpy(*dest
, src
, len
);
1679 /*************************************************************************
1680 * SHStrDupA [SHCORE.@]
1682 HRESULT WINAPI
SHStrDupA(const char *src
, WCHAR
**dest
)
1689 return E_INVALIDARG
;
1691 len
= MultiByteToWideChar(CP_ACP
, 0, src
, -1, NULL
, 0);
1692 *dest
= CoTaskMemAlloc(len
* sizeof(WCHAR
));
1694 return E_OUTOFMEMORY
;
1696 MultiByteToWideChar(CP_ACP
, 0, src
, -1, *dest
, len
);
1701 /*************************************************************************
1702 * SHAnsiToAnsi [SHCORE.@]
1704 DWORD WINAPI
SHAnsiToAnsi(const char *src
, char *dest
, int dest_len
)
1708 TRACE("(%s, %p, %d)\n", debugstr_a(src
), dest
, dest_len
);
1710 if (!src
|| !dest
|| dest_len
<= 0)
1713 lstrcpynA(dest
, src
, dest_len
);
1716 return src
[ret
] ? 0 : ret
+ 1;
1719 /*************************************************************************
1720 * SHUnicodeToAnsi [SHCORE.@]
1722 DWORD WINAPI
SHUnicodeToAnsi(const WCHAR
*src
, char *dest
, int dest_len
)
1726 TRACE("(%s, %p, %d)\n", debugstr_w(src
), dest
, dest_len
);
1728 if (!dest
|| !dest_len
)
1733 ret
= WideCharToMultiByte(CP_ACP
, 0, src
, -1, dest
, dest_len
, NULL
, NULL
);
1736 dest
[dest_len
- 1] = 0;
1746 /*************************************************************************
1747 * SHUnicodeToUnicode [SHCORE.@]
1749 DWORD WINAPI
SHUnicodeToUnicode(const WCHAR
*src
, WCHAR
*dest
, int dest_len
)
1753 TRACE("(%s, %p, %d)\n", debugstr_w(src
), dest
, dest_len
);
1755 if (!src
|| !dest
|| dest_len
<= 0)
1758 lstrcpynW(dest
, src
, dest_len
);
1759 ret
= lstrlenW(dest
);
1761 return src
[ret
] ? 0 : ret
+ 1;
1764 /*************************************************************************
1765 * SHAnsiToUnicode [SHCORE.@]
1767 DWORD WINAPI
SHAnsiToUnicode(const char *src
, WCHAR
*dest
, int dest_len
)
1771 TRACE("(%s, %p, %d)\n", debugstr_a(src
), dest
, dest_len
);
1773 if (!dest
|| !dest_len
)
1778 ret
= MultiByteToWideChar(CP_ACP
, 0, src
, -1, dest
, dest_len
);
1781 dest
[dest_len
- 1] = 0;
1791 /*************************************************************************
1792 * SHRegDuplicateHKey [SHCORE.@]
1794 HKEY WINAPI
SHRegDuplicateHKey(HKEY hKey
)
1798 RegOpenKeyExW(hKey
, 0, 0, MAXIMUM_ALLOWED
, &newKey
);
1799 TRACE("new key is %p\n", newKey
);
1803 /*************************************************************************
1804 * SHDeleteEmptyKeyW [SHCORE.@]
1806 DWORD WINAPI
SHDeleteEmptyKeyW(HKEY hkey
, const WCHAR
*subkey
)
1808 DWORD ret
, count
= 0;
1811 TRACE("(%p, %s)\n", hkey
, debugstr_w(subkey
));
1813 ret
= RegOpenKeyExW(hkey
, subkey
, 0, KEY_READ
, &hsubkey
);
1816 ret
= RegQueryInfoKeyW(hsubkey
, NULL
, NULL
, NULL
, &count
,
1817 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1818 RegCloseKey(hsubkey
);
1822 ret
= ERROR_KEY_HAS_CHILDREN
;
1824 ret
= RegDeleteKeyW(hkey
, subkey
);
1831 /*************************************************************************
1832 * SHDeleteEmptyKeyA [SHCORE.@]
1834 DWORD WINAPI
SHDeleteEmptyKeyA(HKEY hkey
, const char *subkey
)
1836 WCHAR
*subkeyW
= NULL
;
1839 TRACE("(%p, %s)\n", hkey
, debugstr_a(subkey
));
1841 if (subkey
&& FAILED(SHStrDupA(subkey
, &subkeyW
)))
1842 return ERROR_OUTOFMEMORY
;
1844 ret
= SHDeleteEmptyKeyW(hkey
, subkeyW
);
1845 CoTaskMemFree(subkeyW
);
1849 /*************************************************************************
1850 * SHDeleteKeyW [SHCORE.@]
1852 DWORD WINAPI
SHDeleteKeyW(HKEY hkey
, const WCHAR
*subkey
)
1854 TRACE("(%p, %s)\n", hkey
, debugstr_w(subkey
));
1856 return RegDeleteTreeW(hkey
, subkey
);
1859 /*************************************************************************
1860 * SHDeleteKeyA [SHCORE.@]
1862 DWORD WINAPI
SHDeleteKeyA(HKEY hkey
, const char *subkey
)
1864 TRACE("(%p, %s)\n", hkey
, debugstr_a(subkey
));
1866 return RegDeleteTreeA(hkey
, subkey
);
1869 /*************************************************************************
1870 * SHDeleteValueW [SHCORE.@]
1872 DWORD WINAPI
SHDeleteValueW(HKEY hkey
, const WCHAR
*subkey
, const WCHAR
*value
)
1877 TRACE("(%p, %s, %s)\n", hkey
, debugstr_w(subkey
), debugstr_w(value
));
1879 ret
= RegOpenKeyExW(hkey
, subkey
, 0, KEY_SET_VALUE
, &hsubkey
);
1882 ret
= RegDeleteValueW(hsubkey
, value
);
1883 RegCloseKey(hsubkey
);
1889 /*************************************************************************
1890 * SHDeleteValueA [SHCORE.@]
1892 DWORD WINAPI
SHDeleteValueA(HKEY hkey
, const char *subkey
, const char *value
)
1894 WCHAR
*subkeyW
= NULL
, *valueW
= NULL
;
1897 TRACE("(%p, %s, %s)\n", hkey
, debugstr_a(subkey
), debugstr_a(value
));
1899 if (subkey
&& FAILED(SHStrDupA(subkey
, &subkeyW
)))
1900 return ERROR_OUTOFMEMORY
;
1901 if (value
&& FAILED(SHStrDupA(value
, &valueW
)))
1903 CoTaskMemFree(subkeyW
);
1904 return ERROR_OUTOFMEMORY
;
1907 ret
= SHDeleteValueW(hkey
, subkeyW
, valueW
);
1908 CoTaskMemFree(subkeyW
);
1909 CoTaskMemFree(valueW
);
1913 /*************************************************************************
1914 * SHCopyKeyA [SHCORE.@]
1916 DWORD WINAPI
SHCopyKeyA(HKEY hkey_src
, const char *subkey
, HKEY hkey_dst
, DWORD reserved
)
1918 WCHAR
*subkeyW
= NULL
;
1921 TRACE("(%p, %s, %p, %d)\n", hkey_src
, debugstr_a(subkey
), hkey_dst
, reserved
);
1923 if (subkey
&& FAILED(SHStrDupA(subkey
, &subkeyW
)))
1926 ret
= SHCopyKeyW(hkey_src
, subkeyW
, hkey_dst
, reserved
);
1927 CoTaskMemFree(subkeyW
);
1931 /*************************************************************************
1932 * SHCopyKeyW [SHCORE.@]
1934 DWORD WINAPI
SHCopyKeyW(HKEY hkey_src
, const WCHAR
*subkey
, HKEY hkey_dst
, DWORD reserved
)
1936 DWORD key_count
= 0, value_count
= 0, max_key_len
= 0;
1937 WCHAR name
[MAX_PATH
], *ptr_name
= name
;
1938 BYTE buff
[1024], *ptr
= buff
;
1939 DWORD max_data_len
= 0, i
;
1942 TRACE("(%p, %s, %p, %d)\n", hkey_src
, debugstr_w(subkey
), hkey_dst
, reserved
);
1944 if (!hkey_dst
|| !hkey_src
)
1945 return ERROR_INVALID_PARAMETER
;
1948 ret
= RegOpenKeyExW(hkey_src
, subkey
, 0, KEY_ALL_ACCESS
, &hkey_src
);
1951 hkey_src
= NULL
; /* Don't close this key since we didn't open it */
1954 DWORD max_value_len
;
1956 ret
= RegQueryInfoKeyW(hkey_src
, NULL
, NULL
, NULL
, &key_count
, &max_key_len
,
1957 NULL
, &value_count
, &max_value_len
, &max_data_len
, NULL
, NULL
);
1960 /* Get max size for key/value names */
1961 max_key_len
= max(max_key_len
, max_value_len
);
1963 if (max_key_len
++ > MAX_PATH
- 1)
1964 ptr_name
= heap_alloc(max_key_len
* sizeof(WCHAR
));
1966 if (max_data_len
> sizeof(buff
))
1967 ptr
= heap_alloc(max_data_len
);
1969 if (!ptr_name
|| !ptr
)
1970 ret
= ERROR_NOT_ENOUGH_MEMORY
;
1974 for (i
= 0; i
< key_count
&& !ret
; i
++)
1976 HKEY hsubkey_src
, hsubkey_dst
;
1977 DWORD length
= max_key_len
;
1979 ret
= RegEnumKeyExW(hkey_src
, i
, ptr_name
, &length
, NULL
, NULL
, NULL
, NULL
);
1982 ret
= RegOpenKeyExW(hkey_src
, ptr_name
, 0, KEY_READ
, &hsubkey_src
);
1985 /* Create destination sub key */
1986 ret
= RegCreateKeyW(hkey_dst
, ptr_name
, &hsubkey_dst
);
1989 /* Recursively copy keys and values from the sub key */
1990 ret
= SHCopyKeyW(hsubkey_src
, NULL
, hsubkey_dst
, 0);
1991 RegCloseKey(hsubkey_dst
);
1994 RegCloseKey(hsubkey_src
);
1998 /* Copy all the values in this key */
1999 for (i
= 0; i
< value_count
&& !ret
; i
++)
2001 DWORD length
= max_key_len
, type
, data_len
= max_data_len
;
2003 ret
= RegEnumValueW(hkey_src
, i
, ptr_name
, &length
, NULL
, &type
, ptr
, &data_len
);
2005 ret
= SHSetValueW(hkey_dst
, NULL
, ptr_name
, type
, ptr
, data_len
);
2009 /* Free buffers if allocated */
2010 if (ptr_name
!= name
)
2011 heap_free(ptr_name
);
2015 if (subkey
&& hkey_src
)
2016 RegCloseKey(hkey_src
);
2022 /*************************************************************************
2023 * SHEnumKeyExA [SHCORE.@]
2025 LONG WINAPI
SHEnumKeyExA(HKEY hkey
, DWORD index
, char *subkey
, DWORD
*length
)
2027 TRACE("(%p, %d, %s, %p)\n", hkey
, index
, debugstr_a(subkey
), length
);
2029 return RegEnumKeyExA(hkey
, index
, subkey
, length
, NULL
, NULL
, NULL
, NULL
);
2032 /*************************************************************************
2033 * SHEnumKeyExW [SHCORE.@]
2035 LONG WINAPI
SHEnumKeyExW(HKEY hkey
, DWORD index
, WCHAR
*subkey
, DWORD
*length
)
2037 TRACE("(%p, %d, %s, %p)\n", hkey
, index
, debugstr_w(subkey
), length
);
2039 return RegEnumKeyExW(hkey
, index
, subkey
, length
, NULL
, NULL
, NULL
, NULL
);
2042 /*************************************************************************
2043 * SHEnumValueA [SHCORE.@]
2045 LONG WINAPI
SHEnumValueA(HKEY hkey
, DWORD index
, char *value
, DWORD
*length
, DWORD
*type
,
2046 void *data
, DWORD
*data_len
)
2048 TRACE("(%p, %d, %s, %p, %p, %p, %p)\n", hkey
, index
, debugstr_a(value
), length
, type
, data
, data_len
);
2050 return RegEnumValueA(hkey
, index
, value
, length
, NULL
, type
, data
, data_len
);
2053 /*************************************************************************
2054 * SHEnumValueW [SHCORE.@]
2056 LONG WINAPI
SHEnumValueW(HKEY hkey
, DWORD index
, WCHAR
*value
, DWORD
*length
, DWORD
*type
,
2057 void *data
, DWORD
*data_len
)
2059 TRACE("(%p, %d, %s, %p, %p, %p, %p)\n", hkey
, index
, debugstr_w(value
), length
, type
, data
, data_len
);
2061 return RegEnumValueW(hkey
, index
, value
, length
, NULL
, type
, data
, data_len
);
2064 /*************************************************************************
2065 * SHQueryValueExW [SHCORE.@]
2067 DWORD WINAPI
SHQueryValueExW(HKEY hkey
, const WCHAR
*name
, DWORD
*reserved
, DWORD
*type
,
2068 void *buff
, DWORD
*buff_len
)
2070 DWORD ret
, value_type
, data_len
= 0;
2072 TRACE("(%p, %s, %p, %p, %p, %p)\n", hkey
, debugstr_w(name
), reserved
, type
, buff
, buff_len
);
2075 data_len
= *buff_len
;
2077 ret
= RegQueryValueExW(hkey
, name
, reserved
, &value_type
, buff
, &data_len
);
2078 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_MORE_DATA
)
2081 if (buff_len
&& value_type
== REG_EXPAND_SZ
)
2086 if (!buff
|| ret
== ERROR_MORE_DATA
)
2089 value
= heap_alloc(length
);
2090 RegQueryValueExW(hkey
, name
, reserved
, NULL
, (BYTE
*)value
, &length
);
2091 length
= ExpandEnvironmentStringsW(value
, NULL
, 0);
2095 length
= (lstrlenW(buff
) + 1) * sizeof(WCHAR
);
2096 value
= heap_alloc(length
);
2097 memcpy(value
, buff
, length
);
2098 length
= ExpandEnvironmentStringsW(value
, buff
, *buff_len
/ sizeof(WCHAR
));
2099 if (length
> *buff_len
) ret
= ERROR_MORE_DATA
;
2101 data_len
= max(data_len
, length
);
2106 *type
= value_type
== REG_EXPAND_SZ
? REG_SZ
: value_type
;
2108 *buff_len
= data_len
;
2112 /*************************************************************************
2113 * SHQueryValueExA [SHCORE.@]
2115 DWORD WINAPI
SHQueryValueExA(HKEY hkey
, const char *name
, DWORD
*reserved
, DWORD
*type
,
2116 void *buff
, DWORD
*buff_len
)
2118 DWORD ret
, value_type
, data_len
= 0;
2120 TRACE("(%p, %s, %p, %p, %p, %p)\n", hkey
, debugstr_a(name
), reserved
, type
, buff
, buff_len
);
2123 data_len
= *buff_len
;
2125 ret
= RegQueryValueExA(hkey
, name
, reserved
, &value_type
, buff
, &data_len
);
2126 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_MORE_DATA
)
2129 if (buff_len
&& value_type
== REG_EXPAND_SZ
)
2134 if (!buff
|| ret
== ERROR_MORE_DATA
)
2137 value
= heap_alloc(length
);
2138 RegQueryValueExA(hkey
, name
, reserved
, NULL
, (BYTE
*)value
, &length
);
2139 length
= ExpandEnvironmentStringsA(value
, NULL
, 0);
2143 length
= strlen(buff
) + 1;
2144 value
= heap_alloc(length
);
2145 memcpy(value
, buff
, length
);
2146 length
= ExpandEnvironmentStringsA(value
, buff
, *buff_len
);
2147 if (length
> *buff_len
) ret
= ERROR_MORE_DATA
;
2149 data_len
= max(data_len
, length
);
2154 *type
= value_type
== REG_EXPAND_SZ
? REG_SZ
: value_type
;
2156 *buff_len
= data_len
;
2160 /*************************************************************************
2161 * SHGetValueA [SHCORE.@]
2163 DWORD WINAPI
SHGetValueA(HKEY hkey
, const char *subkey
, const char *value
,
2164 DWORD
*type
, void *data
, DWORD
*data_len
)
2169 TRACE("(%p, %s, %s, %p, %p, %p)\n", hkey
, debugstr_a(subkey
), debugstr_a(value
),
2170 type
, data
, data_len
);
2173 ret
= RegOpenKeyExA(hkey
, subkey
, 0, KEY_QUERY_VALUE
, &hsubkey
);
2177 ret
= SHQueryValueExA(hsubkey
? hsubkey
: hkey
, value
, 0, type
, data
, data_len
);
2179 RegCloseKey(hsubkey
);
2185 /*************************************************************************
2186 * SHGetValueW [SHCORE.@]
2188 DWORD WINAPI
SHGetValueW(HKEY hkey
, const WCHAR
*subkey
, const WCHAR
*value
,
2189 DWORD
*type
, void *data
, DWORD
*data_len
)
2194 TRACE("(%p, %s, %s, %p, %p, %p)\n", hkey
, debugstr_w(subkey
), debugstr_w(value
),
2195 type
, data
, data_len
);
2198 ret
= RegOpenKeyExW(hkey
, subkey
, 0, KEY_QUERY_VALUE
, &hsubkey
);
2202 ret
= SHQueryValueExW(hsubkey
? hsubkey
: hkey
, value
, 0, type
, data
, data_len
);
2204 RegCloseKey(hsubkey
);
2210 /*************************************************************************
2211 * SHRegGetIntW [SHCORE.280]
2213 int WINAPI
SHRegGetIntW(HKEY hkey
, const WCHAR
*value
, int default_value
)
2218 TRACE("(%p, %s, %d)\n", hkey
, debugstr_w(value
), default_value
);
2221 buff_len
= sizeof(buff
);
2222 if (SHQueryValueExW(hkey
, value
, 0, 0, buff
, &buff_len
))
2223 return default_value
;
2225 if (*buff
>= '0' && *buff
<= '9')
2226 return wcstol(buff
, NULL
, 10);
2228 return default_value
;
2231 /*************************************************************************
2232 * SHRegGetPathA [SHCORE.@]
2234 DWORD WINAPI
SHRegGetPathA(HKEY hkey
, const char *subkey
, const char *value
, char *path
, DWORD flags
)
2236 DWORD length
= MAX_PATH
;
2238 TRACE("(%p, %s, %s, %p, %#x)\n", hkey
, debugstr_a(subkey
), debugstr_a(value
), path
, flags
);
2240 return SHGetValueA(hkey
, subkey
, value
, 0, path
, &length
);
2243 /*************************************************************************
2244 * SHRegGetPathW [SHCORE.@]
2246 DWORD WINAPI
SHRegGetPathW(HKEY hkey
, const WCHAR
*subkey
, const WCHAR
*value
, WCHAR
*path
, DWORD flags
)
2248 DWORD length
= MAX_PATH
;
2250 TRACE("(%p, %s, %s, %p, %d)\n", hkey
, debugstr_w(subkey
), debugstr_w(value
), path
, flags
);
2252 return SHGetValueW(hkey
, subkey
, value
, 0, path
, &length
);
2255 /*************************************************************************
2256 * SHSetValueW [SHCORE.@]
2258 DWORD WINAPI
SHSetValueW(HKEY hkey
, const WCHAR
*subkey
, const WCHAR
*value
, DWORD type
,
2259 const void *data
, DWORD data_len
)
2261 DWORD ret
= ERROR_SUCCESS
, dummy
;
2264 TRACE("(%p, %s, %s, %d, %p, %d)\n", hkey
, debugstr_w(subkey
), debugstr_w(value
),
2265 type
, data
, data_len
);
2267 if (subkey
&& *subkey
)
2268 ret
= RegCreateKeyExW(hkey
, subkey
, 0, NULL
, 0, KEY_SET_VALUE
, NULL
, &hsubkey
, &dummy
);
2274 ret
= RegSetValueExW(hsubkey
, value
, 0, type
, data
, data_len
);
2275 if (hsubkey
!= hkey
)
2276 RegCloseKey(hsubkey
);
2282 /*************************************************************************
2283 * SHSetValueA [SHCORE.@]
2285 DWORD WINAPI
SHSetValueA(HKEY hkey
, const char *subkey
, const char *value
,
2286 DWORD type
, const void *data
, DWORD data_len
)
2288 DWORD ret
= ERROR_SUCCESS
, dummy
;
2291 TRACE("(%p, %s, %s, %d, %p, %d)\n", hkey
, debugstr_a(subkey
), debugstr_a(value
),
2292 type
, data
, data_len
);
2294 if (subkey
&& *subkey
)
2295 ret
= RegCreateKeyExA(hkey
, subkey
, 0, NULL
, 0, KEY_SET_VALUE
, NULL
, &hsubkey
, &dummy
);
2301 ret
= RegSetValueExA(hsubkey
, value
, 0, type
, data
, data_len
);
2302 if (hsubkey
!= hkey
)
2303 RegCloseKey(hsubkey
);
2309 /*************************************************************************
2310 * SHRegSetPathA [SHCORE.@]
2312 DWORD WINAPI
SHRegSetPathA(HKEY hkey
, const char *subkey
, const char *value
, const char *path
, DWORD flags
)
2314 FIXME("(%p, %s, %s, %s, %#x) - semi-stub\n", hkey
, debugstr_a(subkey
),
2315 debugstr_a(value
), debugstr_a(path
), flags
);
2317 /* FIXME: PathUnExpandEnvStringsA() */
2319 return SHSetValueA(hkey
, subkey
, value
, REG_SZ
, path
, lstrlenA(path
));
2322 /*************************************************************************
2323 * SHRegSetPathW [SHCORE.@]
2325 DWORD WINAPI
SHRegSetPathW(HKEY hkey
, const WCHAR
*subkey
, const WCHAR
*value
, const WCHAR
*path
, DWORD flags
)
2327 FIXME("(%p, %s, %s, %s, %#x) - semi-stub\n", hkey
, debugstr_w(subkey
),
2328 debugstr_w(value
), debugstr_w(path
), flags
);
2330 /* FIXME: PathUnExpandEnvStringsW(); */
2332 return SHSetValueW(hkey
, subkey
, value
, REG_SZ
, path
, lstrlenW(path
));
2335 /*************************************************************************
2336 * SHQueryInfoKeyA [SHCORE.@]
2338 LONG WINAPI
SHQueryInfoKeyA(HKEY hkey
, DWORD
*subkeys
, DWORD
*subkey_max
, DWORD
*values
, DWORD
*value_max
)
2340 TRACE("(%p, %p, %p, %p, %p)\n", hkey
, subkeys
, subkey_max
, values
, value_max
);
2342 return RegQueryInfoKeyA(hkey
, NULL
, NULL
, NULL
, subkeys
, subkey_max
, NULL
, values
, value_max
, NULL
, NULL
, NULL
);
2345 /*************************************************************************
2346 * SHQueryInfoKeyW [SHCORE.@]
2348 LONG WINAPI
SHQueryInfoKeyW(HKEY hkey
, DWORD
*subkeys
, DWORD
*subkey_max
, DWORD
*values
, DWORD
*value_max
)
2350 TRACE("(%p, %p, %p, %p, %p)\n", hkey
, subkeys
, subkey_max
, values
, value_max
);
2352 return RegQueryInfoKeyW(hkey
, NULL
, NULL
, NULL
, subkeys
, subkey_max
, NULL
, values
, value_max
, NULL
, NULL
, NULL
);
2355 /*************************************************************************
2358 BOOL WINAPI
IsOS(DWORD feature
)
2360 DWORD platform
, majorv
, minorv
;
2361 OSVERSIONINFOA osvi
;
2363 osvi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFOA
);
2364 if (!GetVersionExA(&osvi
))
2367 majorv
= osvi
.dwMajorVersion
;
2368 minorv
= osvi
.dwMinorVersion
;
2369 platform
= osvi
.dwPlatformId
;
2371 #define ISOS_RETURN(x) \
2372 TRACE("(0x%x) ret=%d\n",feature,(x)); \
2376 case OS_WIN32SORGREATER
:
2377 ISOS_RETURN(platform
== VER_PLATFORM_WIN32s
2378 || platform
== VER_PLATFORM_WIN32_WINDOWS
);
2380 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
);
2381 case OS_WIN95ORGREATER
:
2382 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_WINDOWS
);
2383 case OS_NT4ORGREATER
:
2384 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& majorv
>= 4);
2385 case OS_WIN2000ORGREATER_ALT
:
2386 case OS_WIN2000ORGREATER
:
2387 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& majorv
>= 5);
2388 case OS_WIN98ORGREATER
:
2389 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_WINDOWS
&& minorv
>= 10);
2391 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_WINDOWS
&& minorv
== 10);
2393 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& majorv
>= 5);
2394 case OS_WIN2000SERVER
:
2395 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& (minorv
== 0 || minorv
== 1));
2396 case OS_WIN2000ADVSERVER
:
2397 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& (minorv
== 0 || minorv
== 1));
2398 case OS_WIN2000DATACENTER
:
2399 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& (minorv
== 0 || minorv
== 1));
2400 case OS_WIN2000TERMINAL
:
2401 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& (minorv
== 0 || minorv
== 1));
2403 FIXME("(OS_EMBEDDED) What should we return here?\n");
2405 case OS_TERMINALCLIENT
:
2406 FIXME("(OS_TERMINALCLIENT) What should we return here?\n");
2408 case OS_TERMINALREMOTEADMIN
:
2409 FIXME("(OS_TERMINALREMOTEADMIN) What should we return here?\n");
2412 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_WINDOWS
&& minorv
== 0);
2413 case OS_MEORGREATER
:
2414 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_WINDOWS
&& minorv
>= 90);
2415 case OS_XPORGREATER
:
2416 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& majorv
>= 5 && minorv
>= 1);
2418 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& majorv
>= 5 && minorv
>= 1);
2419 case OS_PROFESSIONAL
:
2420 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
);
2422 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
);
2424 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& majorv
>= 5);
2426 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
);
2427 case OS_TERMINALSERVER
:
2428 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
);
2429 case OS_PERSONALTERMINALSERVER
:
2430 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& minorv
>= 1 && majorv
>= 5);
2431 case OS_FASTUSERSWITCHING
:
2432 FIXME("(OS_FASTUSERSWITCHING) What should we return here?\n");
2434 case OS_WELCOMELOGONUI
:
2435 FIXME("(OS_WELCOMELOGONUI) What should we return here?\n");
2437 case OS_DOMAINMEMBER
:
2438 FIXME("(OS_DOMAINMEMBER) What should we return here?\n");
2441 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
);
2445 IsWow64Process(GetCurrentProcess(), &is_wow64
);
2449 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
);
2450 case OS_SMALLBUSINESSSERVER
:
2451 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
);
2453 FIXME("(OS_TABLETPC) What should we return here?\n");
2455 case OS_SERVERADMINUI
:
2456 FIXME("(OS_SERVERADMINUI) What should we return here?\n");
2458 case OS_MEDIACENTER
:
2459 FIXME("(OS_MEDIACENTER) What should we return here?\n");
2462 FIXME("(OS_APPLIANCE) What should we return here?\n");
2464 case 0x25: /*OS_VISTAORGREATER*/
2465 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& majorv
>= 6);
2470 WARN("(0x%x) unknown parameter\n", feature
);