|
| 1 | +// Copyright (c) Files Community |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using Windows.Win32.Foundation; |
| 6 | +using Windows.Win32.System.Com.StructuredStorage; |
| 7 | +using Windows.Win32.System.Variant; |
| 8 | +using Windows.Win32.UI.WindowsAndMessaging; |
| 9 | + |
| 10 | +namespace Windows.Win32 |
| 11 | +{ |
| 12 | + public unsafe static partial class PInvoke |
| 13 | + { |
| 14 | + public static HRESULT InitPropVariantFromString(char* psz, PROPVARIANT* ppropvar) |
| 15 | + { |
| 16 | + HRESULT hr = psz != null ? HRESULT.S_OK : HRESULT.E_INVALIDARG; |
| 17 | + |
| 18 | + if (SUCCEEDED(hr)) |
| 19 | + { |
| 20 | + nuint byteCount = (nuint)((MemoryMarshal.CreateReadOnlySpanFromNullTerminated(psz).Length + 1) * 2); |
| 21 | + |
| 22 | + ((ppropvar)->Anonymous.Anonymous.Anonymous.pwszVal) = (char*)(PInvoke.CoTaskMemAlloc(byteCount)); |
| 23 | + hr = ((ppropvar)->Anonymous.Anonymous.Anonymous.pwszVal) != null ? HRESULT.S_OK : HRESULT.E_OUTOFMEMORY; |
| 24 | + if (SUCCEEDED(hr)) |
| 25 | + { |
| 26 | + NativeMemory.Copy(psz, ((ppropvar)->Anonymous.Anonymous.Anonymous.pwszVal), unchecked(byteCount)); |
| 27 | + ((ppropvar)->Anonymous.Anonymous.vt) = VARENUM.VT_LPWSTR; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + if (FAILED(hr)) |
| 32 | + { |
| 33 | + PInvoke.PropVariantInit(ppropvar); |
| 34 | + } |
| 35 | + |
| 36 | + return hr; |
| 37 | + } |
| 38 | + |
| 39 | + public static void PropVariantInit(PROPVARIANT* pvar) |
| 40 | + { |
| 41 | + NativeMemory.Fill(pvar, (uint)(sizeof(PROPVARIANT)), 0); |
| 42 | + } |
| 43 | + |
| 44 | + public static unsafe nint SetWindowLongPtr(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex, nint dwNewLong) |
| 45 | + { |
| 46 | + // NOTE: |
| 47 | + // Since CsWin32 generates SetWindowLong only on x86, and SetWindowLongPtr only on x64, |
| 48 | + // we need to manually define both functions here. |
| 49 | + // For more info, visit https://github.com/microsoft/CsWin32/issues/882 |
| 50 | + return sizeof(nint) is 4 |
| 51 | + ? _SetWindowLong(hWnd, (int)nIndex, (int)dwNewLong) |
| 52 | + : _SetWindowLongPtr(hWnd, (int)nIndex, dwNewLong); |
| 53 | + |
| 54 | + [DllImport("User32", EntryPoint = "SetWindowLongW", ExactSpelling = true)] |
| 55 | + static extern int _SetWindowLong(HWND hWnd, int nIndex, int dwNewLong); |
| 56 | + |
| 57 | + [DllImport("User32", EntryPoint = "SetWindowLongPtrW", ExactSpelling = true)] |
| 58 | + static extern nint _SetWindowLongPtr(HWND hWnd, int nIndex, nint dwNewLong); |
| 59 | + } |
| 60 | + |
| 61 | + [LibraryImport("Shell32.dll", EntryPoint = "SHUpdateRecycleBinIcon")] |
| 62 | + public static partial void SHUpdateRecycleBinIcon(); |
| 63 | + } |
| 64 | +} |
0 commit comments