Судя по некоторым источникам (
http://groups.google.com/group/micro...06e4fc859a3f86) Реализуется нижеприведенным кодом. Этот кусок максимально приближен к примеру из MSDN.
Только вот одна проблемка есть (может кто сможет сказать где собака порылась...):
падает на строке
Код:
hdi := SetupDiGetClassDevs(@GUID_DEVCLASS_NET, nil, 0, DIGCF_PRESENT);
EAccessViolation "Access violation at address 000000. Read of address 00000."
Более того, падает даже если заменить эту строчку на
Этот кусок кода взят из MSDN
http://msdn.microsoft.com/library/de...ed_devices.asp)
Код:
hdi := SetupDiGetClassDevs(nil, nil, 0, DIGCF_PRESENT or DIGCF_ALLCLASSES);
Полный код проги:
Код:
unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,Forms, Dialogs, StdCtrls, SetupAPI;
const GUID_DEVCLASS_NET: TGUID = '{4D36E972-E325-11CE-BFC1-08002BE10318}';
type TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var Form1: TForm1;
implementation
{$R *.dfm}
function StateChange(NewState, SelectedItem: cardinal; hDevInfo:HDEVINFO): boolean;
var PropChangeParams: SP_PROPCHANGE_PARAMS;
DeviceInfoData: SP_DEVINFO_DATA;
begin
DeviceInfoData.cbSize := SizeOf(SP_DEVINFO_DATA);
// Get handle to selected item
if not SetupDiEnumDeviceInfo(hDevInfo, SelectedItem, DeviceInfoData) then
begin
Result := False;
Exit;
end;
// Set the PropChangeParams structure for this item
PropChangeParams. ClassInstallHeader.InstallFunction :=DIF_PROPERTYCHANGE;
PropChangeParams.ClassInstallHeader.cbSize :=SizeOf(SP_CLASSINSTALL_HEADER);
PropChangeParams.Scope := DICS_FLAG_GLOBAL; PropChangeParams.StateChange := NewState;
if not SetupDiSetClassInstallParams(hDevInfo, @DeviceInfoData,PSPCLASSINSTALLHEADER(@PropChangeParams), SizeOf(PropChangeParams)) then
begin
Result := False;
Exit;
end;
// Call the ClassInstaller and perform the change
if not SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, @DeviceInfoData) then
begin
Result := False;
Exit;
end;
Result := True;
end;
function DisableNetAdapter: boolean;
var hdi: HDEVINFO;
begin
hdi := SetupDiGetClassDevs(@GUID_DEVCLASS_NET, nil, 0, DIGCF_PRESENT);
if cardinal(hdi) = INVALID_HANDLE_VALUE then
begin
Result := False;
end
else
begin
Result := StateChange(DICS_DISABLE, 0, hdi);
SetupDiDestroyDeviceInfoList(hdi);
end;
end;
function EnableNetAdapter: boolean;
var hdi: HDEVINFO;
begin
hdi := SetupDiGetClassDevs(@GUID_DEVCLASS_NET, nil, 0, DIGCF_PRESENT);
if cardinal(hdi) = INVALID_HANDLE_VALUE then
begin
Result := False;
end
else
begin
Result := StateChange(DICS_ENABLE, 0, hdi);
SetupDiDestroyDeviceInfoList(hdi);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
EnableNetAdapter;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
DisableNetAdapter;
end;
end.