Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GeneratedDllImport in Microsoft.Extensions.Hosting.WindowsServices, System.Drawing.Primitives, System.Management, System.Security.Cryptography.ProtectedData, System.Speech, System.Windows.Extensions #61949

Merged
merged 8 commits into from
Nov 23, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
ref DATA_BLOB pDataIn,
string? szDataDescr,
ref DATA_BLOB pOptionalEntropy,
IntPtr pvReserved,
IntPtr pPromptStruct,
CryptProtectDataFlags dwFlags,
out DATA_BLOB pDataOut);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
ref DATA_BLOB pDataIn,
elinor-fung marked this conversation as resolved.
Show resolved Hide resolved
IntPtr ppszDataDescr,
ref DATA_BLOB pOptionalEntropy,
IntPtr pvReserved,
IntPtr pPromptStruct,
CryptProtectDataFlags dwFlags,
out DATA_BLOB pDataOut);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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/en-us/windows/desktop/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot
elinor-fung marked this conversation as resolved.
Show resolved Hide resolved
[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/en-us/windows/desktop/api/tlhelp32/nf-tlhelp32-process32first
elinor-fung marked this conversation as resolved.
Show resolved Hide resolved
[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/en-us/windows/desktop/api/tlhelp32/nf-tlhelp32-process32next
elinor-fung marked this conversation as resolved.
Show resolved Hide resolved
[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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is COM, if it isn't too big of a change I suppose we could change this to Guid*?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a bunch of similar things around Guid, so I figure we can just keep their existing signatures and do a pass when we enable it (it is already noted in #60595).

#pragma warning restore DLLIMPORTGENANALYZER015
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions src/libraries/Common/src/Interop/Windows/WinMm/Interop.MMSYSERR.cs
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// This function closes the specified waveform output device.
/// </summary>
/// <param name="hwo">Handle to the waveform-audio output device. If the function
/// succeeds, the handle is no longer valid after this call.</param>
/// <returns>MMSYSERR</returns>
[GeneratedDllImport(Libraries.WinMM)]
internal static partial MMSYSERR waveOutClose(IntPtr hwo);
}
}
Original file line number Diff line number Diff line change
@@ -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

/// <summary>
/// This function queries a specified waveform device to determine its
/// capabilities.
/// </summary>
/// <param name="uDeviceID">Identifier of the waveform-audio output device.
/// It can be either a device identifier or a Handle to an open waveform-audio
/// output device.</param>
/// <param name="caps">Pointer to a WAVEOUTCAPS structure to be filled with
/// information about the capabilities of the device.</param>
/// <param name="cbwoc">Size, in bytes, of the WAVEOUTCAPS structure.</param>
/// <returns>MMSYSERR</returns>
#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
}
}
Loading