Using APIs to implement C # hardware control (I)

Source: Internet
Author: User

Recently, in the project process, it involves enabling and disabling hardware (hardware can be enabled within a specified period of time ). Because C #'s hardware operations are basically zero, so I searched for the device-related API functions in msdn. There is indeed. The following lists the prototype of each function and related instructions. And convert it into C # code implementation.

Function 1: registerdevicenotification
Function: registers a device or device type. Related Information is returned in the specified window.

Prototype:

Hdevnotify winapi registerdevicenotification (
_ In handle hrecipient,
_ In lpvoid icationicationfilter,
_ In DWORD flags
);

Reference: http://msdn2.microsoft.com/en-us/library/aa363431.aspx.

The code after converting to C # is:

[Dllimport ("user32.dll", charset = charset. Auto)]

Public static extern intptr registerdevicenotification (intptr
Hrecipient, dev_broadcast_deviceinterface icationicationfilter, uint32
Flags );
[Structlayout (layoutkind. Sequential)]
Public class dev_broadcast_deviceinterface
...{
Public int dbcc_size;
Public int dbcc_devicetype;
Public int dbcc_reserved;
}

Function 2: unregisterdevicenotification
Function: disables the information of a specified device through the name handle. (Mainly used to clear unmanaged resources and pair them with registerdevicenotification)

Prototype:

Bool winapi unregisterdevicenotification (
_ In hdevpolicy handle
);

Reference documentation: http://msdn2.microsoft.com/en-us/library/aa363475 (vs.85). aspx.

Code converted to C:

[Dllimport ("user32.dll", charset = charset. Auto)]
Public static extern uint unregisterdevicenotification (intptr hhandle );

Function 3: setupdigetclassdevs
Function: obtains information about all installed devices of a specified category or all categories.

Prototype:

Hdevinfo setupdigetclassdevs (in lpguid classguid, optional
In pctstr enumerator, optional in hwnd hwndparent, optional
In DWORD flags );
Reference: http://msdn2.microsoft.com/en-us/library/ms792959.aspx.

Code converted to C:

[Dllimport ("setupapi. dll", setlasterror = true)]
Public static extern intptr setupdigetclassdevs (ref guid gclass, uint32 ienumerator, intptr hparent, uint32 nflags );

Function 4: setupdidestroydeviceinfolist
Function: destroys a collection of device information and releases all associated memory.

Prototype:

Winsetupapi bool winapi setupdidestroydeviceinfolist (in hdevinfo deviceinfoset );
Reference: http://msdn2.microsoft.com/en-us/library/ms792991.aspx.

Code converted to C:

[Dllimport ("setupapi. dll", setlasterror = true)]
Public static extern int setupdidestroydeviceinfolist (intptr lpinfoset );

Function 5: setupdienumdeviceinfo
Function: enumerate the members of a specified device information set and place the data in sp_devinfo_data.

Prototype:

Winsetupapi bool winapi setupdienumdeviceinfo (
In hdevinfo deviceinfoset,
In DWORD memberindex,
Out psp_devinfo_data deviceinfodata );
Reference: http://msdn2.microsoft.com/en-us/library/ms792983.aspx.

Code converted to C:

[Dllimport ("setupapi. dll", setlasterror = true)]
Public static extern bool setupdienumdeviceinfo (intptr lpinfoset, uint32 dwindex, sp_devinfo_data devinfodata );
/** // <Summary>
/// Device information data
/// </Summary>
[Structlayout (layoutkind. Sequential)]
Public class sp_devinfo_data
...{
Public int cbsize;
Public guid classguid;
Public int devinst;
Public ulong reserved;
};

Function 6: setupdigetdeviceregistryproperty
Function: gets the properties of a specified device.

Prototype:

Winsetupapi bool winapi setupdigetdeviceregistryproperty (
In hdevinfo deviceinfoset,
In psp_devinfo_data deviceinfodata,
In DWORD property,
Out pdword propertyregdatatype,
Optional out pbyte propertybuffer,
In DWORD propertybuffersize,
Out pdword requiredsize optional );
Reference: http://msdn2.microsoft.com/en-us/library/ms792967.aspx.

Code converted to C:

[Dllimport ("setupapi. dll", setlasterror = true)]

Public static extern bool setupdigetdeviceregistryproperty (intptr
Lpinfoset, sp_devinfo_data deviceinfodata, uint32 property, uint32
Propertyregdatatype, stringbuilder propertybuffer, uint32
Propertybuffersize, intptr requiredsize );

Function 7: setupdisetclassinstallparams
Function: Disable a device.

Prototype:

Winsetupapi bool winapi
Setupdisetclassinstallparams (
In hdevinfo deviceinfoset,
In psp_devinfo_data deviceinfodata,
Optional in psp_classinstall_header classinstallparams,
Optional in DWORD classinstallparamssize );
Reference: http://msdn2.microsoft.com/en-us/library/ms792876.aspx.

Code converted to C:

[Dllimport ("setupapi. dll", setlasterror = true, charset = charset. Auto)]

Public static extern bool setupdisetclassinstallparams (intptr
Deviceinfoset, intptr deviceinfodata, intptr classinstallparams, int
Classinstallparamssize );

Function 8: setupdicallclassinstaller
Function: Enable the device.

Prototype:

Winsetupapi bool winapi
Setupdicallclassinstaller (
In di_function installfunction,
In hdevinfo deviceinfoset,
In psp_devinfo_data deviceinfodata optional );
Reference: http://msdn2.microsoft.com/en-us/library/ms792989.aspx.

Code converted to C:

[Dllimport ("setupapi. dll", charset = charset. Auto)]

Public static extern Boolean setupdicallclassinstaller (uint32
Installfunction, intptr deviceinfoset, intptr deviceinfodata );

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.