1)原型
WINSETUPAPI
BOOL
WINAPI
SetupDiSetClassInstallParamsA(
    IN HDEVINFO                DeviceInfoSet,
    IN PSP_DEVINFO_DATA        DeviceInfoData,        OPTIONAL
    IN PSP_CLASSINSTALL_HEADER ClassInstallParams,    OPTIONAL
    IN DWORD                   ClassInstallParamsSize
    );[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]static extern bool SetupDiSetClassInstallParams(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, ref SP_CLASSINSTALL_HEADER ClassInstallParams, int ClassInstallParamsSize);2)typedef struct _SP_CLASSINSTALL_HEADER {
    DWORD       cbSize;
    DI_FUNCTION InstallFunction;
} SP_CLASSINSTALL_HEADER, *PSP_CLASSINSTALL_HEADER; [StructLayout(LayoutKind.Sequential)]
        public struct SP_CLASSINSTALL_HEADER
        {
            public UInt32 cbSize;
            public uint InstallFunction;
        }3)typedef struct _SP_PROPCHANGE_PARAMS {
    SP_CLASSINSTALL_HEADER ClassInstallHeader;
    DWORD                  StateChange;
    DWORD                  Scope;
    DWORD                  HwProfile;
} SP_PROPCHANGE_PARAMS, *PSP_PROPCHANGE_PARAMS;  [StructLayout(LayoutKind.Sequential)]
        public struct SP_PROPCHANGE_PARAMS
        {
            public SP_CLASSINSTALL_HEADER ClassInstallHeader;
            public UInt32 StateChange;
            public UInt32 Scope;
            public UInt32 HwProfile;
        }问题:
1)这几个写法是否有问题?
2)
  SP_PROPCHANGE_PARAMS spPropChangeParam = new SP_PROPCHANGE_PARAMS();
            spPropChangeParam.Scope = 0x00000001;
            spPropChangeParam.StateChange = 0;
            spPropChangeParam.ClassInstallHeader.cbSize = (UInt32)System.Runtime.InteropServices.Marshal.SizeOf(spPropChangeParam.ClassInstallHeader);
            spPropChangeParam.ClassInstallHeader.InstallFunction = 0x00000012;
            SetupDiSetClassInstallParams(NewDeviceInfoSet, ref spData, ref spPropChangeParam.ClassInstallHeader, System.Runtime.InteropServices.Marshal.SizeOf(spPropChangeParam));调用不成功,GetLastError错误值87(参数不正确),哪里出问题了?
NewDeviceInfoSet和spData参数应该没有问题。谢谢

解决方案 »

  1.   

    from
    http://www.devmedia.com.br/articles/viewcomp.asp?comp=4092&hl=
    先看看别人是怎么用的public const int DIGCF_PRESENT = 2;
    public const int DIF_PROPERTYCHANGE = 0x00000012;
    public const int DICS_FLAG_GLOBAL = 0x00000001;
    public const int INVALID_HANDLE_VALUE = -1;
    public const int DICS_ENABLE = 0x00000001;
    public const int DICS_DISABLE = 0x00000002;
    public const string USBVIDPID = @"USB\VID_0403&PID_6001\A1000PRA";[StructLayout(LayoutKind.Sequential)]
    public struct SP_CLASSINSTALL_HEADER
    {
        public int cbSize;
        public int InstallFunction;
    }[StructLayout(LayoutKind.Sequential)]
    public struct SP_PROPCHANGE_PARAMS
    {
        public SP_CLASSINSTALL_HEADER ClassInstallHeader;
        public int StateChange;
        public int Scope;
        public int HwProfile;
        public void Init()
        {
            ClassInstallHeader = new SP_CLASSINSTALL_HEADER();
        }
    }[StructLayout(LayoutKind.Sequential)]
    public struct SP_DEVINFO_DATA
    {
        public int cbSize;
        public Guid ClassGuid;
        public int DevInst;
        public int Reserved;
    }[DllImport("setupapi.dll")]
    public static extern bool SetupDiEnumDeviceInfo(
        IntPtr DeviceInfoSet,
        int Supplies,
        ref SP_DEVINFO_DATA DeviceInfoData);[DllImport("setupapi.dll")]
    public static extern bool SetupDiSetClassInstallParams(
        IntPtr DeviceInfoSet,
        ref SP_DEVINFO_DATA DeviceInfoData,
        ref SP_CLASSINSTALL_HEADER ClassInstallParams,
        int ClassInstallParamsSize);[DllImport("setupapi.dll")]
    public static extern bool SetupDiCallClassInstaller(
        int InstallFunction,
        IntPtr DeviceInfoSet,
         ref SP_DEVINFO_DATA DeviceInfoData);static bool StateChange(int NewState, int SelectedItem, IntPtr hDevInfo)
    {
        SP_PROPCHANGE_PARAMS PropChangeParams;
        SP_DEVINFO_DATA DeviceInfoData;
        PropChangeParams = new SP_PROPCHANGE_PARAMS();
        PropChangeParams.Init();
        DeviceInfoData = new SP_DEVINFO_DATA();
        PropChangeParams.ClassInstallHeader.cbSize
            = Marshal.SizeOf(PropChangeParams.ClassInstallHeader);
        DeviceInfoData.cbSize = Marshal.SizeOf(DeviceInfoData);
        if (!SetupDiEnumDeviceInfo(hDevInfo, SelectedItem, ref DeviceInfoData))
            return false;
        
        PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
        PropChangeParams.Scope = DICS_FLAG_GLOBAL;
        PropChangeParams.StateChange = NewState;
        if (!SetupDiSetClassInstallParams(
            hDevInfo,
            ref DeviceInfoData,
            ref PropChangeParams.ClassInstallHeader,
            (int)Marshal.SizeOf(PropChangeParams)))
            return false;
        if (!SetupDiCallClassInstaller(
            DIF_PROPERTYCHANGE,
            hDevInfo,
            ref DeviceInfoData))
            return false;
        return true;
    }
      

  2.   

    谢谢,问题出在spPropChangeParam.StateChange = 0;所以返回参数不正确。改成 spPropChangeParam.StateChange = 1;就ok了