尝试从setupapi.dll调用/调用函数时抛出EntryPointNotFound异常

时间:2011-08-11 00:36:21

标签: c# interop pinvoke

[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);
}

我使用上面的函数:

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.");
        }

        ...
    }
}

当调用SetupApi.SetupDiGetClassDevsEx时,它会抛出一个EntryPointNotFound异常。我不能为我的生活理解为什么。思考?我是否错过了interop decarations中的某些内容?

1 个答案:

答案 0 :(得分:0)

我不小心将我的interop dll setupapi.dll命名为。这阻止了系统找到真正的setupapi.dll。现在我觉得有点傻......