3) 使用
setupapi.dll
setupapi.dll是流行的安装程序支持相关文件
setupapi.dll不能象前面两个通过Add Reference导入到工程中,只能使用DllImport
代码比较多,贴主要代码
[DllImport("setupapi.dll")]
public
static extern IntPtr
SetupDiGetClassDevsA(ref Guid ClassGuid, UInt32 Enumerator,
IntPtr
hwndParent, UInt32 Flags);
[DllImport("setupapi.dll")]
public
static extern IntPtr
SetupDiGetClassDevs(UInt32 ClassGuid, String e, IntPtr hwndParent, UInt32 Flags);
[DllImport("setupapi.dll")]
static
extern Boolean
SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, UInt32 MemberIndex,
ref
SP_DEVINFO_DATA DeviceInfoData);
…………
uint
NewNetStatus = 0;
if
(newStatus)
NewNetStatus = DICS_ENABLE;
else
NewNetStatus = DICS_DISABLE;
IntPtr
NewDeviceInfoSet;
SP_DEVINFO_DATA
spData = new SP_DEVINFO_DATA();
spData.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(spData);
UInt32
RequiredSize = 0;
byte
[] st1 = new byte[1024];
uint
Data = 0;
NewDeviceInfoSet = SetupDiGetClassDevs(0, "PCI", IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES);
bool
bFound = false;
for
(uint i = 0; SetupDiEnumDeviceInfo(NewDeviceInfoSet, i, ref spData); i++)
{
while
(!SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet, ref spData, SPDRP_HARDWAREID, ref Data, st1, 1024, ref RequiredSize))
{
}
string
str = System.Text.Encoding.ASCII.GetString(st1); ;
char
[] a ={ '\0' };
string
[] strSPlit = str.Split(a, StringSplitOptions.RemoveEmptyEntries);
string
HardId = @"PCI\VEN_10EC&DEV_8029&SUBSYS_00000000";
for
(uint j = 0; j < strSPlit.Length; j++)
{
if
(strSPlit[j] == HardId)
{
bFound = true;
break
;
}
}
if (bFound)
break;
}
SP_PROPCHANGE_PARAMS
spPropChangeParam = new SP_PROPCHANGE_PARAMS();
spPropChangeParam.Scope = DICS_FLAG_GLOBAL;
spPropChangeParam.StateChange = NewNetStatus;
spPropChangeParam.ClassInstallHeader.cbSize = (UInt32)System.Runtime.InteropServices.Marshal.SizeOf(spPropChangeParam.ClassInstallHeader);
spPropChangeParam.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
SetupDiSetClassInstallParams(NewDeviceInfoSet, ref spData, ref spPropChangeParam.ClassInstallHeader, System.Runtime.InteropServices.Marshal.SizeOf(spPropChangeParam));
SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, NewDeviceInfoSet, ref spData);
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);