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_WINE_PREATTACH
:
48 return FALSE
; /* prefer native version */
49 case DLL_PROCESS_ATTACH
:
50 DisableThreadLibraryCalls(instance
);
51 shcore_tls
= TlsAlloc();
53 case DLL_PROCESS_DETACH
:
55 if (shcore_tls
!= TLS_OUT_OF_INDEXES
)
63 HRESULT WINAPI
GetProcessDpiAwareness(HANDLE process
, PROCESS_DPI_AWARENESS
*value
)
65 if (GetProcessDpiAwarenessInternal( process
, (DPI_AWARENESS
*)value
)) return S_OK
;
66 return HRESULT_FROM_WIN32( GetLastError() );
69 HRESULT WINAPI
SetProcessDpiAwareness(PROCESS_DPI_AWARENESS value
)
71 if (SetProcessDpiAwarenessInternal( value
)) return S_OK
;
72 return HRESULT_FROM_WIN32( GetLastError() );
75 HRESULT WINAPI
GetDpiForMonitor(HMONITOR monitor
, MONITOR_DPI_TYPE type
, UINT
*x
, UINT
*y
)
77 if (GetDpiForMonitorInternal( monitor
, type
, x
, y
)) return S_OK
;
78 return HRESULT_FROM_WIN32( GetLastError() );
81 HRESULT WINAPI
GetScaleFactorForMonitor(HMONITOR monitor
, DEVICE_SCALE_FACTOR
*scale
)
83 FIXME("(%p %p): stub\n", monitor
, scale
);
85 *scale
= SCALE_100_PERCENT
;
89 HRESULT WINAPI
_IStream_Read(IStream
*stream
, void *dest
, ULONG size
)
94 TRACE("(%p, %p, %u)\n", stream
, dest
, size
);
96 hr
= IStream_Read(stream
, dest
, size
, &read
);
97 if (SUCCEEDED(hr
) && read
!= size
)
102 HRESULT WINAPI
IStream_Reset(IStream
*stream
)
104 static const LARGE_INTEGER zero
;
106 TRACE("(%p)\n", stream
);
108 return IStream_Seek(stream
, zero
, 0, NULL
);
111 HRESULT WINAPI
IStream_Size(IStream
*stream
, ULARGE_INTEGER
*size
)
116 TRACE("(%p, %p)\n", stream
, size
);
118 memset(&statstg
, 0, sizeof(statstg
));
120 hr
= IStream_Stat(stream
, &statstg
, STATFLAG_NONAME
);
122 if (SUCCEEDED(hr
) && size
)
123 *size
= statstg
.cbSize
;
127 HRESULT WINAPI
_IStream_Write(IStream
*stream
, const void *src
, ULONG size
)
132 TRACE("(%p, %p, %u)\n", stream
, src
, size
);
134 hr
= IStream_Write(stream
, src
, size
, &written
);
135 if (SUCCEEDED(hr
) && written
!= size
)
141 void WINAPI
IUnknown_AtomicRelease(IUnknown
**obj
)
143 TRACE("(%p)\n", obj
);
148 IUnknown_Release(*obj
);
152 HRESULT WINAPI
IUnknown_GetSite(IUnknown
*unk
, REFIID iid
, void **site
)
154 IObjectWithSite
*obj
= NULL
;
155 HRESULT hr
= E_INVALIDARG
;
157 TRACE("(%p, %s, %p)\n", unk
, debugstr_guid(iid
), site
);
159 if (unk
&& iid
&& site
)
161 hr
= IUnknown_QueryInterface(unk
, &IID_IObjectWithSite
, (void **)&obj
);
162 if (SUCCEEDED(hr
) && obj
)
164 hr
= IObjectWithSite_GetSite(obj
, iid
, site
);
165 IObjectWithSite_Release(obj
);
172 HRESULT WINAPI
IUnknown_QueryService(IUnknown
*obj
, REFGUID sid
, REFIID iid
, void **out
)
174 IServiceProvider
*provider
= NULL
;
185 hr
= IUnknown_QueryInterface(obj
, &IID_IServiceProvider
, (void **)&provider
);
186 if (hr
== S_OK
&& provider
)
188 TRACE("Using provider %p.\n", provider
);
190 hr
= IServiceProvider_QueryService(provider
, sid
, iid
, out
);
192 TRACE("Provider %p returned %p.\n", provider
, *out
);
194 IServiceProvider_Release(provider
);
200 void WINAPI
IUnknown_Set(IUnknown
**dest
, IUnknown
*src
)
202 TRACE("(%p, %p)\n", dest
, src
);
204 IUnknown_AtomicRelease(dest
);
208 IUnknown_AddRef(src
);
213 HRESULT WINAPI
IUnknown_SetSite(IUnknown
*obj
, IUnknown
*site
)
215 IInternetSecurityManager
*sec_manager
;
216 IObjectWithSite
*objwithsite
;
222 hr
= IUnknown_QueryInterface(obj
, &IID_IObjectWithSite
, (void **)&objwithsite
);
223 TRACE("ObjectWithSite %p, hr %#x.\n", objwithsite
, hr
);
226 hr
= IObjectWithSite_SetSite(objwithsite
, site
);
227 TRACE("SetSite() hr %#x.\n", hr
);
228 IObjectWithSite_Release(objwithsite
);
232 hr
= IUnknown_QueryInterface(obj
, &IID_IInternetSecurityManager
, (void **)&sec_manager
);
233 TRACE("InternetSecurityManager %p, hr %#x.\n", sec_manager
, hr
);
237 hr
= IInternetSecurityManager_SetSecuritySite(sec_manager
, (IInternetSecurityMgrSite
*)site
);
238 TRACE("SetSecuritySite() hr %#x.\n", hr
);
239 IInternetSecurityManager_Release(sec_manager
);
245 HRESULT WINAPI
SetCurrentProcessExplicitAppUserModelID(const WCHAR
*appid
)
247 FIXME("%s: stub\n", debugstr_w(appid
));
251 HRESULT WINAPI
GetCurrentProcessExplicitAppUserModelID(const WCHAR
**appid
)
253 FIXME("%p: stub\n", appid
);
258 /*************************************************************************
259 * CommandLineToArgvW [SHCORE.@]
261 * We must interpret the quotes in the command line to rebuild the argv
263 * - arguments are separated by spaces or tabs
264 * - quotes serve as optional argument delimiters
266 * - escaped quotes must be converted back to '"'
268 * - consecutive backslashes preceding a quote see their number halved with
269 * the remainder escaping the quote:
270 * 2n backslashes + quote -> n backslashes + quote as an argument delimiter
271 * 2n+1 backslashes + quote -> n backslashes + literal quote
272 * - backslashes that are not followed by a quote are copied literally:
275 * - in quoted strings, consecutive quotes see their number divided by three
276 * with the remainder modulo 3 deciding whether to close the string or not.
277 * Note that the opening quote must be counted in the consecutive quotes,
278 * that's the (1+) below:
279 * (1+) 3n quotes -> n quotes
280 * (1+) 3n+1 quotes -> n quotes plus closes the quoted string
281 * (1+) 3n+2 quotes -> n+1 quotes plus closes the quoted string
282 * - in unquoted strings, the first quote opens the quoted string and the
283 * remaining consecutive quotes follow the above rule.
285 WCHAR
** WINAPI
CommandLineToArgvW(const WCHAR
*cmdline
, int *numargs
)
295 SetLastError(ERROR_INVALID_PARAMETER
);
301 /* Return the path to the executable */
302 DWORD len
, deslen
= MAX_PATH
, size
;
304 size
= sizeof(WCHAR
*) * 2 + deslen
* sizeof(WCHAR
);
307 if (!(argv
= LocalAlloc(LMEM_FIXED
, size
))) return NULL
;
308 len
= GetModuleFileNameW(0, (WCHAR
*)(argv
+ 2), deslen
);
314 if (len
< deslen
) break;
316 size
= sizeof(WCHAR
*) * 2 + deslen
* sizeof(WCHAR
);
319 argv
[0] = (WCHAR
*)(argv
+ 2);
326 /* --- First count the arguments */
329 /* The first argument, the executable path, follows special rules */
332 /* The executable path ends at the next quote, no matter what */
340 /* The executable path ends at the next space, no matter what */
341 while (*s
&& *s
!= ' ' && *s
!= '\t')
344 /* skip to the first argument, if any */
345 while (*s
== ' ' || *s
== '\t')
350 /* Analyze the remaining arguments */
354 if ((*s
== ' ' || *s
== '\t') && qcount
== 0)
356 /* skip to the next argument and count it if any */
357 while (*s
== ' ' || *s
== '\t')
365 /* '\', count them */
372 if ((bcount
& 1) == 0)
373 qcount
++; /* unescaped '"' */
376 /* consecutive quotes, see comment in copying code below */
388 /* a regular character */
394 /* Allocate in a single lump, the string array, and the strings that go
395 * with it. This way the caller can make a single LocalFree() call to free
398 argv
= LocalAlloc(LMEM_FIXED
, (argc
+ 1) * sizeof(WCHAR
*) + (lstrlenW(cmdline
) + 1) * sizeof(WCHAR
));
402 /* --- Then split and copy the arguments */
403 argv
[0] = d
= lstrcpyW((WCHAR
*)(argv
+ argc
+ 1), cmdline
);
405 /* The first argument, the executable path, follows special rules */
408 /* The executable path ends at the next quote, no matter what */
422 /* The executable path ends at the next space, no matter what */
423 while (*d
&& *d
!= ' ' && *d
!= '\t')
429 /* close the executable path */
431 /* skip to the first argument and initialize it if any */
432 while (*s
== ' ' || *s
== '\t')
436 /* There are no parameters so we are all done */
442 /* Split and copy the remaining arguments */
447 if ((*s
== ' ' || *s
== '\t') && qcount
== 0)
449 /* close the argument */
453 /* skip to the next one and initialize it if any */
456 } while (*s
== ' ' || *s
== '\t');
467 if ((bcount
& 1) == 0)
469 /* Preceded by an even number of '\', this is half that
470 * number of '\', plus a quote which we erase.
477 /* Preceded by an odd number of '\', this is half that
478 * number of '\' followed by a '"'
480 d
= d
- bcount
/ 2 - 1;
485 /* Now count the number of consecutive quotes. Note that qcount
486 * already takes into account the opening quote if any, as well as
487 * the quote that lead us here.
503 /* a regular character */
517 IStream IStream_iface
;
540 static inline struct shstream
*impl_from_IStream(IStream
*iface
)
542 return CONTAINING_RECORD(iface
, struct shstream
, IStream_iface
);
545 static HRESULT WINAPI
shstream_QueryInterface(IStream
*iface
, REFIID riid
, void **out
)
547 struct shstream
*stream
= impl_from_IStream(iface
);
549 TRACE("(%p)->(%s, %p)\n", stream
, debugstr_guid(riid
), out
);
551 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IStream
))
554 IStream_AddRef(iface
);
559 WARN("Unsupported interface %s.\n", debugstr_guid(riid
));
560 return E_NOINTERFACE
;
563 static ULONG WINAPI
shstream_AddRef(IStream
*iface
)
565 struct shstream
*stream
= impl_from_IStream(iface
);
566 ULONG refcount
= InterlockedIncrement(&stream
->refcount
);
568 TRACE("(%p)->(%u)\n", stream
, refcount
);
573 static ULONG WINAPI
memstream_Release(IStream
*iface
)
575 struct shstream
*stream
= impl_from_IStream(iface
);
576 ULONG refcount
= InterlockedDecrement(&stream
->refcount
);
578 TRACE("(%p)->(%u)\n", stream
, refcount
);
582 heap_free(stream
->u
.mem
.buffer
);
589 static HRESULT WINAPI
memstream_Read(IStream
*iface
, void *buff
, ULONG buff_size
, ULONG
*read_len
)
591 struct shstream
*stream
= impl_from_IStream(iface
);
594 TRACE("(%p)->(%p, %u, %p)\n", stream
, buff
, buff_size
, read_len
);
596 if (stream
->u
.mem
.position
>= stream
->u
.mem
.length
)
599 length
= stream
->u
.mem
.length
- stream
->u
.mem
.position
;
601 length
= buff_size
> length
? length
: buff_size
;
602 if (length
!= 0) /* not at end of buffer and we want to read something */
604 memmove(buff
, stream
->u
.mem
.buffer
+ stream
->u
.mem
.position
, length
);
605 stream
->u
.mem
.position
+= length
; /* adjust pointer */
614 static HRESULT WINAPI
memstream_Write(IStream
*iface
, const void *buff
, ULONG buff_size
, ULONG
*written
)
616 struct shstream
*stream
= impl_from_IStream(iface
);
617 DWORD length
= stream
->u
.mem
.position
+ buff_size
;
619 TRACE("(%p)->(%p, %u, %p)\n", stream
, buff
, buff_size
, written
);
621 if (length
< stream
->u
.mem
.position
) /* overflow */
622 return STG_E_INSUFFICIENTMEMORY
;
624 if (length
> stream
->u
.mem
.length
)
626 BYTE
*buffer
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, stream
->u
.mem
.buffer
, length
);
628 return STG_E_INSUFFICIENTMEMORY
;
630 stream
->u
.mem
.length
= length
;
631 stream
->u
.mem
.buffer
= buffer
;
633 memmove(stream
->u
.mem
.buffer
+ stream
->u
.mem
.position
, buff
, buff_size
);
634 stream
->u
.mem
.position
+= buff_size
; /* adjust pointer */
637 *written
= buff_size
;
642 static HRESULT WINAPI
memstream_Seek(IStream
*iface
, LARGE_INTEGER move
, DWORD origin
, ULARGE_INTEGER
*new_pos
)
644 struct shstream
*stream
= impl_from_IStream(iface
);
647 TRACE("(%p)->(%s, %d, %p)\n", stream
, wine_dbgstr_longlong(move
.QuadPart
), origin
, new_pos
);
649 if (origin
== STREAM_SEEK_SET
)
651 else if (origin
== STREAM_SEEK_CUR
)
652 tmp
.QuadPart
= stream
->u
.mem
.position
+ move
.QuadPart
;
653 else if (origin
== STREAM_SEEK_END
)
654 tmp
.QuadPart
= stream
->u
.mem
.length
+ move
.QuadPart
;
656 return STG_E_INVALIDPARAMETER
;
658 if (tmp
.QuadPart
< 0)
659 return STG_E_INVALIDFUNCTION
;
661 /* we cut off the high part here */
662 stream
->u
.mem
.position
= tmp
.u
.LowPart
;
665 new_pos
->QuadPart
= stream
->u
.mem
.position
;
669 static HRESULT WINAPI
memstream_SetSize(IStream
*iface
, ULARGE_INTEGER new_size
)
671 struct shstream
*stream
= impl_from_IStream(iface
);
675 TRACE("(%p, %s)\n", stream
, wine_dbgstr_longlong(new_size
.QuadPart
));
677 /* we cut off the high part here */
678 length
= new_size
.u
.LowPart
;
679 buffer
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, stream
->u
.mem
.buffer
, length
);
681 return STG_E_INSUFFICIENTMEMORY
;
683 stream
->u
.mem
.buffer
= buffer
;
684 stream
->u
.mem
.length
= length
;
689 static HRESULT WINAPI
shstream_CopyTo(IStream
*iface
, IStream
*pstm
, ULARGE_INTEGER size
, ULARGE_INTEGER
*read_len
, ULARGE_INTEGER
*written
)
691 struct shstream
*stream
= impl_from_IStream(iface
);
693 TRACE("(%p)\n", stream
);
696 read_len
->QuadPart
= 0;
699 written
->QuadPart
= 0;
705 static HRESULT WINAPI
shstream_Commit(IStream
*iface
, DWORD flags
)
707 struct shstream
*stream
= impl_from_IStream(iface
);
709 TRACE("(%p, %#x)\n", stream
, flags
);
711 /* Commit is not supported by this stream */
715 static HRESULT WINAPI
shstream_Revert(IStream
*iface
)
717 struct shstream
*stream
= impl_from_IStream(iface
);
719 TRACE("(%p)\n", stream
);
721 /* revert not supported by this stream */
725 static HRESULT WINAPI
shstream_LockRegion(IStream
*iface
, ULARGE_INTEGER offset
, ULARGE_INTEGER size
, DWORD lock_type
)
727 struct shstream
*stream
= impl_from_IStream(iface
);
729 TRACE("(%p)\n", stream
);
731 /* lock/unlock not supported by this stream */
735 static HRESULT WINAPI
shstream_UnlockRegion(IStream
*iface
, ULARGE_INTEGER offset
, ULARGE_INTEGER size
, DWORD lock_type
)
737 struct shstream
*stream
= impl_from_IStream(iface
);
739 TRACE("(%p)\n", stream
);
741 /* lock/unlock not supported by this stream */
745 static HRESULT WINAPI
memstream_Stat(IStream
*iface
, STATSTG
*statstg
, DWORD flags
)
747 struct shstream
*stream
= impl_from_IStream(iface
);
749 TRACE("(%p, %p, %#x)\n", stream
, statstg
, flags
);
751 memset(statstg
, 0, sizeof(*statstg
));
752 statstg
->type
= STGTY_STREAM
;
753 statstg
->cbSize
.QuadPart
= stream
->u
.mem
.length
;
754 statstg
->grfMode
= STGM_READWRITE
;
759 static HRESULT WINAPI
shstream_Clone(IStream
*iface
, IStream
**dest
)
761 struct shstream
*stream
= impl_from_IStream(iface
);
763 TRACE("(%p, %p)\n", stream
, dest
);
767 /* clone not supported by this stream */
771 static const IStreamVtbl memstreamvtbl
=
773 shstream_QueryInterface
,
784 shstream_UnlockRegion
,
789 static struct shstream
*shstream_create(const IStreamVtbl
*vtbl
, const BYTE
*data
, UINT data_len
)
791 struct shstream
*stream
;
796 stream
= heap_alloc(sizeof(*stream
));
797 stream
->IStream_iface
.lpVtbl
= vtbl
;
798 stream
->refcount
= 1;
799 stream
->u
.mem
.buffer
= heap_alloc(data_len
);
800 if (!stream
->u
.mem
.buffer
)
805 memcpy(stream
->u
.mem
.buffer
, data
, data_len
);
806 stream
->u
.mem
.length
= data_len
;
807 stream
->u
.mem
.position
= 0;
812 /*************************************************************************
813 * SHCreateMemStream [SHCORE.@]
815 * Create an IStream object on a block of memory.
818 * data [I] Memory block to create the IStream object on
819 * data_len [I] Length of data block
822 * Success: A pointer to the IStream object.
823 * Failure: NULL, if any parameters are invalid or an error occurs.
826 * A copy of the memory block is made, it's freed when the stream is released.
828 IStream
* WINAPI
SHCreateMemStream(const BYTE
*data
, UINT data_len
)
830 struct shstream
*stream
;
832 TRACE("(%p, %u)\n", data
, data_len
);
834 stream
= shstream_create(&memstreamvtbl
, data
, data_len
);
835 return stream
? &stream
->IStream_iface
: NULL
;
838 static ULONG WINAPI
filestream_Release(IStream
*iface
)
840 struct shstream
*stream
= impl_from_IStream(iface
);
841 ULONG refcount
= InterlockedDecrement(&stream
->refcount
);
843 TRACE("(%p)->(%u)\n", stream
, refcount
);
847 CloseHandle(stream
->u
.file
.handle
);
848 heap_free(stream
->u
.file
.path
);
855 static HRESULT WINAPI
filestream_Read(IStream
*iface
, void *buff
, ULONG size
, ULONG
*read_len
)
857 struct shstream
*stream
= impl_from_IStream(iface
);
860 TRACE("(%p, %p, %u, %p)\n", stream
, buff
, size
, read_len
);
862 if (!ReadFile(stream
->u
.file
.handle
, buff
, size
, &read
, NULL
))
864 WARN("error %d reading file\n", GetLastError());
871 return read
== size
? S_OK
: S_FALSE
;
874 static HRESULT WINAPI
filestream_Write(IStream
*iface
, const void *buff
, ULONG size
, ULONG
*written
)
876 struct shstream
*stream
= impl_from_IStream(iface
);
877 DWORD written_len
= 0;
879 TRACE("(%p, %p, %u, %p)\n", stream
, buff
, size
, written
);
881 switch (stream
->u
.file
.mode
& 0xf)
887 return STG_E_ACCESSDENIED
;
890 if (!WriteFile(stream
->u
.file
.handle
, buff
, size
, &written_len
, NULL
))
891 return HRESULT_FROM_WIN32(GetLastError());
894 *written
= written_len
;
899 static HRESULT WINAPI
filestream_Seek(IStream
*iface
, LARGE_INTEGER move
, DWORD origin
, ULARGE_INTEGER
*new_pos
)
901 struct shstream
*stream
= impl_from_IStream(iface
);
904 TRACE("(%p, %s, %d, %p)\n", stream
, wine_dbgstr_longlong(move
.QuadPart
), origin
, new_pos
);
906 position
= SetFilePointer(stream
->u
.file
.handle
, move
.u
.LowPart
, NULL
, origin
);
907 if (position
== INVALID_SET_FILE_POINTER
)
908 return HRESULT_FROM_WIN32(GetLastError());
912 new_pos
->u
.HighPart
= 0;
913 new_pos
->u
.LowPart
= position
;
919 static HRESULT WINAPI
filestream_SetSize(IStream
*iface
, ULARGE_INTEGER size
)
921 struct shstream
*stream
= impl_from_IStream(iface
);
922 LARGE_INTEGER origin
, move
;
924 TRACE("(%p, %s)\n", stream
, wine_dbgstr_longlong(size
.QuadPart
));
927 if (!SetFilePointerEx(stream
->u
.file
.handle
, move
, &origin
, FILE_CURRENT
))
930 move
.QuadPart
= size
.QuadPart
;
931 if (!SetFilePointerEx(stream
->u
.file
.handle
, move
, NULL
, FILE_BEGIN
))
934 if (stream
->u
.file
.mode
!= STGM_READ
)
936 if (!SetEndOfFile(stream
->u
.file
.handle
))
938 if (!SetFilePointerEx(stream
->u
.file
.handle
, origin
, NULL
, FILE_BEGIN
))
945 static HRESULT WINAPI
filestream_CopyTo(IStream
*iface
, IStream
*dest
, ULARGE_INTEGER size
,
946 ULARGE_INTEGER
*read_len
, ULARGE_INTEGER
*written
)
948 struct shstream
*stream
= impl_from_IStream(iface
);
952 TRACE("(%p, %p, %s, %p, %p)\n", stream
, dest
, wine_dbgstr_longlong(size
.QuadPart
), read_len
, written
);
955 read_len
->QuadPart
= 0;
957 written
->QuadPart
= 0;
962 while (size
.QuadPart
)
964 ULONG left
, read_chunk
, written_chunk
;
966 left
= size
.QuadPart
> sizeof(buff
) ? sizeof(buff
) : size
.QuadPart
;
969 hr
= IStream_Read(iface
, buff
, left
, &read_chunk
);
970 if (FAILED(hr
) || read_chunk
== 0)
973 read_len
->QuadPart
+= read_chunk
;
976 hr
= IStream_Write(dest
, buff
, read_chunk
, &written_chunk
);
978 written
->QuadPart
+= written_chunk
;
979 if (FAILED(hr
) || written_chunk
!= left
)
982 size
.QuadPart
-= left
;
988 static HRESULT WINAPI
filestream_Commit(IStream
*iface
, DWORD flags
)
990 struct shstream
*stream
= impl_from_IStream(iface
);
992 TRACE("(%p, %#x)\n", stream
, flags
);
997 static HRESULT WINAPI
filestream_Stat(IStream
*iface
, STATSTG
*statstg
, DWORD flags
)
999 struct shstream
*stream
= impl_from_IStream(iface
);
1000 BY_HANDLE_FILE_INFORMATION fi
;
1002 TRACE("(%p, %p, %#x)\n", stream
, statstg
, flags
);
1005 return STG_E_INVALIDPOINTER
;
1007 memset(&fi
, 0, sizeof(fi
));
1008 GetFileInformationByHandle(stream
->u
.file
.handle
, &fi
);
1010 if (flags
& STATFLAG_NONAME
)
1011 statstg
->pwcsName
= NULL
;
1014 int len
= lstrlenW(stream
->u
.file
.path
);
1015 if ((statstg
->pwcsName
= CoTaskMemAlloc((len
+ 1) * sizeof(WCHAR
))))
1016 memcpy(statstg
->pwcsName
, stream
->u
.file
.path
, (len
+ 1) * sizeof(WCHAR
));
1019 statstg
->cbSize
.u
.LowPart
= fi
.nFileSizeLow
;
1020 statstg
->cbSize
.u
.HighPart
= fi
.nFileSizeHigh
;
1021 statstg
->mtime
= fi
.ftLastWriteTime
;
1022 statstg
->ctime
= fi
.ftCreationTime
;
1023 statstg
->atime
= fi
.ftLastAccessTime
;
1024 statstg
->grfMode
= stream
->u
.file
.mode
;
1025 statstg
->grfLocksSupported
= 0;
1026 memcpy(&statstg
->clsid
, &IID_IStream
, sizeof(CLSID
));
1027 statstg
->grfStateBits
= 0;
1028 statstg
->reserved
= 0;
1033 static const IStreamVtbl filestreamvtbl
=
1035 shstream_QueryInterface
,
1045 shstream_LockRegion
,
1046 shstream_UnlockRegion
,
1051 /*************************************************************************
1052 * SHCreateStreamOnFileEx [SHCORE.@]
1054 HRESULT WINAPI
SHCreateStreamOnFileEx(const WCHAR
*path
, DWORD mode
, DWORD attributes
,
1055 BOOL create
, IStream
*template, IStream
**ret
)
1057 DWORD access
, share
, creation_disposition
, len
;
1058 struct shstream
*stream
;
1061 TRACE("(%s, %d, 0x%08X, %d, %p, %p)\n", debugstr_w(path
), mode
, attributes
,
1062 create
, template, ret
);
1064 if (!path
|| !ret
|| template)
1065 return E_INVALIDARG
;
1073 case STGM_READWRITE
:
1074 access
= GENERIC_READ
| GENERIC_WRITE
;
1077 access
= GENERIC_READ
;
1080 return E_INVALIDARG
;
1084 switch (mode
& 0xf0)
1087 case STGM_SHARE_DENY_NONE
:
1088 share
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1090 case STGM_SHARE_DENY_READ
:
1091 share
= FILE_SHARE_WRITE
;
1093 case STGM_SHARE_DENY_WRITE
:
1094 share
= FILE_SHARE_READ
;
1096 case STGM_SHARE_EXCLUSIVE
:
1100 return E_INVALIDARG
;
1103 switch (mode
& 0xf000)
1105 case STGM_FAILIFTHERE
:
1106 creation_disposition
= create
? CREATE_NEW
: OPEN_EXISTING
;
1109 creation_disposition
= CREATE_ALWAYS
;
1112 return E_INVALIDARG
;
1115 hFile
= CreateFileW(path
, access
, share
, NULL
, creation_disposition
, attributes
, 0);
1116 if (hFile
== INVALID_HANDLE_VALUE
)
1117 return HRESULT_FROM_WIN32(GetLastError());
1119 stream
= heap_alloc(sizeof(*stream
));
1120 stream
->IStream_iface
.lpVtbl
= &filestreamvtbl
;
1121 stream
->refcount
= 1;
1122 stream
->u
.file
.handle
= hFile
;
1123 stream
->u
.file
.mode
= mode
;
1125 len
= lstrlenW(path
);
1126 stream
->u
.file
.path
= heap_alloc((len
+ 1) * sizeof(WCHAR
));
1127 memcpy(stream
->u
.file
.path
, path
, (len
+ 1) * sizeof(WCHAR
));
1129 *ret
= &stream
->IStream_iface
;
1134 /*************************************************************************
1135 * SHCreateStreamOnFileW [SHCORE.@]
1137 HRESULT WINAPI
SHCreateStreamOnFileW(const WCHAR
*path
, DWORD mode
, IStream
**stream
)
1139 TRACE("(%s, %#x, %p)\n", debugstr_w(path
), mode
, stream
);
1141 if (!path
|| !stream
)
1142 return E_INVALIDARG
;
1144 if ((mode
& (STGM_CONVERT
| STGM_DELETEONRELEASE
| STGM_TRANSACTED
)) != 0)
1145 return E_INVALIDARG
;
1147 return SHCreateStreamOnFileEx(path
, mode
, 0, FALSE
, NULL
, stream
);
1150 /*************************************************************************
1151 * SHCreateStreamOnFileA [SHCORE.@]
1153 HRESULT WINAPI
SHCreateStreamOnFileA(const char *path
, DWORD mode
, IStream
**stream
)
1159 TRACE("(%s, %#x, %p)\n", debugstr_a(path
), mode
, stream
);
1162 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND
);
1164 len
= MultiByteToWideChar(CP_ACP
, 0, path
, -1, NULL
, 0);
1165 pathW
= heap_alloc(len
* sizeof(WCHAR
));
1167 return E_OUTOFMEMORY
;
1169 MultiByteToWideChar(CP_ACP
, 0, path
, -1, pathW
, len
);
1170 hr
= SHCreateStreamOnFileW(pathW
, mode
, stream
);
1176 static ULONG WINAPI
regstream_Release(IStream
*iface
)
1178 struct shstream
*stream
= impl_from_IStream(iface
);
1179 ULONG refcount
= InterlockedDecrement(&stream
->refcount
);
1181 TRACE("(%p)->(%u)\n", stream
, refcount
);
1185 if (stream
->u
.mem
.hkey
)
1187 if (stream
->u
.mem
.length
)
1188 RegSetValueExW(stream
->u
.mem
.hkey
, stream
->u
.mem
.valuename
, 0, REG_BINARY
,
1189 (const BYTE
*)stream
->u
.mem
.buffer
, stream
->u
.mem
.length
);
1191 RegDeleteValueW(stream
->u
.mem
.hkey
, stream
->u
.mem
.valuename
);
1192 RegCloseKey(stream
->u
.mem
.hkey
);
1194 CoTaskMemFree(stream
->u
.mem
.valuename
);
1195 heap_free(stream
->u
.mem
.buffer
);
1202 static const IStreamVtbl regstreamvtbl
=
1204 shstream_QueryInterface
,
1214 shstream_LockRegion
,
1215 shstream_UnlockRegion
,
1220 /*************************************************************************
1221 * SHOpenRegStream2W [SHCORE.@]
1223 IStream
* WINAPI
SHOpenRegStream2W(HKEY hKey
, const WCHAR
*subkey
, const WCHAR
*value
, DWORD mode
)
1225 struct shstream
*stream
;
1226 HKEY hStrKey
= NULL
;
1231 TRACE("(%p, %s, %s, %#x)\n", hKey
, debugstr_w(subkey
), debugstr_w(value
), mode
);
1233 if (mode
== STGM_READ
)
1234 ret
= RegOpenKeyExW(hKey
, subkey
, 0, KEY_READ
, &hStrKey
);
1235 else /* in write mode we make sure the subkey exits */
1236 ret
= RegCreateKeyExW(hKey
, subkey
, 0, NULL
, 0, KEY_READ
| KEY_WRITE
, NULL
, &hStrKey
, NULL
);
1238 if (ret
== ERROR_SUCCESS
)
1240 if (mode
== STGM_READ
|| mode
== STGM_READWRITE
)
1242 /* read initial data */
1243 ret
= RegQueryValueExW(hStrKey
, value
, 0, 0, 0, &length
);
1244 if (ret
== ERROR_SUCCESS
&& length
)
1246 buff
= heap_alloc(length
);
1247 RegQueryValueExW(hStrKey
, value
, 0, 0, buff
, &length
);
1252 buff
= heap_alloc(length
);
1254 stream
= shstream_create(®streamvtbl
, buff
, length
);
1258 stream
->u
.mem
.hkey
= hStrKey
;
1259 SHStrDupW(value
, &stream
->u
.mem
.valuename
);
1260 return &stream
->IStream_iface
;
1265 RegCloseKey(hStrKey
);
1270 /*************************************************************************
1271 * SHOpenRegStream2A [SHCORE.@]
1273 IStream
* WINAPI
SHOpenRegStream2A(HKEY hKey
, const char *subkey
, const char *value
, DWORD mode
)
1275 WCHAR
*subkeyW
= NULL
, *valueW
= NULL
;
1278 TRACE("(%p, %s, %s, %#x)\n", hKey
, debugstr_a(subkey
), debugstr_a(value
), mode
);
1280 if (subkey
&& FAILED(SHStrDupA(subkey
, &subkeyW
)))
1282 if (value
&& FAILED(SHStrDupA(value
, &valueW
)))
1284 CoTaskMemFree(subkeyW
);
1288 stream
= SHOpenRegStream2W(hKey
, subkeyW
, valueW
, mode
);
1289 CoTaskMemFree(subkeyW
);
1290 CoTaskMemFree(valueW
);
1294 /*************************************************************************
1295 * SHOpenRegStreamA [SHCORE.@]
1297 IStream
* WINAPI
SHOpenRegStreamA(HKEY hkey
, const char *subkey
, const char *value
, DWORD mode
)
1299 WCHAR
*subkeyW
= NULL
, *valueW
= NULL
;
1302 TRACE("(%p, %s, %s, %#x)\n", hkey
, debugstr_a(subkey
), debugstr_a(value
), mode
);
1304 if (subkey
&& FAILED(SHStrDupA(subkey
, &subkeyW
)))
1306 if (value
&& FAILED(SHStrDupA(value
, &valueW
)))
1308 CoTaskMemFree(subkeyW
);
1312 stream
= SHOpenRegStreamW(hkey
, subkeyW
, valueW
, mode
);
1313 CoTaskMemFree(subkeyW
);
1314 CoTaskMemFree(valueW
);
1318 static ULONG WINAPI
dummystream_AddRef(IStream
*iface
)
1324 static ULONG WINAPI
dummystream_Release(IStream
*iface
)
1330 static HRESULT WINAPI
dummystream_Read(IStream
*iface
, void *buff
, ULONG buff_size
, ULONG
*read_len
)
1338 static const IStreamVtbl dummystreamvtbl
=
1340 shstream_QueryInterface
,
1342 dummystream_Release
,
1350 shstream_LockRegion
,
1351 shstream_UnlockRegion
,
1356 static struct shstream dummyregstream
= { { &dummystreamvtbl
} };
1358 /*************************************************************************
1359 * SHOpenRegStreamW [SHCORE.@]
1361 IStream
* WINAPI
SHOpenRegStreamW(HKEY hkey
, const WCHAR
*subkey
, const WCHAR
*value
, DWORD mode
)
1365 TRACE("(%p, %s, %s, %#x)\n", hkey
, debugstr_w(subkey
), debugstr_w(value
), mode
);
1366 stream
= SHOpenRegStream2W(hkey
, subkey
, value
, mode
);
1367 return stream
? stream
: &dummyregstream
.IStream_iface
;
1372 IUnknown IUnknown_iface
;
1376 static inline struct threadref
*threadref_impl_from_IUnknown(IUnknown
*iface
)
1378 return CONTAINING_RECORD(iface
, struct threadref
, IUnknown_iface
);
1381 static HRESULT WINAPI
threadref_QueryInterface(IUnknown
*iface
, REFIID riid
, void **out
)
1383 struct threadref
*threadref
= threadref_impl_from_IUnknown(iface
);
1385 TRACE("(%p, %s, %p)\n", threadref
, debugstr_guid(riid
), out
);
1390 if (IsEqualGUID(&IID_IUnknown
, riid
))
1393 IUnknown_AddRef(iface
);
1398 WARN("Interface %s not supported.\n", debugstr_guid(riid
));
1399 return E_NOINTERFACE
;
1402 static ULONG WINAPI
threadref_AddRef(IUnknown
*iface
)
1404 struct threadref
*threadref
= threadref_impl_from_IUnknown(iface
);
1405 LONG refcount
= InterlockedIncrement(threadref
->refcount
);
1407 TRACE("(%p, %d)\n", threadref
, refcount
);
1412 static ULONG WINAPI
threadref_Release(IUnknown
*iface
)
1414 struct threadref
*threadref
= threadref_impl_from_IUnknown(iface
);
1415 LONG refcount
= InterlockedDecrement(threadref
->refcount
);
1417 TRACE("(%p, %d)\n", threadref
, refcount
);
1420 heap_free(threadref
);
1425 static const IUnknownVtbl threadrefvtbl
=
1427 threadref_QueryInterface
,
1432 /*************************************************************************
1433 * SHCreateThreadRef [SHCORE.@]
1435 HRESULT WINAPI
SHCreateThreadRef(LONG
*refcount
, IUnknown
**out
)
1437 struct threadref
*threadref
;
1439 TRACE("(%p, %p)\n", refcount
, out
);
1441 if (!refcount
|| !out
)
1442 return E_INVALIDARG
;
1446 threadref
= heap_alloc(sizeof(*threadref
));
1448 return E_OUTOFMEMORY
;
1449 threadref
->IUnknown_iface
.lpVtbl
= &threadrefvtbl
;
1450 threadref
->refcount
= refcount
;
1453 *out
= &threadref
->IUnknown_iface
;
1455 TRACE("Created %p.\n", threadref
);
1459 /*************************************************************************
1460 * SHGetThreadRef [SHCORE.@]
1462 HRESULT WINAPI
SHGetThreadRef(IUnknown
**out
)
1464 TRACE("(%p)\n", out
);
1466 if (shcore_tls
== TLS_OUT_OF_INDEXES
)
1467 return E_NOINTERFACE
;
1469 *out
= TlsGetValue(shcore_tls
);
1471 return E_NOINTERFACE
;
1473 IUnknown_AddRef(*out
);
1477 /*************************************************************************
1478 * SHSetThreadRef [SHCORE.@]
1480 HRESULT WINAPI
SHSetThreadRef(IUnknown
*obj
)
1482 TRACE("(%p)\n", obj
);
1484 if (shcore_tls
== TLS_OUT_OF_INDEXES
)
1485 return E_NOINTERFACE
;
1487 TlsSetValue(shcore_tls
, obj
);
1491 /*************************************************************************
1492 * SHReleaseThreadRef [SHCORE.@]
1494 HRESULT WINAPI
SHReleaseThreadRef(void)
1496 FIXME("() - stub!\n");
1500 /*************************************************************************
1501 * GetProcessReference [SHCORE.@]
1503 HRESULT WINAPI
GetProcessReference(IUnknown
**obj
)
1505 TRACE("(%p)\n", obj
);
1513 IUnknown_AddRef(*obj
);
1518 /*************************************************************************
1519 * SetProcessReference [SHCORE.@]
1521 void WINAPI
SetProcessReference(IUnknown
*obj
)
1523 TRACE("(%p)\n", obj
);
1530 LPTHREAD_START_ROUTINE thread_proc
;
1531 LPTHREAD_START_ROUTINE callback
;
1535 IUnknown
*thread_ref
;
1536 IUnknown
*process_ref
;
1539 static DWORD WINAPI
shcore_thread_wrapper(void *data
)
1541 struct thread_data thread_data
;
1542 HRESULT hr
= E_FAIL
;
1545 TRACE("(%p)\n", data
);
1547 /* We are now executing in the context of the newly created thread.
1548 * So we copy the data passed to us (it is on the stack of the function
1549 * that called us, which is waiting for us to signal an event before
1551 thread_data
= *(struct thread_data
*)data
;
1553 if (thread_data
.flags
& CTF_COINIT
)
1555 hr
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
| COINIT_DISABLE_OLE1DDE
);
1557 hr
= CoInitializeEx(NULL
, COINIT_DISABLE_OLE1DDE
);
1560 if (thread_data
.callback
)
1561 thread_data
.callback(thread_data
.data
);
1563 /* Signal the thread that created us; it can return now. */
1564 SetEvent(thread_data
.hEvent
);
1566 /* Execute the callers start code. */
1567 retval
= thread_data
.thread_proc(thread_data
.data
);
1569 /* Release thread and process references. */
1570 if (thread_data
.thread_ref
)
1571 IUnknown_Release(thread_data
.thread_ref
);
1573 if (thread_data
.process_ref
)
1574 IUnknown_Release(thread_data
.process_ref
);
1582 /*************************************************************************
1583 * SHCreateThread [SHCORE.@]
1585 BOOL WINAPI
SHCreateThread(LPTHREAD_START_ROUTINE thread_proc
, void *data
, DWORD flags
, LPTHREAD_START_ROUTINE callback
)
1587 struct thread_data thread_data
;
1588 BOOL called
= FALSE
;
1590 TRACE("(%p, %p, %#x, %p)\n", thread_proc
, data
, flags
, callback
);
1592 thread_data
.thread_proc
= thread_proc
;
1593 thread_data
.callback
= callback
;
1594 thread_data
.data
= data
;
1595 thread_data
.flags
= flags
;
1596 thread_data
.hEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1598 if (flags
& CTF_THREAD_REF
)
1599 SHGetThreadRef(&thread_data
.thread_ref
);
1601 thread_data
.thread_ref
= NULL
;
1603 if (flags
& CTF_PROCESS_REF
)
1604 GetProcessReference(&thread_data
.process_ref
);
1606 thread_data
.process_ref
= NULL
;
1608 /* Create the thread */
1609 if (thread_data
.hEvent
)
1614 hThread
= CreateThread(NULL
, 0, shcore_thread_wrapper
, &thread_data
, 0, &retval
);
1617 /* Wait for the thread to signal us to continue */
1618 WaitForSingleObject(thread_data
.hEvent
, INFINITE
);
1619 CloseHandle(hThread
);
1622 CloseHandle(thread_data
.hEvent
);
1627 if (!thread_data
.callback
&& flags
& CTF_INSIST
)
1629 /* Couldn't call, call synchronously */
1630 thread_data
.thread_proc(data
);
1635 if (thread_data
.thread_ref
)
1636 IUnknown_Release(thread_data
.thread_ref
);
1638 if (thread_data
.process_ref
)
1639 IUnknown_Release(thread_data
.process_ref
);
1646 /*************************************************************************
1647 * SHStrDupW [SHCORE.@]
1649 HRESULT WINAPI
SHStrDupW(const WCHAR
*src
, WCHAR
**dest
)
1653 TRACE("(%s, %p)\n", debugstr_w(src
), dest
);
1658 return E_INVALIDARG
;
1660 len
= (lstrlenW(src
) + 1) * sizeof(WCHAR
);
1661 *dest
= CoTaskMemAlloc(len
);
1663 return E_OUTOFMEMORY
;
1665 memcpy(*dest
, src
, len
);
1670 /*************************************************************************
1671 * SHStrDupA [SHCORE.@]
1673 HRESULT WINAPI
SHStrDupA(const char *src
, WCHAR
**dest
)
1680 return E_INVALIDARG
;
1682 len
= MultiByteToWideChar(CP_ACP
, 0, src
, -1, NULL
, 0);
1683 *dest
= CoTaskMemAlloc(len
* sizeof(WCHAR
));
1685 return E_OUTOFMEMORY
;
1687 MultiByteToWideChar(CP_ACP
, 0, src
, -1, *dest
, len
);
1692 /*************************************************************************
1693 * SHAnsiToAnsi [SHCORE.@]
1695 DWORD WINAPI
SHAnsiToAnsi(const char *src
, char *dest
, int dest_len
)
1699 TRACE("(%s, %p, %d)\n", debugstr_a(src
), dest
, dest_len
);
1701 if (!src
|| !dest
|| dest_len
<= 0)
1704 lstrcpynA(dest
, src
, dest_len
);
1707 return src
[ret
] ? 0 : ret
+ 1;
1710 /*************************************************************************
1711 * SHUnicodeToAnsi [SHCORE.@]
1713 DWORD WINAPI
SHUnicodeToAnsi(const WCHAR
*src
, char *dest
, int dest_len
)
1717 TRACE("(%s, %p, %d)\n", debugstr_w(src
), dest
, dest_len
);
1719 if (!dest
|| !dest_len
)
1724 ret
= WideCharToMultiByte(CP_ACP
, 0, src
, -1, dest
, dest_len
, NULL
, NULL
);
1727 dest
[dest_len
- 1] = 0;
1737 /*************************************************************************
1738 * SHUnicodeToUnicode [SHCORE.@]
1740 DWORD WINAPI
SHUnicodeToUnicode(const WCHAR
*src
, WCHAR
*dest
, int dest_len
)
1744 TRACE("(%s, %p, %d)\n", debugstr_w(src
), dest
, dest_len
);
1746 if (!src
|| !dest
|| dest_len
<= 0)
1749 lstrcpynW(dest
, src
, dest_len
);
1750 ret
= lstrlenW(dest
);
1752 return src
[ret
] ? 0 : ret
+ 1;
1755 /*************************************************************************
1756 * SHAnsiToUnicode [SHCORE.@]
1758 DWORD WINAPI
SHAnsiToUnicode(const char *src
, WCHAR
*dest
, int dest_len
)
1762 TRACE("(%s, %p, %d)\n", debugstr_a(src
), dest
, dest_len
);
1764 if (!dest
|| !dest_len
)
1769 ret
= MultiByteToWideChar(CP_ACP
, 0, src
, -1, dest
, dest_len
);
1772 dest
[dest_len
- 1] = 0;
1782 /*************************************************************************
1783 * SHRegDuplicateHKey [SHCORE.@]
1785 HKEY WINAPI
SHRegDuplicateHKey(HKEY hKey
)
1789 RegOpenKeyExW(hKey
, 0, 0, MAXIMUM_ALLOWED
, &newKey
);
1790 TRACE("new key is %p\n", newKey
);
1794 /*************************************************************************
1795 * SHDeleteEmptyKeyW [SHCORE.@]
1797 DWORD WINAPI
SHDeleteEmptyKeyW(HKEY hkey
, const WCHAR
*subkey
)
1799 DWORD ret
, count
= 0;
1802 TRACE("(%p, %s)\n", hkey
, debugstr_w(subkey
));
1804 ret
= RegOpenKeyExW(hkey
, subkey
, 0, KEY_READ
, &hsubkey
);
1807 ret
= RegQueryInfoKeyW(hsubkey
, NULL
, NULL
, NULL
, &count
,
1808 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1809 RegCloseKey(hsubkey
);
1813 ret
= ERROR_KEY_HAS_CHILDREN
;
1815 ret
= RegDeleteKeyW(hkey
, subkey
);
1822 /*************************************************************************
1823 * SHDeleteEmptyKeyA [SHCORE.@]
1825 DWORD WINAPI
SHDeleteEmptyKeyA(HKEY hkey
, const char *subkey
)
1827 WCHAR
*subkeyW
= NULL
;
1830 TRACE("(%p, %s)\n", hkey
, debugstr_a(subkey
));
1832 if (subkey
&& FAILED(SHStrDupA(subkey
, &subkeyW
)))
1833 return ERROR_OUTOFMEMORY
;
1835 ret
= SHDeleteEmptyKeyW(hkey
, subkeyW
);
1836 CoTaskMemFree(subkeyW
);
1840 /*************************************************************************
1841 * SHDeleteKeyW [SHCORE.@]
1843 DWORD WINAPI
SHDeleteKeyW(HKEY hkey
, const WCHAR
*subkey
)
1845 TRACE("(%p, %s)\n", hkey
, debugstr_w(subkey
));
1847 return RegDeleteTreeW(hkey
, subkey
);
1850 /*************************************************************************
1851 * SHDeleteKeyA [SHCORE.@]
1853 DWORD WINAPI
SHDeleteKeyA(HKEY hkey
, const char *subkey
)
1855 TRACE("(%p, %s)\n", hkey
, debugstr_a(subkey
));
1857 return RegDeleteTreeA(hkey
, subkey
);
1860 /*************************************************************************
1861 * SHDeleteValueW [SHCORE.@]
1863 DWORD WINAPI
SHDeleteValueW(HKEY hkey
, const WCHAR
*subkey
, const WCHAR
*value
)
1868 TRACE("(%p, %s, %s)\n", hkey
, debugstr_w(subkey
), debugstr_w(value
));
1870 ret
= RegOpenKeyExW(hkey
, subkey
, 0, KEY_SET_VALUE
, &hsubkey
);
1873 ret
= RegDeleteValueW(hsubkey
, value
);
1874 RegCloseKey(hsubkey
);
1880 /*************************************************************************
1881 * SHDeleteValueA [SHCORE.@]
1883 DWORD WINAPI
SHDeleteValueA(HKEY hkey
, const char *subkey
, const char *value
)
1885 WCHAR
*subkeyW
= NULL
, *valueW
= NULL
;
1888 TRACE("(%p, %s, %s)\n", hkey
, debugstr_a(subkey
), debugstr_a(value
));
1890 if (subkey
&& FAILED(SHStrDupA(subkey
, &subkeyW
)))
1891 return ERROR_OUTOFMEMORY
;
1892 if (value
&& FAILED(SHStrDupA(value
, &valueW
)))
1894 CoTaskMemFree(subkeyW
);
1895 return ERROR_OUTOFMEMORY
;
1898 ret
= SHDeleteValueW(hkey
, subkeyW
, valueW
);
1899 CoTaskMemFree(subkeyW
);
1900 CoTaskMemFree(valueW
);
1904 /*************************************************************************
1905 * SHCopyKeyA [SHCORE.@]
1907 DWORD WINAPI
SHCopyKeyA(HKEY hkey_src
, const char *subkey
, HKEY hkey_dst
, DWORD reserved
)
1909 WCHAR
*subkeyW
= NULL
;
1912 TRACE("(%p, %s, %p, %d)\n", hkey_src
, debugstr_a(subkey
), hkey_dst
, reserved
);
1914 if (subkey
&& FAILED(SHStrDupA(subkey
, &subkeyW
)))
1917 ret
= SHCopyKeyW(hkey_src
, subkeyW
, hkey_dst
, reserved
);
1918 CoTaskMemFree(subkeyW
);
1922 /*************************************************************************
1923 * SHCopyKeyW [SHCORE.@]
1925 DWORD WINAPI
SHCopyKeyW(HKEY hkey_src
, const WCHAR
*subkey
, HKEY hkey_dst
, DWORD reserved
)
1927 DWORD key_count
= 0, value_count
= 0, max_key_len
= 0;
1928 WCHAR name
[MAX_PATH
], *ptr_name
= name
;
1929 BYTE buff
[1024], *ptr
= buff
;
1930 DWORD max_data_len
= 0, i
;
1933 TRACE("(%p, %s, %p, %d)\n", hkey_src
, debugstr_w(subkey
), hkey_dst
, reserved
);
1935 if (!hkey_dst
|| !hkey_src
)
1936 return ERROR_INVALID_PARAMETER
;
1939 ret
= RegOpenKeyExW(hkey_src
, subkey
, 0, KEY_ALL_ACCESS
, &hkey_src
);
1942 hkey_src
= NULL
; /* Don't close this key since we didn't open it */
1945 DWORD max_value_len
;
1947 ret
= RegQueryInfoKeyW(hkey_src
, NULL
, NULL
, NULL
, &key_count
, &max_key_len
,
1948 NULL
, &value_count
, &max_value_len
, &max_data_len
, NULL
, NULL
);
1951 /* Get max size for key/value names */
1952 max_key_len
= max(max_key_len
, max_value_len
);
1954 if (max_key_len
++ > MAX_PATH
- 1)
1955 ptr_name
= heap_alloc(max_key_len
* sizeof(WCHAR
));
1957 if (max_data_len
> sizeof(buff
))
1958 ptr
= heap_alloc(max_data_len
);
1960 if (!ptr_name
|| !ptr
)
1961 ret
= ERROR_NOT_ENOUGH_MEMORY
;
1965 for (i
= 0; i
< key_count
&& !ret
; i
++)
1967 HKEY hsubkey_src
, hsubkey_dst
;
1968 DWORD length
= max_key_len
;
1970 ret
= RegEnumKeyExW(hkey_src
, i
, ptr_name
, &length
, NULL
, NULL
, NULL
, NULL
);
1973 ret
= RegOpenKeyExW(hkey_src
, ptr_name
, 0, KEY_READ
, &hsubkey_src
);
1976 /* Create destination sub key */
1977 ret
= RegCreateKeyW(hkey_dst
, ptr_name
, &hsubkey_dst
);
1980 /* Recursively copy keys and values from the sub key */
1981 ret
= SHCopyKeyW(hsubkey_src
, NULL
, hsubkey_dst
, 0);
1982 RegCloseKey(hsubkey_dst
);
1985 RegCloseKey(hsubkey_src
);
1989 /* Copy all the values in this key */
1990 for (i
= 0; i
< value_count
&& !ret
; i
++)
1992 DWORD length
= max_key_len
, type
, data_len
= max_data_len
;
1994 ret
= RegEnumValueW(hkey_src
, i
, ptr_name
, &length
, NULL
, &type
, ptr
, &data_len
);
1996 ret
= SHSetValueW(hkey_dst
, NULL
, ptr_name
, type
, ptr
, data_len
);
2000 /* Free buffers if allocated */
2001 if (ptr_name
!= name
)
2002 heap_free(ptr_name
);
2006 if (subkey
&& hkey_src
)
2007 RegCloseKey(hkey_src
);
2013 /*************************************************************************
2014 * SHEnumKeyExA [SHCORE.@]
2016 LONG WINAPI
SHEnumKeyExA(HKEY hkey
, DWORD index
, char *subkey
, DWORD
*length
)
2018 TRACE("(%p, %d, %s, %p)\n", hkey
, index
, debugstr_a(subkey
), length
);
2020 return RegEnumKeyExA(hkey
, index
, subkey
, length
, NULL
, NULL
, NULL
, NULL
);
2023 /*************************************************************************
2024 * SHEnumKeyExW [SHCORE.@]
2026 LONG WINAPI
SHEnumKeyExW(HKEY hkey
, DWORD index
, WCHAR
*subkey
, DWORD
*length
)
2028 TRACE("(%p, %d, %s, %p)\n", hkey
, index
, debugstr_w(subkey
), length
);
2030 return RegEnumKeyExW(hkey
, index
, subkey
, length
, NULL
, NULL
, NULL
, NULL
);
2033 /*************************************************************************
2034 * SHEnumValueA [SHCORE.@]
2036 LONG WINAPI
SHEnumValueA(HKEY hkey
, DWORD index
, char *value
, DWORD
*length
, DWORD
*type
,
2037 void *data
, DWORD
*data_len
)
2039 TRACE("(%p, %d, %s, %p, %p, %p, %p)\n", hkey
, index
, debugstr_a(value
), length
, type
, data
, data_len
);
2041 return RegEnumValueA(hkey
, index
, value
, length
, NULL
, type
, data
, data_len
);
2044 /*************************************************************************
2045 * SHEnumValueW [SHCORE.@]
2047 LONG WINAPI
SHEnumValueW(HKEY hkey
, DWORD index
, WCHAR
*value
, DWORD
*length
, DWORD
*type
,
2048 void *data
, DWORD
*data_len
)
2050 TRACE("(%p, %d, %s, %p, %p, %p, %p)\n", hkey
, index
, debugstr_w(value
), length
, type
, data
, data_len
);
2052 return RegEnumValueW(hkey
, index
, value
, length
, NULL
, type
, data
, data_len
);
2055 /*************************************************************************
2056 * SHQueryValueExW [SHCORE.@]
2058 DWORD WINAPI
SHQueryValueExW(HKEY hkey
, const WCHAR
*name
, DWORD
*reserved
, DWORD
*type
,
2059 void *buff
, DWORD
*buff_len
)
2061 DWORD ret
, value_type
, data_len
= 0;
2063 TRACE("(%p, %s, %p, %p, %p, %p)\n", hkey
, debugstr_w(name
), reserved
, type
, buff
, buff_len
);
2066 data_len
= *buff_len
;
2068 ret
= RegQueryValueExW(hkey
, name
, reserved
, &value_type
, buff
, &data_len
);
2069 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_MORE_DATA
)
2072 if (buff_len
&& value_type
== REG_EXPAND_SZ
)
2077 if (!buff
|| ret
== ERROR_MORE_DATA
)
2080 value
= heap_alloc(length
);
2081 RegQueryValueExW(hkey
, name
, reserved
, NULL
, (BYTE
*)value
, &length
);
2082 length
= ExpandEnvironmentStringsW(value
, NULL
, 0);
2086 length
= (lstrlenW(buff
) + 1) * sizeof(WCHAR
);
2087 value
= heap_alloc(length
);
2088 memcpy(value
, buff
, length
);
2089 length
= ExpandEnvironmentStringsW(value
, buff
, *buff_len
/ sizeof(WCHAR
));
2090 if (length
> *buff_len
) ret
= ERROR_MORE_DATA
;
2092 data_len
= max(data_len
, length
);
2097 *type
= value_type
== REG_EXPAND_SZ
? REG_SZ
: value_type
;
2099 *buff_len
= data_len
;
2103 /*************************************************************************
2104 * SHQueryValueExA [SHCORE.@]
2106 DWORD WINAPI
SHQueryValueExA(HKEY hkey
, const char *name
, DWORD
*reserved
, DWORD
*type
,
2107 void *buff
, DWORD
*buff_len
)
2109 DWORD ret
, value_type
, data_len
= 0;
2111 TRACE("(%p, %s, %p, %p, %p, %p)\n", hkey
, debugstr_a(name
), reserved
, type
, buff
, buff_len
);
2114 data_len
= *buff_len
;
2116 ret
= RegQueryValueExA(hkey
, name
, reserved
, &value_type
, buff
, &data_len
);
2117 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_MORE_DATA
)
2120 if (buff_len
&& value_type
== REG_EXPAND_SZ
)
2125 if (!buff
|| ret
== ERROR_MORE_DATA
)
2128 value
= heap_alloc(length
);
2129 RegQueryValueExA(hkey
, name
, reserved
, NULL
, (BYTE
*)value
, &length
);
2130 length
= ExpandEnvironmentStringsA(value
, NULL
, 0);
2134 length
= strlen(buff
) + 1;
2135 value
= heap_alloc(length
);
2136 memcpy(value
, buff
, length
);
2137 length
= ExpandEnvironmentStringsA(value
, buff
, *buff_len
);
2138 if (length
> *buff_len
) ret
= ERROR_MORE_DATA
;
2140 data_len
= max(data_len
, length
);
2145 *type
= value_type
== REG_EXPAND_SZ
? REG_SZ
: value_type
;
2147 *buff_len
= data_len
;
2151 /*************************************************************************
2152 * SHGetValueA [SHCORE.@]
2154 DWORD WINAPI
SHGetValueA(HKEY hkey
, const char *subkey
, const char *value
,
2155 DWORD
*type
, void *data
, DWORD
*data_len
)
2160 TRACE("(%p, %s, %s, %p, %p, %p)\n", hkey
, debugstr_a(subkey
), debugstr_a(value
),
2161 type
, data
, data_len
);
2164 ret
= RegOpenKeyExA(hkey
, subkey
, 0, KEY_QUERY_VALUE
, &hsubkey
);
2168 ret
= SHQueryValueExA(hsubkey
? hsubkey
: hkey
, value
, 0, type
, data
, data_len
);
2170 RegCloseKey(hsubkey
);
2176 /*************************************************************************
2177 * SHGetValueW [SHCORE.@]
2179 DWORD WINAPI
SHGetValueW(HKEY hkey
, const WCHAR
*subkey
, const WCHAR
*value
,
2180 DWORD
*type
, void *data
, DWORD
*data_len
)
2185 TRACE("(%p, %s, %s, %p, %p, %p)\n", hkey
, debugstr_w(subkey
), debugstr_w(value
),
2186 type
, data
, data_len
);
2189 ret
= RegOpenKeyExW(hkey
, subkey
, 0, KEY_QUERY_VALUE
, &hsubkey
);
2193 ret
= SHQueryValueExW(hsubkey
? hsubkey
: hkey
, value
, 0, type
, data
, data_len
);
2195 RegCloseKey(hsubkey
);
2201 /*************************************************************************
2202 * SHRegGetIntW [SHCORE.280]
2204 int WINAPI
SHRegGetIntW(HKEY hkey
, const WCHAR
*value
, int default_value
)
2209 TRACE("(%p, %s, %d)\n", hkey
, debugstr_w(value
), default_value
);
2212 buff_len
= sizeof(buff
);
2213 if (SHQueryValueExW(hkey
, value
, 0, 0, buff
, &buff_len
))
2214 return default_value
;
2216 if (*buff
>= '0' && *buff
<= '9')
2217 return wcstol(buff
, NULL
, 10);
2219 return default_value
;
2222 /*************************************************************************
2223 * SHRegGetPathA [SHCORE.@]
2225 DWORD WINAPI
SHRegGetPathA(HKEY hkey
, const char *subkey
, const char *value
, char *path
, DWORD flags
)
2227 DWORD length
= MAX_PATH
;
2229 TRACE("(%p, %s, %s, %p, %#x)\n", hkey
, debugstr_a(subkey
), debugstr_a(value
), path
, flags
);
2231 return SHGetValueA(hkey
, subkey
, value
, 0, path
, &length
);
2234 /*************************************************************************
2235 * SHRegGetPathW [SHCORE.@]
2237 DWORD WINAPI
SHRegGetPathW(HKEY hkey
, const WCHAR
*subkey
, const WCHAR
*value
, WCHAR
*path
, DWORD flags
)
2239 DWORD length
= MAX_PATH
;
2241 TRACE("(%p, %s, %s, %p, %d)\n", hkey
, debugstr_w(subkey
), debugstr_w(value
), path
, flags
);
2243 return SHGetValueW(hkey
, subkey
, value
, 0, path
, &length
);
2246 /*************************************************************************
2247 * SHSetValueW [SHCORE.@]
2249 DWORD WINAPI
SHSetValueW(HKEY hkey
, const WCHAR
*subkey
, const WCHAR
*value
, DWORD type
,
2250 const void *data
, DWORD data_len
)
2252 DWORD ret
= ERROR_SUCCESS
, dummy
;
2255 TRACE("(%p, %s, %s, %d, %p, %d)\n", hkey
, debugstr_w(subkey
), debugstr_w(value
),
2256 type
, data
, data_len
);
2258 if (subkey
&& *subkey
)
2259 ret
= RegCreateKeyExW(hkey
, subkey
, 0, NULL
, 0, KEY_SET_VALUE
, NULL
, &hsubkey
, &dummy
);
2265 ret
= RegSetValueExW(hsubkey
, value
, 0, type
, data
, data_len
);
2266 if (hsubkey
!= hkey
)
2267 RegCloseKey(hsubkey
);
2273 /*************************************************************************
2274 * SHSetValueA [SHCORE.@]
2276 DWORD WINAPI
SHSetValueA(HKEY hkey
, const char *subkey
, const char *value
,
2277 DWORD type
, const void *data
, DWORD data_len
)
2279 DWORD ret
= ERROR_SUCCESS
, dummy
;
2282 TRACE("(%p, %s, %s, %d, %p, %d)\n", hkey
, debugstr_a(subkey
), debugstr_a(value
),
2283 type
, data
, data_len
);
2285 if (subkey
&& *subkey
)
2286 ret
= RegCreateKeyExA(hkey
, subkey
, 0, NULL
, 0, KEY_SET_VALUE
, NULL
, &hsubkey
, &dummy
);
2292 ret
= RegSetValueExA(hsubkey
, value
, 0, type
, data
, data_len
);
2293 if (hsubkey
!= hkey
)
2294 RegCloseKey(hsubkey
);
2300 /*************************************************************************
2301 * SHRegSetPathA [SHCORE.@]
2303 DWORD WINAPI
SHRegSetPathA(HKEY hkey
, const char *subkey
, const char *value
, const char *path
, DWORD flags
)
2305 FIXME("(%p, %s, %s, %s, %#x) - semi-stub\n", hkey
, debugstr_a(subkey
),
2306 debugstr_a(value
), debugstr_a(path
), flags
);
2308 /* FIXME: PathUnExpandEnvStringsA() */
2310 return SHSetValueA(hkey
, subkey
, value
, REG_SZ
, path
, lstrlenA(path
));
2313 /*************************************************************************
2314 * SHRegSetPathW [SHCORE.@]
2316 DWORD WINAPI
SHRegSetPathW(HKEY hkey
, const WCHAR
*subkey
, const WCHAR
*value
, const WCHAR
*path
, DWORD flags
)
2318 FIXME("(%p, %s, %s, %s, %#x) - semi-stub\n", hkey
, debugstr_w(subkey
),
2319 debugstr_w(value
), debugstr_w(path
), flags
);
2321 /* FIXME: PathUnExpandEnvStringsW(); */
2323 return SHSetValueW(hkey
, subkey
, value
, REG_SZ
, path
, lstrlenW(path
));
2326 /*************************************************************************
2327 * SHQueryInfoKeyA [SHCORE.@]
2329 LONG WINAPI
SHQueryInfoKeyA(HKEY hkey
, DWORD
*subkeys
, DWORD
*subkey_max
, DWORD
*values
, DWORD
*value_max
)
2331 TRACE("(%p, %p, %p, %p, %p)\n", hkey
, subkeys
, subkey_max
, values
, value_max
);
2333 return RegQueryInfoKeyA(hkey
, NULL
, NULL
, NULL
, subkeys
, subkey_max
, NULL
, values
, value_max
, NULL
, NULL
, NULL
);
2336 /*************************************************************************
2337 * SHQueryInfoKeyW [SHCORE.@]
2339 LONG WINAPI
SHQueryInfoKeyW(HKEY hkey
, DWORD
*subkeys
, DWORD
*subkey_max
, DWORD
*values
, DWORD
*value_max
)
2341 TRACE("(%p, %p, %p, %p, %p)\n", hkey
, subkeys
, subkey_max
, values
, value_max
);
2343 return RegQueryInfoKeyW(hkey
, NULL
, NULL
, NULL
, subkeys
, subkey_max
, NULL
, values
, value_max
, NULL
, NULL
, NULL
);
2346 /*************************************************************************
2349 BOOL WINAPI
IsOS(DWORD feature
)
2351 DWORD platform
, majorv
, minorv
;
2352 OSVERSIONINFOA osvi
;
2354 osvi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFOA
);
2355 if (!GetVersionExA(&osvi
))
2358 majorv
= osvi
.dwMajorVersion
;
2359 minorv
= osvi
.dwMinorVersion
;
2360 platform
= osvi
.dwPlatformId
;
2362 #define ISOS_RETURN(x) \
2363 TRACE("(0x%x) ret=%d\n",feature,(x)); \
2367 case OS_WIN32SORGREATER
:
2368 ISOS_RETURN(platform
== VER_PLATFORM_WIN32s
2369 || platform
== VER_PLATFORM_WIN32_WINDOWS
);
2371 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
);
2372 case OS_WIN95ORGREATER
:
2373 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_WINDOWS
);
2374 case OS_NT4ORGREATER
:
2375 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& majorv
>= 4);
2376 case OS_WIN2000ORGREATER_ALT
:
2377 case OS_WIN2000ORGREATER
:
2378 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& majorv
>= 5);
2379 case OS_WIN98ORGREATER
:
2380 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_WINDOWS
&& minorv
>= 10);
2382 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_WINDOWS
&& minorv
== 10);
2384 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& majorv
>= 5);
2385 case OS_WIN2000SERVER
:
2386 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& (minorv
== 0 || minorv
== 1));
2387 case OS_WIN2000ADVSERVER
:
2388 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& (minorv
== 0 || minorv
== 1));
2389 case OS_WIN2000DATACENTER
:
2390 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& (minorv
== 0 || minorv
== 1));
2391 case OS_WIN2000TERMINAL
:
2392 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& (minorv
== 0 || minorv
== 1));
2394 FIXME("(OS_EMBEDDED) What should we return here?\n");
2396 case OS_TERMINALCLIENT
:
2397 FIXME("(OS_TERMINALCLIENT) What should we return here?\n");
2399 case OS_TERMINALREMOTEADMIN
:
2400 FIXME("(OS_TERMINALREMOTEADMIN) What should we return here?\n");
2403 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_WINDOWS
&& minorv
== 0);
2404 case OS_MEORGREATER
:
2405 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_WINDOWS
&& minorv
>= 90);
2406 case OS_XPORGREATER
:
2407 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& majorv
>= 5 && minorv
>= 1);
2409 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& majorv
>= 5 && minorv
>= 1);
2410 case OS_PROFESSIONAL
:
2411 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
);
2413 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
);
2415 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& majorv
>= 5);
2417 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
);
2418 case OS_TERMINALSERVER
:
2419 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
);
2420 case OS_PERSONALTERMINALSERVER
:
2421 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& minorv
>= 1 && majorv
>= 5);
2422 case OS_FASTUSERSWITCHING
:
2423 FIXME("(OS_FASTUSERSWITCHING) What should we return here?\n");
2425 case OS_WELCOMELOGONUI
:
2426 FIXME("(OS_WELCOMELOGONUI) What should we return here?\n");
2428 case OS_DOMAINMEMBER
:
2429 FIXME("(OS_DOMAINMEMBER) What should we return here?\n");
2432 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
);
2436 IsWow64Process(GetCurrentProcess(), &is_wow64
);
2440 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
);
2441 case OS_SMALLBUSINESSSERVER
:
2442 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
);
2444 FIXME("(OS_TABLETPC) What should we return here?\n");
2446 case OS_SERVERADMINUI
:
2447 FIXME("(OS_SERVERADMINUI) What should we return here?\n");
2449 case OS_MEDIACENTER
:
2450 FIXME("(OS_MEDIACENTER) What should we return here?\n");
2453 FIXME("(OS_APPLIANCE) What should we return here?\n");
2455 case 0x25: /*OS_VISTAORGREATER*/
2456 ISOS_RETURN(platform
== VER_PLATFORM_WIN32_NT
&& majorv
>= 6);
2461 WARN("(0x%x) unknown parameter\n", feature
);