Does the program read the status (enable/disable) of each device in the computer Device Manager )? (From monopoly)

Source: Internet
Author: User
This section Code Add comments.

Const
Dev_class_name = 'net ';
Dem_class_name = 'Media ';
Dec_class_name = 'cdrom ';
Deu_class_name = 'usb ';
Dep_class_namw = 'ports ';
Unknown_device = '<Unknown device> ';

Private
{Private Declarations}
Devstate: array of Boolean;
Procedure refreshdevstate;

Procedure enumnetdevice; // obtain the NIC Information
Function isclasshidden (const classguid: pguid): Boolean; // called by enumnetdevice
Function constructdevicename (deviceinfoset: hdevinfo; // called by enumnetdevice
Deviceinfodata: psp_devinfo_data; Buffer: pansichar; Length: ulong): Boolean;
Function getregistryproperty (deviceinfoset: hdevinfo; // called by enumnetdevice
Deviceinfodata: psp_devinfo_data; aproperty: ulong; Buffer: pansichar;
Length: ulong): Boolean;
Function isdevdisable (devindex: DWORD; hdevinfo: hdevinfo): Boolean; // called by enumnetdevice
Function changedevstate (devindex, newstate: DWORD): bool; // change the NIC status
Public
{Public declarations}
End;

VaR
Devform: tdevform;

Implementation

{$ R *. DFM}

Procedure tdevform. enumnetdevice; // obtain the NIC Information
VaR
Deviceinfoset: hdevinfo;
Deviceinfodata: sp_devinfo_data;
I: integer;
Status, problem: DWORD;
Classname: pchar;
Classsize, reqclasssize: DWORD;
Devicename: pchar;
Begin
Clbdevlist. Clear;

Deviceinfoset: = setupdigetclassdevs (nil, nil, 0, digcf_allclasses or digcf_present );
If deviceinfoset = pointer (invalid_handle_value) then
Exit;
Classsize: = 255;
Getmem (classname, 256 );
Try
Deviceinfodata. cbsize: = sizeof (sp_devinfo_data); // enumerate hardware and obtain the required interface

I: = 0;
While setupdienumdeviceinfo (deviceinfoset, I, @ deviceinfodata) Do
Begin
INC (I );

If not setupdiclassnamefromguid (@ deviceinfodata. classguid, classname, classsize,
@ Reqclasssize) then
Begin
If reqclasssize> classsize then
Begin
Freemem (classname );
Classsize: = reqclasssize;
Getmem (classname, classsize + 1 );
If not setupdiclassnamefromguid (@ deviceinfodata. classguid, classname, classsize,
@ Reqclasssize) then
Exit;
End
Else
Exit;
End;

If not sametext (classname, dev_class_name) and not sametext (classname, dem_class_name) and
Not sametext (classname, dec_class_name) and not sametext (classname, deu_class_name) and
Not sametext (classname, dep_class_namw) then
Continue;

If cm_get_devnode_status (@ status, @ problem, deviceinfodata. devinst, 0)
<> Cr_success then
Exit;

If (Status and dn_no_show_in_dm) <> 0) or
Isclasshidden (@ deviceinfodata. classguid) then
Continue;

