diff --git a/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CertAddCertificateLinkToStore.cs b/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CertAddCertificateLinkToStore.cs index 0d440dce6f022d..0c09d360066b9d 100644 --- a/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CertAddCertificateLinkToStore.cs +++ b/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CertAddCertificateLinkToStore.cs @@ -10,7 +10,7 @@ internal static partial class Crypt32 { internal const uint CERT_STORE_ADD_ALWAYS = 4; - [DllImport(Interop.Libraries.Crypt32, CharSet = CharSet.Unicode, SetLastError = true)] - internal static extern bool CertAddCertificateLinkToStore(SafeCertStoreHandle hCertStore, SafeCertContextHandle pCertContext, uint dwAddDisposition, [In, Out] SafeCertContextHandle ppStoreContext); + [GeneratedDllImport(Interop.Libraries.Crypt32, SetLastError = true)] + internal static partial bool CertAddCertificateLinkToStore(SafeCertStoreHandle hCertStore, SafeCertContextHandle pCertContext, uint dwAddDisposition, SafeCertContextHandle ppStoreContext); } } diff --git a/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CertEnumCertificatesInStore_IntPtr.cs b/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CertEnumCertificatesInStore_IntPtr.cs index 2302457bf7d3a9..e84ba3f3177544 100644 --- a/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CertEnumCertificatesInStore_IntPtr.cs +++ b/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CertEnumCertificatesInStore_IntPtr.cs @@ -9,7 +9,7 @@ internal static partial class Interop { internal static partial class Crypt32 { - [DllImport(Interop.Libraries.Crypt32, CharSet = CharSet.Unicode, SetLastError = true)] - internal static extern IntPtr CertEnumCertificatesInStore(SafeCertStoreHandle hCertStore, IntPtr pPrevCertContext); + [GeneratedDllImport(Interop.Libraries.Crypt32, SetLastError = true)] + internal static partial IntPtr CertEnumCertificatesInStore(SafeCertStoreHandle hCertStore, IntPtr pPrevCertContext); } } diff --git a/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CertOpenStore.cs b/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CertOpenStore.cs index e67285209065c4..0374c231d3cf95 100644 --- a/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CertOpenStore.cs +++ b/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CertOpenStore.cs @@ -13,7 +13,7 @@ internal static partial class Crypt32 internal const uint X509_ASN_ENCODING = 0x00000001; internal const uint CERT_STORE_PROV_MEMORY = 2; - [DllImport(Interop.Libraries.Crypt32, CharSet = CharSet.Unicode, SetLastError = true)] - internal static extern SafeCertStoreHandle CertOpenStore(IntPtr lpszStoreProvider, uint dwMsgAndCertEncodingType, IntPtr hCryptProv, uint dwFlags, string pvPara); + [GeneratedDllImport(Interop.Libraries.Crypt32, SetLastError = true)] + internal static partial SafeCertStoreHandle CertOpenStore(IntPtr lpszStoreProvider, uint dwMsgAndCertEncodingType, IntPtr hCryptProv, uint dwFlags, IntPtr pvPara); } } diff --git a/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CryptProtectData.cs b/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CryptProtectData.cs index 6fab3a93fad5f4..c41e78d82d8e6b 100644 --- a/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CryptProtectData.cs +++ b/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CryptProtectData.cs @@ -9,15 +9,15 @@ internal static partial class Interop { internal static partial class Crypt32 { - [DllImport(Libraries.Crypt32, CharSet = CharSet.Unicode, SetLastError = true)] + [GeneratedDllImport(Libraries.Crypt32, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] - internal static extern bool CryptProtectData( - [In] ref DATA_BLOB pDataIn, - [In] string? szDataDescr, - [In] ref DATA_BLOB pOptionalEntropy, - [In] IntPtr pvReserved, - [In] IntPtr pPromptStruct, - [In] CryptProtectDataFlags dwFlags, - [Out] out DATA_BLOB pDataOut); + internal static partial bool CryptProtectData( + in DATA_BLOB pDataIn, + string? szDataDescr, + ref DATA_BLOB pOptionalEntropy, + IntPtr pvReserved, + IntPtr pPromptStruct, + CryptProtectDataFlags dwFlags, + out DATA_BLOB pDataOut); } } diff --git a/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CryptUnprotectData.cs b/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CryptUnprotectData.cs index ab32e33d59f02e..e4f2ba8564b8bb 100644 --- a/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CryptUnprotectData.cs +++ b/src/libraries/Common/src/Interop/Windows/Crypt32/Interop.CryptUnprotectData.cs @@ -9,15 +9,15 @@ internal static partial class Interop { internal static partial class Crypt32 { - [DllImport(Libraries.Crypt32, CharSet = CharSet.Unicode, SetLastError = true)] + [GeneratedDllImport(Libraries.Crypt32, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] - internal static extern bool CryptUnprotectData( - [In] ref DATA_BLOB pDataIn, - [In] IntPtr ppszDataDescr, - [In] ref DATA_BLOB pOptionalEntropy, - [In] IntPtr pvReserved, - [In] IntPtr pPromptStruct, - [In] CryptProtectDataFlags dwFlags, - [Out] out DATA_BLOB pDataOut); + internal static partial bool CryptUnprotectData( + in DATA_BLOB pDataIn, + IntPtr ppszDataDescr, + ref DATA_BLOB pOptionalEntropy, + IntPtr pvReserved, + IntPtr pPromptStruct, + CryptProtectDataFlags dwFlags, + out DATA_BLOB pDataOut); } } diff --git a/src/libraries/Common/src/Interop/Windows/CryptUI/Interop.CryptUIDlgCertificate.cs b/src/libraries/Common/src/Interop/Windows/CryptUI/Interop.CryptUIDlgCertificate.cs index d7d06ad62bfa3f..04925e171d71f1 100644 --- a/src/libraries/Common/src/Interop/Windows/CryptUI/Interop.CryptUIDlgCertificate.cs +++ b/src/libraries/Common/src/Interop/Windows/CryptUI/Interop.CryptUIDlgCertificate.cs @@ -53,10 +53,13 @@ internal sealed class CRYPTUI_SELECTCERTIFICATE_STRUCTW internal IntPtr hSelectedCertStore; } - [DllImport(Interop.Libraries.CryptUI, CharSet = CharSet.Unicode, SetLastError = true)] +#pragma warning disable DLLIMPORTGENANALYZER015 // Use 'GeneratedDllImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time + // TODO: [DllImportGenerator] Switch to use GeneratedDllImport once we support non-blittable types. + [DllImport(Interop.Libraries.CryptUI, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)] internal static extern bool CryptUIDlgViewCertificateW([MarshalAs(UnmanagedType.LPStruct)] CRYPTUI_VIEWCERTIFICATE_STRUCTW ViewInfo, IntPtr pfPropertiesChanged); - [DllImport(Interop.Libraries.CryptUI, CharSet = CharSet.Unicode, SetLastError = true)] + [DllImport(Interop.Libraries.CryptUI, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)] internal static extern SafeCertContextHandle CryptUIDlgSelectCertificateW([In, Out, MarshalAs(UnmanagedType.LPStruct)] CRYPTUI_SELECTCERTIFICATE_STRUCTW csc); +#pragma warning restore DLLIMPORTGENANALYZER015 } } diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateToolhelp32Snapshot.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateToolhelp32Snapshot.cs new file mode 100644 index 00000000000000..b0dff37964bf08 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateToolhelp32Snapshot.cs @@ -0,0 +1,55 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Kernel32 + { + [Flags] + internal enum SnapshotFlags : uint + { + HeapList = 0x00000001, + Process = 0x00000002, + Thread = 0x00000004, + Module = 0x00000008, + Module32 = 0x00000010, + All = (HeapList | Process | Thread | Module), + Inherit = 0x80000000, + NoHeaps = 0x40000000 + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] + internal struct PROCESSENTRY32 + { + internal int dwSize; + internal int cntUsage; + internal int th32ProcessID; + internal IntPtr th32DefaultHeapID; + internal int th32ModuleID; + internal int cntThreads; + internal int th32ParentProcessID; + internal int pcPriClassBase; + internal int dwFlags; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)] + internal string szExeFile; + } + + // https://docs.microsoft.com/windows/desktop/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot + [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)] + internal static partial IntPtr CreateToolhelp32Snapshot(SnapshotFlags dwFlags, uint th32ProcessID); + +#pragma warning disable DLLIMPORTGENANALYZER015 // Use 'GeneratedDllImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time + // TODO: [DllImportGenerator] Switch to use GeneratedDllImport once we support non-blittable types. + // https://docs.microsoft.com/windows/desktop/api/tlhelp32/nf-tlhelp32-process32first + [DllImport(Libraries.Kernel32, SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] + internal static extern bool Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 lppe); + + // https://docs.microsoft.com/windows/desktop/api/tlhelp32/nf-tlhelp32-process32next + [DllImport(Libraries.Kernel32, SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] + internal static extern bool Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 lppe); +#pragma warning restore DLLIMPORTGENANALYZER015 + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GlobalLock.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GlobalLock.cs index 8971a6326bf841..196db6ce44e205 100644 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GlobalLock.cs +++ b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GlobalLock.cs @@ -8,8 +8,8 @@ internal static partial class Interop { internal static partial class Kernel32 { - [DllImport(Libraries.Kernel32, ExactSpelling = true, SetLastError = true)] - public static extern IntPtr GlobalLock(IntPtr hMem); + [GeneratedDllImport(Libraries.Kernel32, ExactSpelling = true, SetLastError = true)] + public static partial IntPtr GlobalLock(IntPtr hMem); public static IntPtr GlobalLock(HandleRef hMem) { @@ -18,8 +18,8 @@ public static IntPtr GlobalLock(HandleRef hMem) return result; } - [DllImport(Libraries.Kernel32, ExactSpelling = true)] - public static extern IntPtr GlobalUnlock(IntPtr hMem); + [GeneratedDllImport(Libraries.Kernel32, ExactSpelling = true)] + public static partial IntPtr GlobalUnlock(IntPtr hMem); public static IntPtr GlobalUnlock(HandleRef hMem) { diff --git a/src/libraries/Common/src/Interop/Windows/Ole32/Interop.CoGetObjectContext.cs b/src/libraries/Common/src/Interop/Windows/Ole32/Interop.CoGetObjectContext.cs index c73b065ea59537..851878a81554d6 100644 --- a/src/libraries/Common/src/Interop/Windows/Ole32/Interop.CoGetObjectContext.cs +++ b/src/libraries/Common/src/Interop/Windows/Ole32/Interop.CoGetObjectContext.cs @@ -3,13 +3,15 @@ using System; using System.Runtime.InteropServices; -using System.Runtime.InteropServices.ComTypes; internal static partial class Interop { internal static partial class Ole32 { +#pragma warning disable DLLIMPORTGENANALYZER015 // Use 'GeneratedDllImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time + // TODO: [DllImportGenerator] Switch to use GeneratedDllImport once we annotate blittable types used in interop in CoreLib (like Guid) [DllImport(Libraries.Ole32)] internal static extern int CoGetObjectContext([MarshalAs(UnmanagedType.LPStruct)] Guid riid, out IntPtr ppv); +#pragma warning restore DLLIMPORTGENANALYZER015 } } diff --git a/src/libraries/Common/src/Interop/Windows/User32/Interop.GetSysColor.cs b/src/libraries/Common/src/Interop/Windows/User32/Interop.GetSysColor.cs index b066e25ec6eb2a..2ab729812f012e 100644 --- a/src/libraries/Common/src/Interop/Windows/User32/Interop.GetSysColor.cs +++ b/src/libraries/Common/src/Interop/Windows/User32/Interop.GetSysColor.cs @@ -15,7 +15,7 @@ internal static partial class User32 // index that doesn't exist. [SuppressGCTransition] - [DllImport(Libraries.User32, ExactSpelling = true)] - internal static extern uint GetSysColor(int nIndex); + [GeneratedDllImport(Libraries.User32, ExactSpelling = true)] + internal static partial uint GetSysColor(int nIndex); } } diff --git a/src/libraries/Common/src/Interop/Windows/User32/Interop.MessageBeep.cs b/src/libraries/Common/src/Interop/Windows/User32/Interop.MessageBeep.cs index ad7bbbedcc17e1..cc3b1f2a23f060 100644 --- a/src/libraries/Common/src/Interop/Windows/User32/Interop.MessageBeep.cs +++ b/src/libraries/Common/src/Interop/Windows/User32/Interop.MessageBeep.cs @@ -13,7 +13,7 @@ internal static partial class User32 internal const int MB_ICONEXCLAMATION = 0x30; internal const int MB_ICONASTERISK = 0x40; - [DllImport(Libraries.User32, ExactSpelling = true)] - internal static extern bool MessageBeep(int type); + [GeneratedDllImport(Libraries.User32, ExactSpelling = true)] + internal static partial bool MessageBeep(int type); } } diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.MMCKINFO.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.MMCKINFO.cs index 1bcbc116c8a29a..f619de10d38eb7 100644 --- a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.MMCKINFO.cs +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.MMCKINFO.cs @@ -8,7 +8,7 @@ internal static partial class Interop internal static partial class WinMM { [StructLayout(LayoutKind.Sequential)] - internal sealed class MMCKINFO + internal struct MMCKINFO { internal int ckID; internal int cksize; diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.MMSYSERR.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.MMSYSERR.cs new file mode 100644 index 00000000000000..6cfb6fba7164e0 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.MMSYSERR.cs @@ -0,0 +1,37 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class WinMM + { + // Enum equivalent to MMSYSERR_* + internal enum MMSYSERR : int + { + NOERROR = 0, + ERROR = (1), + BADDEVICEID = (2), + NOTENABLED = (3), + ALLOCATED = (4), + INVALHANDLE = (5), + NODRIVER = (6), + NOMEM = (7), + NOTSUPPORTED = (8), + BADERRNUM = (9), + INVALFLAG = (10), + INVALPARAM = (11), + HANDLEBUSY = (12), + INVALIDALIAS = (13), + BADDB = (14), + KEYNOTFOUND = (15), + READERROR = (16), + WRITEERROR = (17), + DELETEERROR = (18), + VALNOTFOUND = (19), + NODRIVERCB = (20), + LASTERROR = (20) + } + } +} diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.PlaySound.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.PlaySound.cs index 771fa9d1b2fd22..14b789bf118069 100644 --- a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.PlaySound.cs +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.PlaySound.cs @@ -17,10 +17,10 @@ internal static partial class WinMM internal const int SND_FILENAME = 0x20000; internal const int SND_NOSTOP = 0x10; - [DllImport(Libraries.WinMM, ExactSpelling = true, CharSet = CharSet.Unicode, EntryPoint = "PlaySoundW")] - internal static extern bool PlaySound(string soundName, IntPtr hmod, int soundFlags); + [GeneratedDllImport(Libraries.WinMM, EntryPoint = "PlaySoundW", CharSet = CharSet.Unicode, ExactSpelling = true)] + internal static partial bool PlaySound(string soundName, IntPtr hmod, int soundFlags); - [DllImport(Libraries.WinMM, ExactSpelling = true, EntryPoint = "PlaySoundW")] - internal static extern bool PlaySound(byte[] soundName, IntPtr hmod, int soundFlags); + [GeneratedDllImport(Libraries.WinMM, EntryPoint = "PlaySoundW", ExactSpelling = true)] + internal static partial bool PlaySound(byte[] soundName, IntPtr hmod, int soundFlags); } } diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioAscend.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioAscend.cs index 69c35b762890f8..66ca175b0493d0 100644 --- a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioAscend.cs +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioAscend.cs @@ -8,7 +8,7 @@ internal static partial class Interop { internal static partial class WinMM { - [DllImport(Libraries.WinMM)] - internal static extern int mmioAscend(IntPtr hMIO, MMCKINFO lpck, int flags); + [GeneratedDllImport(Libraries.WinMM)] + internal static unsafe partial int mmioAscend(IntPtr hMIO, MMCKINFO* lpck, int flags); } } diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioClose.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioClose.cs index 0948ba5b44b6e6..dc3c9af75f5bef 100644 --- a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioClose.cs +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioClose.cs @@ -8,7 +8,7 @@ internal static partial class Interop { internal static partial class WinMM { - [DllImport(Interop.Libraries.WinMM)] - internal static extern int mmioClose(IntPtr hMIO, int flags); + [GeneratedDllImport(Interop.Libraries.WinMM)] + internal static partial int mmioClose(IntPtr hMIO, int flags); } } diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioDescend.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioDescend.cs index ede50147bd43a0..9b3396350d3b0c 100644 --- a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioDescend.cs +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioDescend.cs @@ -10,10 +10,11 @@ internal static partial class WinMM { internal const int MMIO_FINDRIFF = 0x00000020; - [DllImport(Libraries.WinMM)] - internal static extern int mmioDescend(IntPtr hMIO, - [MarshalAs(UnmanagedType.LPStruct)] MMCKINFO lpck, - [MarshalAs(UnmanagedType.LPStruct)] MMCKINFO lcpkParent, - int flags); + [GeneratedDllImport(Libraries.WinMM)] + internal static unsafe partial int mmioDescend( + IntPtr hMIO, + MMCKINFO* lpck, + MMCKINFO* lcpkParent, + int flags); } } diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioOpen.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioOpen.cs index bdff1f1cd7ffa0..a1282b1209ac68 100644 --- a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioOpen.cs +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioOpen.cs @@ -11,7 +11,7 @@ internal static partial class WinMM internal const int MMIO_READ = 0x00000000; internal const int MMIO_ALLOCBUF = 0x00010000; - [DllImport(Libraries.WinMM, CharSet = CharSet.Auto)] - internal static extern IntPtr mmioOpen(string fileName, IntPtr not_used, int flags); + [GeneratedDllImport(Libraries.WinMM, CharSet = CharSet.Auto)] + internal static partial IntPtr mmioOpen(string fileName, IntPtr not_used, int flags); } } diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioRead.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioRead.cs index 3fb30c873de647..18c4007a5ff88b 100644 --- a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioRead.cs +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.mmioRead.cs @@ -24,7 +24,7 @@ internal sealed class WAVEFORMATEX internal const int WAVE_FORMAT_ADPCM = 0x0002; internal const int WAVE_FORMAT_IEEE_FLOAT = 0x0003; - [DllImport(Libraries.WinMM)] - internal static extern int mmioRead(IntPtr hMIO, [MarshalAs(UnmanagedType.LPArray)] byte[] wf, int cch); + [GeneratedDllImport(Libraries.WinMM)] + internal static partial int mmioRead(IntPtr hMIO, [MarshalAs(UnmanagedType.LPArray)] byte[] wf, int cch); } } diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutClose.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutClose.cs new file mode 100644 index 00000000000000..ffbd5829d91506 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutClose.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class WinMM + { + /// + /// This function closes the specified waveform output device. + /// + /// Handle to the waveform-audio output device. If the function + /// succeeds, the handle is no longer valid after this call. + /// MMSYSERR + [GeneratedDllImport(Libraries.WinMM)] + internal static partial MMSYSERR waveOutClose(IntPtr hwo); + } +} diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutGetDevCaps.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutGetDevCaps.cs new file mode 100644 index 00000000000000..31a5a78df9eae7 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutGetDevCaps.cs @@ -0,0 +1,43 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class WinMM + { +#pragma warning disable CA1823 // unused fields + internal struct WAVEOUTCAPS + { + private ushort wMid; + private ushort wPid; + private uint vDriverVersion; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] + internal string szPname; + private uint dwFormats; + private ushort wChannels; + private ushort wReserved1; + private ushort dwSupport; + } +#pragma warning restore CA1823 + + /// + /// This function queries a specified waveform device to determine its + /// capabilities. + /// + /// Identifier of the waveform-audio output device. + /// It can be either a device identifier or a Handle to an open waveform-audio + /// output device. + /// Pointer to a WAVEOUTCAPS structure to be filled with + /// information about the capabilities of the device. + /// Size, in bytes, of the WAVEOUTCAPS structure. + /// MMSYSERR +#pragma warning disable DLLIMPORTGENANALYZER015 // Use 'GeneratedDllImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time + // TODO: [DllImportGenerator] Switch to use GeneratedDllImport once we support non-blittable types. + [DllImport(Libraries.WinMM)] + internal static extern MMSYSERR waveOutGetDevCaps(IntPtr uDeviceID, ref WAVEOUTCAPS caps, int cbwoc); +#pragma warning restore DLLIMPORTGENANALYZER015 + } +} diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutGetNumDevs.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutGetNumDevs.cs new file mode 100644 index 00000000000000..0878bc92ddfd17 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutGetNumDevs.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class WinMM + { + /// + /// This function retrieves the number of waveform output devices present + /// in the system. + /// + /// The number of devices indicates success. Zero indicates that + /// no devices are present or that an error occurred. + [GeneratedDllImport(Libraries.WinMM)] + internal static partial int waveOutGetNumDevs(); + } +} diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutOpen.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutOpen.cs new file mode 100644 index 00000000000000..1fbb89c8836dc9 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutOpen.cs @@ -0,0 +1,51 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class WinMM + { + internal enum MM_MSG + { + MM_WOM_OPEN = 0x03BB, + MM_WOM_CLOSE = 0x03BC, + MM_WOM_DONE = 0x03BD + } + + // Flag specifying the use of a callback window for sound messages + internal const uint CALLBACK_WINDOW = 0x10000; + internal const uint CALLBACK_NULL = 0x00000000; + internal const uint CALLBACK_FUNCTION = 0x00030000; + + internal delegate void WaveOutProc(IntPtr hwo, MM_MSG uMsg, IntPtr dwInstance, IntPtr dwParam1, IntPtr dwParam2); + + /// + /// This function opens a specified waveform output device for playback. + /// + /// Address filled with a handle identifying the open + /// waveform-audio output device. Use the handle to identify the device + /// when calling other waveform-audio output functions. This parameter might + /// be NULL if the WAVE_FORMAT_QUERY flag is specified for fdwOpen. + /// Identifier of the waveform-audio output device to + /// open. It can be either a device identifier or a Handle to an open + /// waveform-audio input device. + /// Pointer to a WaveFormat structure that identifies + /// the format of the waveform-audio data to be sent to the device. You can + /// free this structure immediately after passing it to waveOutOpen. + /// Specifies the address of a fixed callback function, + /// an event handle, a handle to a window, or the identifier of a thread to be + /// called during waveform-audio playback to process messages related to the + /// progress of the playback. If no callback function is required, this value + /// can be zero. + /// Specifies user-instance data passed to the + /// callback mechanism. This parameter is not used with the window callback + /// mechanism. + /// Flags for opening the device. + /// MMSYSERR + [GeneratedDllImport(Libraries.WinMM)] + internal static partial MMSYSERR waveOutOpen(ref IntPtr phwo, int uDeviceID, byte[] pwfx, WaveOutProc dwCallback, IntPtr dwInstance, uint fdwOpen); + } +} diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutPause.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutPause.cs new file mode 100644 index 00000000000000..02b5d5a13d014d --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutPause.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class WinMM + { + /// + /// This function pauses playback on a specified waveform output device. The + /// current playback position is saved. Use waveOutRestart to resume playback + /// from the current playback position. + /// + /// Handle to the waveform-audio output device. + /// MMSYSERR + [GeneratedDllImport(Libraries.WinMM)] + internal static partial MMSYSERR waveOutPause(IntPtr hwo); + } +} diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutPrepareHeader.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutPrepareHeader.cs new file mode 100644 index 00000000000000..a8921127996e06 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutPrepareHeader.cs @@ -0,0 +1,52 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class WinMM + { + /// + /// MM WAVEHDR structure + /// + [StructLayout(LayoutKind.Sequential)] + internal struct WAVEHDR + { + internal IntPtr lpData; // disposed by the GCHandle + internal uint dwBufferLength; + internal uint dwBytesRecorded; + internal uint dwUser; + internal uint dwFlags; + internal uint dwLoops; + internal IntPtr lpNext; // unused + internal uint reserved; + } + + /// + /// This function prepares a waveform data block for playback. + /// + /// Handle to the waveform-audio output device. + /// Pointer to a WaveHeader structure that identifies the data + /// block to be prepared. The buffer's base address must be aligned with the + /// respect to the sample size. + /// Size, in bytes, of the WaveHeader structure. + /// MMSYSERR + [GeneratedDllImport(Libraries.WinMM)] + internal static partial MMSYSERR waveOutPrepareHeader(IntPtr hwo, IntPtr pwh, int cbwh); + + /// + /// This function cleans up the preparation performed by waveOutPrepareHeader. + /// The function must be called after the device driver is finished with a data + /// block. You must call this function before freeing the data buffer. + /// + /// Handle to the waveform-audio output device. + /// Pointer to a WaveHeader structure identifying the data block + /// to be cleaned up. + /// Size, in bytes, of the WaveHeader structure. + /// MMSYSERR + [GeneratedDllImport(Libraries.WinMM)] + internal static partial MMSYSERR waveOutUnprepareHeader(IntPtr hwo, IntPtr pwh, int cbwh); + } +} diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutReset.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutReset.cs new file mode 100644 index 00000000000000..6b98aa846b7991 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutReset.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class WinMM + { + /// + /// This function stops playback on a specified waveform output device and + /// resets the current position to 0. All pending playback buffers are marked + /// as done and returned to the application. + /// + /// Handle to the waveform-audio output device. + /// MMSYSERR + [GeneratedDllImport(Libraries.WinMM)] + internal static partial MMSYSERR waveOutReset(IntPtr hwo); + } +} diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutRestart.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutRestart.cs new file mode 100644 index 00000000000000..c9282fd393f385 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutRestart.cs @@ -0,0 +1,19 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class WinMM + { + /// + /// This function restarts a paused waveform output device. + /// + /// Handle to the waveform-audio output device. + /// MMSYSERR + [GeneratedDllImport(Libraries.WinMM)] + internal static partial MMSYSERR waveOutRestart(IntPtr hwo); + } +} diff --git a/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutWrite.cs b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutWrite.cs new file mode 100644 index 00000000000000..ff37bc6336e564 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/WinMm/Interop.waveOutWrite.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class WinMM + { + /// + /// This function sends a data block to the specified waveform output device. + /// + /// Handle to the waveform-audio output device. + /// Pointer to a WaveHeader structure containing information + /// about the data block. + /// Size, in bytes, of the WaveHeader structure. + /// MMSYSERR + [GeneratedDllImport(Libraries.WinMM)] + internal static partial MMSYSERR waveOutWrite(IntPtr hwo, IntPtr pwh, int cbwh); + } +} diff --git a/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/src/Internal/Win32.cs b/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/src/Internal/Win32.cs index b8ebd1dae1f4f4..7e9f8e3371c7bf 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/src/Internal/Win32.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/src/Internal/Win32.cs @@ -9,33 +9,17 @@ namespace Microsoft.Extensions.Hosting.WindowsServices.Internal { internal static class Win32 { - // https://docs.microsoft.com/en-us/windows/desktop/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot - [DllImport("kernel32", SetLastError = true)] - private static extern IntPtr CreateToolhelp32Snapshot(SnapshotFlags dwFlags, uint th32ProcessID); - - // https://docs.microsoft.com/en-us/windows/desktop/api/tlhelp32/nf-tlhelp32-process32first - [DllImport("kernel32", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] - private static extern bool Process32First([In]IntPtr hSnapshot, ref PROCESSENTRY32 lppe); - - // https://docs.microsoft.com/en-us/windows/desktop/api/tlhelp32/nf-tlhelp32-process32next - [DllImport("kernel32", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] - private static extern bool Process32Next([In]IntPtr hSnapshot, ref PROCESSENTRY32 lppe); - - [DllImport("kernel32", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool CloseHandle([In] IntPtr hObject); - internal static Process GetParentProcess() { var snapshotHandle = IntPtr.Zero; try { // Get a list of all processes - snapshotHandle = CreateToolhelp32Snapshot(SnapshotFlags.Process, 0); + snapshotHandle = Interop.Kernel32.CreateToolhelp32Snapshot(Interop.Kernel32.SnapshotFlags.Process, 0); - PROCESSENTRY32 procEntry = default(PROCESSENTRY32); - procEntry.dwSize = Marshal.SizeOf(typeof(PROCESSENTRY32)); - if (Process32First(snapshotHandle, ref procEntry)) + Interop.Kernel32.PROCESSENTRY32 procEntry = default(Interop.Kernel32.PROCESSENTRY32); + procEntry.dwSize = Marshal.SizeOf(typeof(Interop.Kernel32.PROCESSENTRY32)); + if (Interop.Kernel32.Process32First(snapshotHandle, ref procEntry)) { var currentProcessId = Process.GetCurrentProcess().Id; do @@ -45,7 +29,7 @@ internal static Process GetParentProcess() return Process.GetProcessById((int)procEntry.th32ParentProcessID); } } - while (Process32Next(snapshotHandle, ref procEntry)); + while (Interop.Kernel32.Process32Next(snapshotHandle, ref procEntry)); } } catch (Exception) @@ -53,40 +37,10 @@ internal static Process GetParentProcess() } finally { - CloseHandle(snapshotHandle); + Interop.Kernel32.CloseHandle(snapshotHandle); } return null; } - - [Flags] - private enum SnapshotFlags : uint - { - HeapList = 0x00000001, - Process = 0x00000002, - Thread = 0x00000004, - Module = 0x00000008, - Module32 = 0x00000010, - All = (HeapList | Process | Thread | Module), - Inherit = 0x80000000, - NoHeaps = 0x40000000 - } - - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] - private struct PROCESSENTRY32 - { - internal const int MAX_PATH = 260; - internal int dwSize; - internal int cntUsage; - internal int th32ProcessID; - internal IntPtr th32DefaultHeapID; - internal int th32ModuleID; - internal int cntThreads; - internal int th32ParentProcessID; - internal int pcPriClassBase; - internal int dwFlags; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)] - internal string szExeFile; - } } } diff --git a/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/src/Microsoft.Extensions.Hosting.WindowsServices.csproj b/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/src/Microsoft.Extensions.Hosting.WindowsServices.csproj index 88472dd6b31bc0..260db551d47776 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/src/Microsoft.Extensions.Hosting.WindowsServices.csproj +++ b/src/libraries/Microsoft.Extensions.Hosting.WindowsServices/src/Microsoft.Extensions.Hosting.WindowsServices.csproj @@ -1,11 +1,23 @@  + true netstandard2.1;netstandard2.0;$(NetFrameworkMinimum) true .NET hosting infrastructure for Windows Services. + + + + + + + diff --git a/src/libraries/System.Security.Cryptography.ProtectedData/src/System/Security/Cryptography/ProtectedData.cs b/src/libraries/System.Security.Cryptography.ProtectedData/src/System/Security/Cryptography/ProtectedData.cs index 0e6b65bea16644..d67da72075b395 100644 --- a/src/libraries/System.Security.Cryptography.ProtectedData/src/System/Security/Cryptography/ProtectedData.cs +++ b/src/libraries/System.Security.Cryptography.ProtectedData/src/System/Security/Cryptography/ProtectedData.cs @@ -59,8 +59,8 @@ private static byte[] ProtectOrUnprotect(byte[] inputData, byte[]? optionalEntro try { bool success = protect ? - Interop.Crypt32.CryptProtectData(ref userDataBlob, null, ref optionalEntropyBlob, IntPtr.Zero, IntPtr.Zero, flags, out outputBlob) : - Interop.Crypt32.CryptUnprotectData(ref userDataBlob, IntPtr.Zero, ref optionalEntropyBlob, IntPtr.Zero, IntPtr.Zero, flags, out outputBlob); + Interop.Crypt32.CryptProtectData(in userDataBlob, null, ref optionalEntropyBlob, IntPtr.Zero, IntPtr.Zero, flags, out outputBlob) : + Interop.Crypt32.CryptUnprotectData(in userDataBlob, IntPtr.Zero, ref optionalEntropyBlob, IntPtr.Zero, IntPtr.Zero, flags, out outputBlob); if (!success) { int lastWin32Error = Marshal.GetLastWin32Error(); diff --git a/src/libraries/System.Speech/src/Internal/Synthesis/AudioDeviceOut.cs b/src/libraries/System.Speech/src/Internal/Synthesis/AudioDeviceOut.cs index a40b1e855a014e..40b1ecfcfa20db 100644 --- a/src/libraries/System.Speech/src/Internal/Synthesis/AudioDeviceOut.cs +++ b/src/libraries/System.Speech/src/Internal/Synthesis/AudioDeviceOut.cs @@ -20,7 +20,7 @@ internal class AudioDeviceOut : AudioBase, IDisposable /// internal AudioDeviceOut(int curDevice, IAsyncDispatch asyncDispatch) { - _delegate = new SafeNativeMethods.WaveOutProc(CallBackProc); + _delegate = new Interop.WinMM.WaveOutProc(CallBackProc); _asyncDispatch = asyncDispatch; _curDevice = curDevice; } @@ -39,7 +39,7 @@ private void Dispose(bool disposing) { if (_deviceOpen && _hwo != IntPtr.Zero) { - SafeNativeMethods.waveOutClose(_hwo); + Interop.WinMM.waveOutClose(_hwo); _deviceOpen = false; } if (disposing) @@ -68,21 +68,21 @@ internal override void Begin(byte[] wfx) // Get the alignments values WAVEFORMATEX.AvgBytesPerSec(wfx, out _nAvgBytesPerSec, out _blockAlign); - MMSYSERR result; + Interop.WinMM.MMSYSERR result; lock (_noWriteOutLock) { - result = SafeNativeMethods.waveOutOpen(ref _hwo, _curDevice, wfx, _delegate, IntPtr.Zero, SafeNativeMethods.CALLBACK_FUNCTION); + result = Interop.WinMM.waveOutOpen(ref _hwo, _curDevice, wfx, _delegate, IntPtr.Zero, Interop.WinMM.CALLBACK_FUNCTION); - if (_fPaused && result == MMSYSERR.NOERROR) + if (_fPaused && result == Interop.WinMM.MMSYSERR.NOERROR) { - result = SafeNativeMethods.waveOutPause(_hwo); + result = Interop.WinMM.waveOutPause(_hwo); } // set the flags _aborted = false; _deviceOpen = true; } - if (result != MMSYSERR.NOERROR) + if (result != Interop.WinMM.MMSYSERR.NOERROR) { throw new AudioException(result); } @@ -108,19 +108,17 @@ internal override void End() { _deviceOpen = false; - MMSYSERR result; - CheckForAbort(); if (_queueIn.Count != 0) { - SafeNativeMethods.waveOutReset(_hwo); + Interop.WinMM.waveOutReset(_hwo); } // Close it; no point in returning errors if this fails - result = SafeNativeMethods.waveOutClose(_hwo); + Interop.WinMM.MMSYSERR result = Interop.WinMM.waveOutClose(_hwo); - if (result != MMSYSERR.NOERROR) + if (result != Interop.WinMM.MMSYSERR.NOERROR) { // This may create a dead lock System.Diagnostics.Debug.Assert(false); @@ -146,9 +144,9 @@ internal override void Play(byte[] buffer) WaveHeader waveHeader = new(buffer); GCHandle waveHdr = waveHeader.WAVEHDR; - MMSYSERR result = SafeNativeMethods.waveOutPrepareHeader(_hwo, waveHdr.AddrOfPinnedObject(), waveHeader.SizeHDR); + Interop.WinMM.MMSYSERR result = Interop.WinMM.waveOutPrepareHeader(_hwo, waveHdr.AddrOfPinnedObject(), waveHeader.SizeHDR); - if (result != MMSYSERR.NOERROR) + if (result != Interop.WinMM.MMSYSERR.NOERROR) { throw new AudioException(result); } @@ -168,8 +166,8 @@ internal override void Play(byte[] buffer) } // Start playback of the first buffer - result = SafeNativeMethods.waveOutWrite(_hwo, waveHdr.AddrOfPinnedObject(), waveHeader.SizeHDR); - if (result != MMSYSERR.NOERROR) + result = Interop.WinMM.waveOutWrite(_hwo, waveHdr.AddrOfPinnedObject(), waveHeader.SizeHDR); + if (result != Interop.WinMM.MMSYSERR.NOERROR) { lock (_queueIn) { @@ -193,8 +191,8 @@ internal override void Pause() { if (_deviceOpen) { - MMSYSERR result = SafeNativeMethods.waveOutPause(_hwo); - if (result != MMSYSERR.NOERROR) + Interop.WinMM.MMSYSERR result = Interop.WinMM.waveOutPause(_hwo); + if (result != Interop.WinMM.MMSYSERR.NOERROR) { System.Diagnostics.Debug.Assert(false, ((int)result).ToString(System.Globalization.CultureInfo.InvariantCulture)); } @@ -215,8 +213,8 @@ internal override void Resume() { if (_deviceOpen) { - MMSYSERR result = SafeNativeMethods.waveOutRestart(_hwo); - if (result != MMSYSERR.NOERROR) + Interop.WinMM.MMSYSERR result = Interop.WinMM.waveOutRestart(_hwo); + if (result != Interop.WinMM.MMSYSERR.NOERROR) { System.Diagnostics.Debug.Assert(false); } @@ -236,7 +234,7 @@ internal override void Abort() _aborted = true; if (_queueIn.Count > 0) { - SafeNativeMethods.waveOutReset(_hwo); + Interop.WinMM.waveOutReset(_hwo); _evt.WaitOne(); } } @@ -286,7 +284,7 @@ internal override void WaitUntilDone() /// Number of output devices internal static int NumDevices() { - return SafeNativeMethods.waveOutGetNumDevs(); + return Interop.WinMM.waveOutGetNumDevs(); } internal static int GetDevicedId(string name) @@ -294,7 +292,7 @@ internal static int GetDevicedId(string name) for (int iDevice = 0; iDevice < NumDevices(); iDevice++) { string device; - if (GetDeviceName(iDevice, out device) == MMSYSERR.NOERROR && string.Equals(device, name, StringComparison.OrdinalIgnoreCase)) + if (GetDeviceName(iDevice, out device) == Interop.WinMM.MMSYSERR.NOERROR && string.Equals(device, name, StringComparison.OrdinalIgnoreCase)) { return iDevice; } @@ -308,20 +306,20 @@ internal static int GetDevicedId(string name) /// ID of the device /// Destination string assigned the name /// MMSYSERR.NOERROR if successful - internal static MMSYSERR GetDeviceName(int deviceId, [MarshalAs(UnmanagedType.LPWStr)] out string prodName) + internal static Interop.WinMM.MMSYSERR GetDeviceName(int deviceId, [MarshalAs(UnmanagedType.LPWStr)] out string prodName) { prodName = string.Empty; - SafeNativeMethods.WAVEOUTCAPS caps = new(); + Interop.WinMM.WAVEOUTCAPS caps = new(); - MMSYSERR result = SafeNativeMethods.waveOutGetDevCaps((IntPtr)deviceId, ref caps, Marshal.SizeOf()); - if (result != MMSYSERR.NOERROR) + Interop.WinMM.MMSYSERR result = Interop.WinMM.waveOutGetDevCaps((IntPtr)deviceId, ref caps, Marshal.SizeOf()); + if (result != Interop.WinMM.MMSYSERR.NOERROR) { return result; } prodName = caps.szPname; - return MMSYSERR.NOERROR; + return Interop.WinMM.MMSYSERR.NOERROR; } #endregion @@ -346,9 +344,9 @@ internal override TimeSpan Duration #region Private Methods - private void CallBackProc(IntPtr hwo, MM_MSG uMsg, IntPtr dwInstance, IntPtr dwParam1, IntPtr dwParam2) + private void CallBackProc(IntPtr hwo, Interop.WinMM.MM_MSG uMsg, IntPtr dwInstance, IntPtr dwParam1, IntPtr dwParam2) { - if (uMsg == MM_MSG.MM_WOM_DONE) + if (uMsg == Interop.WinMM.MM_MSG.MM_WOM_DONE) { InItem inItem; lock (_queueIn) @@ -391,11 +389,9 @@ private void ClearBuffers() foreach (InItem item in _queueOut) { WaveHeader waveHeader = item._waveHeader; - MMSYSERR result; - - result = SafeNativeMethods.waveOutUnprepareHeader( + Interop.WinMM.MMSYSERR result = Interop.WinMM.waveOutUnprepareHeader( _hwo, waveHeader.WAVEHDR.AddrOfPinnedObject(), waveHeader.SizeHDR); - if (result != MMSYSERR.NOERROR) + if (result != Interop.WinMM.MMSYSERR.NOERROR) { //System.Diagnostics.Debug.Assert (false); } @@ -418,7 +414,7 @@ private void CheckForAbort() if (inItem._waveHeader != null) { WaveHeader waveHeader = inItem._waveHeader; - SafeNativeMethods.waveOutUnprepareHeader( + Interop.WinMM.waveOutUnprepareHeader( _hwo, waveHeader.WAVEHDR.AddrOfPinnedObject(), waveHeader.SizeHDR); waveHeader.Dispose(); } @@ -496,7 +492,7 @@ internal void ReleaseData() private ManualResetEvent _evt = new(false); - private SafeNativeMethods.WaveOutProc _delegate; + private Interop.WinMM.WaveOutProc _delegate; private IAsyncDispatch _asyncDispatch; diff --git a/src/libraries/System.Speech/src/Internal/Synthesis/AudioException.cs b/src/libraries/System.Speech/src/Internal/Synthesis/AudioException.cs index c766cd484dc9b8..c23de398cbb00f 100644 --- a/src/libraries/System.Speech/src/Internal/Synthesis/AudioException.cs +++ b/src/libraries/System.Speech/src/Internal/Synthesis/AudioException.cs @@ -12,7 +12,7 @@ internal class AudioException : Exception internal AudioException() { } - internal AudioException(MMSYSERR errorCode) : base(string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0} - Error Code: 0x{1:x}", SR.Get(SRID.AudioDeviceError), (int)errorCode)) + internal AudioException(Interop.WinMM.MMSYSERR errorCode) : base(string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0} - Error Code: 0x{1:x}", SR.Get(SRID.AudioDeviceError), (int)errorCode)) { } protected AudioException(SerializationInfo info, StreamingContext context) : base(info, context) diff --git a/src/libraries/System.Speech/src/Internal/Synthesis/SafeNativeMethods.cs b/src/libraries/System.Speech/src/Internal/Synthesis/SafeNativeMethods.cs deleted file mode 100644 index f0c380b5e919e5..00000000000000 --- a/src/libraries/System.Speech/src/Internal/Synthesis/SafeNativeMethods.cs +++ /dev/null @@ -1,216 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Runtime.InteropServices; - -namespace System.Speech.Internal.Synthesis -{ - // This class *MUST* be internal for security purposes - //CASRemoval:[SuppressUnmanagedCodeSecurity] - internal static class SafeNativeMethods - { - /// - /// This function prepares a waveform data block for playback. - /// - /// Handle to the waveform-audio output device. - /// Pointer to a WaveHeader structure that identifies the data - /// block to be prepared. The buffer's base address must be aligned with the - /// respect to the sample size. - /// Size, in bytes, of the WaveHeader structure. - /// MMSYSERR - [DllImport("winmm.dll")] - internal static extern MMSYSERR waveOutPrepareHeader(IntPtr hwo, IntPtr pwh, int cbwh); - - /// - /// This function sends a data block to the specified waveform output device. - /// - /// Handle to the waveform-audio output device. - /// Pointer to a WaveHeader structure containing information - /// about the data block. - /// Size, in bytes, of the WaveHeader structure. - /// MMSYSERR - [DllImport("winmm.dll")] - internal static extern MMSYSERR waveOutWrite(IntPtr hwo, IntPtr pwh, int cbwh); - - /// - /// This function cleans up the preparation performed by waveOutPrepareHeader. - /// The function must be called after the device driver is finished with a data - /// block. You must call this function before freeing the data buffer. - /// - /// Handle to the waveform-audio output device. - /// Pointer to a WaveHeader structure identifying the data block - /// to be cleaned up. - /// Size, in bytes, of the WaveHeader structure. - /// MMSYSERR - [DllImport("winmm.dll")] - internal static extern MMSYSERR waveOutUnprepareHeader(IntPtr hwo, IntPtr pwh, int cbwh); - - /// - /// This function opens a specified waveform output device for playback. - /// - /// Address filled with a handle identifying the open - /// waveform-audio output device. Use the handle to identify the device - /// when calling other waveform-audio output functions. This parameter might - /// be NULL if the WAVE_FORMAT_QUERY flag is specified for fdwOpen. - /// Identifier of the waveform-audio output device to - /// open. It can be either a device identifier or a Handle to an open - /// waveform-audio input device. - /// Pointer to a WaveFormat structure that identifies - /// the format of the waveform-audio data to be sent to the device. You can - /// free this structure immediately after passing it to waveOutOpen. - /// Specifies the address of a fixed callback function, - /// an event handle, a handle to a window, or the identifier of a thread to be - /// called during waveform-audio playback to process messages related to the - /// progress of the playback. If no callback function is required, this value - /// can be zero. - /// Specifies user-instance data passed to the - /// callback mechanism. This parameter is not used with the window callback - /// mechanism. - /// Flags for opening the device. - /// MMSYSERR - [DllImport("winmm.dll")] - internal static extern MMSYSERR waveOutOpen(ref IntPtr phwo, int uDeviceID, byte[] pwfx, WaveOutProc dwCallback, IntPtr dwInstance, uint fdwOpen); - - /// - /// This function closes the specified waveform output device. - /// - /// Handle to the waveform-audio output device. If the function - /// succeeds, the handle is no longer valid after this call. - /// MMSYSERR - [DllImport("winmm.dll")] - internal static extern MMSYSERR waveOutClose(IntPtr hwo); - - /// - /// This function stops playback on a specified waveform output device and - /// resets the current position to 0. All pending playback buffers are marked - /// as done and returned to the application. - /// - /// Handle to the waveform-audio output device. - /// MMSYSERR - [DllImport("winmm.dll")] - internal static extern MMSYSERR waveOutReset(IntPtr hwo); - - /// - /// This function pauses playback on a specified waveform output device. The - /// current playback position is saved. Use waveOutRestart to resume playback - /// from the current playback position. - /// - /// Handle to the waveform-audio output device. - /// MMSYSERR - [DllImport("winmm.dll")] - internal static extern MMSYSERR waveOutPause(IntPtr hwo); - - /// - /// This function restarts a paused waveform output device. - /// - /// Handle to the waveform-audio output device. - /// MMSYSERR - [DllImport("winmm.dll")] - internal static extern MMSYSERR waveOutRestart(IntPtr hwo); - - internal delegate void WaveOutProc(IntPtr hwo, MM_MSG uMsg, IntPtr dwInstance, IntPtr dwParam1, IntPtr dwParam2); - -#pragma warning disable CA1823 // unused fields - internal struct WAVEOUTCAPS - { - private ushort wMid; - private ushort wPid; - private uint vDriverVersion; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] - internal string szPname; - private uint dwFormats; - private ushort wChannels; - private ushort wReserved1; - private ushort dwSupport; - } -#pragma warning restore CA1823 - - /// - /// This function queries a specified waveform device to determine its - /// capabilities. - /// - /// Identifier of the waveform-audio output device. - /// It can be either a device identifier or a Handle to an open waveform-audio - /// output device. - /// Pointer to a WAVEOUTCAPS structure to be filled with - /// information about the capabilities of the device. - /// Size, in bytes, of the WAVEOUTCAPS structure. - /// MMSYSERR - [DllImport("winmm.dll")] - internal static extern MMSYSERR waveOutGetDevCaps(IntPtr uDeviceID, ref WAVEOUTCAPS caps, int cbwoc); - - /// - /// This function retrieves the number of waveform output devices present - /// in the system. - /// - /// The number of devices indicates success. Zero indicates that - /// no devices are present or that an error occurred. - [DllImport("winmm.dll")] - internal static extern int waveOutGetNumDevs(); - - // Used by MMTIME.wType - internal const uint TIME_MS = 0x0001; - internal const uint TIME_SAMPLES = 0x0002; - internal const uint TIME_BYTES = 0x0004; - internal const uint TIME_TICKS = 0x0020; - - // Flag specifying the use of a callback window for sound messages - internal const uint CALLBACK_WINDOW = 0x10000; - internal const uint CALLBACK_NULL = 0x00000000; - internal const uint CALLBACK_FUNCTION = 0x00030000; - } - - #region Internal Types - - /// - /// MM WAVEHDR structure - /// - [StructLayout(LayoutKind.Sequential)] - internal struct WAVEHDR - { - internal IntPtr lpData; // disposed by the GCHandle - internal uint dwBufferLength; - internal uint dwBytesRecorded; - internal uint dwUser; - internal uint dwFlags; - internal uint dwLoops; - internal IntPtr lpNext; // unused - internal uint reserved; - } - - // Enum equivalent to MMSYSERR_* - internal enum MMSYSERR : int - { - NOERROR = 0, - ERROR = (1), - BADDEVICEID = (2), - NOTENABLED = (3), - ALLOCATED = (4), - INVALHANDLE = (5), - NODRIVER = (6), - NOMEM = (7), - NOTSUPPORTED = (8), - BADERRNUM = (9), - INVALFLAG = (10), - INVALPARAM = (11), - HANDLEBUSY = (12), - INVALIDALIAS = (13), - BADDB = (14), - KEYNOTFOUND = (15), - READERROR = (16), - WRITEERROR = (17), - DELETEERROR = (18), - VALNOTFOUND = (19), - NODRIVERCB = (20), - LASTERROR = (20) - } - - internal enum MM_MSG - { - MM_WOM_OPEN = 0x03BB, - MM_WOM_CLOSE = 0x03BC, - MM_WOM_DONE = 0x03BD - } - - #endregion -} diff --git a/src/libraries/System.Speech/src/Internal/Synthesis/WaveHeader.cs b/src/libraries/System.Speech/src/Internal/Synthesis/WaveHeader.cs index b90879db18a6f9..723f9390b72733 100644 --- a/src/libraries/System.Speech/src/Internal/Synthesis/WaveHeader.cs +++ b/src/libraries/System.Speech/src/Internal/Synthesis/WaveHeader.cs @@ -86,7 +86,7 @@ internal int SizeHDR { get { - return Marshal.SizeOf(); + return Marshal.SizeOf(); } } @@ -142,7 +142,7 @@ internal int SizeHDR private GCHandle _gcHandleWaveHdr; - private WAVEHDR _waveHdr; + private Interop.WinMM.WAVEHDR _waveHdr; /// /// Specifies the length, in bytes, of the buffer. diff --git a/src/libraries/System.Speech/src/System.Speech.csproj b/src/libraries/System.Speech/src/System.Speech.csproj index 783e89d8b0e4bd..660dae30037667 100644 --- a/src/libraries/System.Speech/src/System.Speech.csproj +++ b/src/libraries/System.Speech/src/System.Speech.csproj @@ -4,7 +4,8 @@ $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum);netstandard2.0 - $(NoWarn);CS0649;SA1129;CA1847 + + $(NoWarn);CS0649;SA1129;CA1846;CA1847 annotations false true @@ -119,7 +120,6 @@ System.Speech.Recognition.SpeechRecognizer - @@ -198,6 +198,28 @@ System.Speech.Recognition.SpeechRecognizer + + + + + + + + + + + @@ -238,6 +260,7 @@ System.Speech.Recognition.SpeechRecognizer + @@ -245,7 +268,7 @@ System.Speech.Recognition.SpeechRecognizer - + diff --git a/src/libraries/System.Windows.Extensions/src/System/Media/SoundPlayer.cs b/src/libraries/System.Windows.Extensions/src/System/Media/SoundPlayer.cs index 28a49eeababa84..336a7b4cd8344b 100644 --- a/src/libraries/System.Windows.Extensions/src/System/Media/SoundPlayer.cs +++ b/src/libraries/System.Windows.Extensions/src/System/Media/SoundPlayer.cs @@ -544,13 +544,13 @@ private unsafe void ValidateSoundFile(string fileName) { fccType = mmioFOURCC('W', 'A', 'V', 'E') }; - var ck = new Interop.WinMM.MMCKINFO(); - if (Interop.WinMM.mmioDescend(hMIO, ckRIFF, null, Interop.WinMM.MMIO_FINDRIFF) != 0) + var ck = default(Interop.WinMM.MMCKINFO); + if (Interop.WinMM.mmioDescend(hMIO, &ckRIFF, null, Interop.WinMM.MMIO_FINDRIFF) != 0) { throw new InvalidOperationException(SR.Format(SR.SoundAPIInvalidWaveFile, _soundLocation)); } - while (Interop.WinMM.mmioDescend(hMIO, ck, ckRIFF, 0) == 0) + while (Interop.WinMM.mmioDescend(hMIO, &ck, &ckRIFF, 0) == 0) { if (ck.dwDataOffset + ck.cksize > ckRIFF.dwDataOffset + ckRIFF.cksize) { @@ -584,7 +584,7 @@ private unsafe void ValidateSoundFile(string fileName) // multiple formats? } } - Interop.WinMM.mmioAscend(hMIO, ck, 0); + Interop.WinMM.mmioAscend(hMIO, &ck, 0); } if (waveFormat == null) diff --git a/src/libraries/System.Windows.Extensions/src/System/Security/Cryptography/X509Certificates/X509Certificate2UI.cs b/src/libraries/System.Windows.Extensions/src/System/Security/Cryptography/X509Certificates/X509Certificate2UI.cs index 9e5a9af0b92b4d..7f26dcf23e6fde 100644 --- a/src/libraries/System.Windows.Extensions/src/System/Security/Cryptography/X509Certificates/X509Certificate2UI.cs +++ b/src/libraries/System.Windows.Extensions/src/System/Security/Cryptography/X509Certificates/X509Certificate2UI.cs @@ -105,7 +105,7 @@ private static unsafe SafeCertStoreHandle SelectFromStore(SafeCertStoreHandle sa Interop.Crypt32.X509_ASN_ENCODING | Interop.Crypt32.PKCS_7_ASN_ENCODING, IntPtr.Zero, 0, - null); + IntPtr.Zero); if (safeCertStoreHandle == null || safeCertStoreHandle.IsInvalid) throw new CryptographicException(Marshal.GetLastWin32Error()); diff --git a/src/libraries/System.Windows.Extensions/src/System/Security/Cryptography/X509Certificates/X509Utils.cs b/src/libraries/System.Windows.Extensions/src/System/Security/Cryptography/X509Certificates/X509Utils.cs index 39084758aa0d0f..eb445034c25466 100644 --- a/src/libraries/System.Windows.Extensions/src/System/Security/Cryptography/X509Certificates/X509Utils.cs +++ b/src/libraries/System.Windows.Extensions/src/System/Security/Cryptography/X509Certificates/X509Utils.cs @@ -29,7 +29,7 @@ internal static SafeCertStoreHandle ExportToMemoryStore(X509Certificate2Collecti Interop.Crypt32.X509_ASN_ENCODING | Interop.Crypt32.PKCS_7_ASN_ENCODING, IntPtr.Zero, CERT_STORE_ENUM_ARCHIVED_FLAG | CERT_STORE_CREATE_NEW_FLAG, - null); + IntPtr.Zero); if (safeCertStoreHandle == null || safeCertStoreHandle.IsInvalid) throw new CryptographicException(Marshal.GetLastWin32Error());