开发者

EntryPointNotFound exception thrown when attempting to p/Invoke functions from setupapi.dll

开发者 https://www.devze.com 2023-03-26 23:13 出处:网络
[Flags] public enum DiGetClassFlags : uint { Default= 0x01, Present= 0x02, AllClasses= 0x04, Profile= 0x08,
[Flags]
public enum DiGetClassFlags : uint
{
    Default         = 0x01,
    Present         = 0x02,
    AllClasses      = 0x04,
    Profile         = 0x08,
    DeviceInterface = 0x10
}

[StructLayout(LayoutKind.Sequential)]
public struct SpDeviceInfoData
{
    public uint Size;
    public Guid ClassGuid;
    public uint DevInst;
    public IntPtr Reserved;

    public SpDeviceInfoData(Guid classGuid, uint devInst)
    {
        Size = 0;
        ClassGuid = classGuid;
        DevInst = devInst;
        Reserved = IntPtr.Zero;
        Size = (uint)Marshal.SizeOf(this);
    }
}

public class SetupApi
{
    public const int SP_MAX_MACHINENAME_LENGTH = 263;

    [DllImport("setupapi.dll", CharSet=CharSet.Auto)]
    public static 开发者_运维技巧extern IntPtr SetupDiGetClassDevsEx(
        ref Guid classGuid,
        IntPtr enumerator,
        IntPtr hwndParent,
        DiGetClassFlags flags,
        IntPtr deviceInfoSet,
        IntPtr machineName,
        IntPtr reserved);

    [DllImport("setupapi.dll", SetLastError = true)]
    public static extern bool SetupDiEnumDeviceInfo(
        IntPtr deviceInfoSet, 
        uint memberIndex, 
        ref SpDeviceInfoData deviceInfoData);
}

I use the functions above like such:

public class DisplayInformation
{
    public static string GetDisplayName()
    {
        Guid classMonitor = new Guid(0x4D36E96E, 0xE325, 0x11CE, 0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18);

        IntPtr deviceInfo = SetupApi.SetupDiGetClassDevsEx(
            ref classMonitor, 
            IntPtr.Zero, 
            IntPtr.Zero, 
            DiGetClassFlags.Present,
            IntPtr.Zero,
            IntPtr.Zero,
            IntPtr.Zero);

        if (deviceInfo == IntPtr.Zero)
        {
            throw new Win32Exception(Marshal.GetLastWin32Error(), "SetupDiGetClassDevs failed.");
        }

        ...
    }
}

When SetupApi.SetupDiGetClassDevsEx is called, it throws an EntryPointNotFound exception. I can't for the life of me understand why. Thoughts? Have I missed something in the interop delcarations?


I accidentally named my interop dll setupapi.dll. This prevented the system from finding the real setupapi.dll. Now I feel kind of silly ...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号