Getmem (devicename, 256); // allocate 256 of the memory to the devicename variable
Zeromemory (devicename, 256); // assign a value of zero to each unit of devicename
Constructdevicename (deviceinfoset, @ deviceinfodata, devicename, 255); // obtain the value of devicename
If isdevdisable (I-1, deviceinfoset) then
Begin
Clbdevlist. Items. addobject (strpas (devicename) + '-----' + classname, tobject (I-1); // Add the value of devicename to the I-1 line in the list
Clbdevlist. Checked [clbdevlist. Count-1]: = isdevdisable (I-1, deviceinfoset); // The value of checked is true
End;
Freemem (devicename); // release the memory occupied by devicename
End;
Finally
Freemem (classname );
Setupdidestroydeviceinfolist (deviceinfoset );
End;
End;

Function tdevform. constructdevicename (deviceinfoset: hdevinfo; // construct the value of devicename
Deviceinfodata: psp_devinfo_data; Buffer: pansichar;
Length: ulong): Boolean;
Begin
Result: = true;

If not getregistryproperty (deviceinfoset, deviceinfodata, spdrp_friendlyname,
Buffer, length) then
Begin
If not getregistryproperty (deviceinfoset, deviceinfodata, spdrp_devicedesc,
Buffer, length) then
Begin
If not getregistryproperty (deviceinfoset, deviceinfodata, spdrp_class,
Buffer, length) then
Begin
If not getregistryproperty (deviceinfoset, deviceinfodata, spdrp_classguid,
Buffer, length) then
Begin
Strcopy (buffer, unknown_device); // obtain the value of devicename as 'unknown device'
End
Else
Result: = false;
End;
End;
End;
End;

Function tdevform. getregistryproperty (deviceinfoset: hdevinfo;
Deviceinfodata: psp_devinfo_data; aproperty: ulong; Buffer: pansichar;
Length: ulong): Boolean;
VaR
Reqlen: DWORD;
Begin
Result: = false;

while not setupdigetdeviceregistryproperty (deviceinfoset, deviceinfodata,
aproperty, nil, buffer, length, @ reqlen) DO
begin
If getlasterror () = error_invalid_data then
Break
else
If getlasterror () = error_insufficient_buffer then
begin
if assigned (buffer) then
freemem (buffer);
length: = reqlen;
getmem (buffer, Length + 1 );
end
else
exit;
end;
result: = buffer ^ <> #0;
end;

Function tdevform. isclasshidden (const classguid: pguid): Boolean;
VaR
Hkeyclass: hkey;
Begin
Result: = false;

Hkeyclass: = setupdiopenclassregkey (classguid, key_read); // obtain the key value
If hkeyclass <> 0 then
Begin
Result: = regqueryvalueex (hkeyclass, regstr_val_nodisplayclass, nil) = error_success; // find the key value and return a Boolean value.
Regclosekey (hkeyclass );
End;
End;

Function tdevform. isdevdisable (devindex: DWORD;
Hdevinfo: hdevinfo): Boolean;
VaR
Deviceinfodata: sp_devinfo_data;
Status, problem: DWORD;
Begin
Result: = false;
Deviceinfodata. cbsize: = sizeof (sp_devinfo_data );

If not setupdienumdeviceinfo (hdevinfo, devindex, @ deviceinfodata) then
Exit;

If cm_get_devnode_status (@ status, @ problem, deviceinfodata. devinst, 0) <> cr_success then
Exit;

Result: = (Status and dn_disableable) <> 0) and (cm_prob_hardware_disabled <> problem );
End;

Function tdevform. changedevstate (devindex, newstate: DWORD): bool; // change the drive status
VaR // devindex: Drive index newstate: Enable of NIC
Deviceinfoset: hdevinfo; // disable; Disabled
Deviceinfodata: sp_devinfo_data;
Propchangeparams: sp_propchange_params;
Cursor: hcursor;
Begin
Result: = false;

Deviceinfoset: = setupdigetclassdevs (nil, nil, 0, digcf_allclasses or digcf_present); // get the pointer to the current drive
If deviceinfoset = pointer (invalid_handle_value) then
Exit;

Try
Propchangeparams. classinstallheader. cbsize: = sizeof (sp_classinstall_header );
Deviceinfodata. cbsize: = sizeof (sp_devinfo_data );

Cursor: = setcursor (loadcursor (0, idc_wait ));

If not setupdienumdeviceinfo (deviceinfoset, devindex, @ deviceinfodata) then
Exit;

///// You can restart, stop, and start the device.
Propchangeparams. classinstallheader. installfunction: = dif_propertychange;
Propchangeparams. Scope: = dics_flag_global;
Propchangeparams. statechange: = newstate;

If not setupdisetclassinstallparams (deviceinfoset, @ deviceinfodata,
@ Propchangeparams, sizeof (propchangeparams) then
Exit;

If not setupdicallclassinstaller (dif_propertychange, deviceinfoset,
@ Deviceinfodata) then
Exit;

Setcursor (cursor );
Result: = true;
Finally
Setupdidestroydeviceinfolist (deviceinfoset );
End;
End;

Procedure tdevform. formcreate (Sender: tobject );
Begin
Btrefresh. Click;
End;

Procedure tdevform. btexitclick (Sender: tobject );
Begin
Close;
End;

procedure tdevform. btapplyclick (Sender: tobject); // application settings
var
I: integer;
begin
for I: = 0 to clbdevlist. count-1 do
begin
If clbdevlist. checked [I] <> devstate [I] Then // when the new status of the NIC is different from the original status, it determines whether the new status of the NIC is selected
begin
If clbdevlist. checked [I] Then
changedevstate (Cardinal (clbdevlist. items. objects [I]), dics_enable) // enable the NIC (Cardinal 32-bit unsigned count)
else
changedevstate (Cardinal (clbdevlist. items. objects [I]), dics_disable); // disable the NIC
end;
refreshdevstate;
end;

Procedure tdevform. refreshdevstate;
VaR
I: integer;
Begin
Setlength (devstate, clbdevlist. Count );
For I: = 0 to clbdevlist. Count-1 do
Devstate [I]: = clbdevlist. Checked [I];
End;

Procedure tdevform. btrefreshclick (Sender: tobject); // obtain the NIC information and store it in the Boolean array of D E V S T A T E
Begin
Enumnetdevice;
Refreshdevstate;
End;

End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.