VB.NET 获取驱动信息

在.NET框架中本身就提供了一套API接口,包含在System.Service

Process中你可能会有疑惑,驱动与系统服务是不一样的 对的它们

的确不一样、这样说吧、驱动是跟随系统最先启动的程序,当然这

个前提必须建立在你的驱动安%WINDIR%\[32/64]\Drivers中,

然后是系统组件包括系统服务两个部分、.NET可以开发系统服务但

不可以开发驱动这是因为,在驱动随系统映射时、CLR组件还没有

被系统映射、当然驱动与服务两个本身相当类似,我们需要调用

提供的服务于命令基本方式都差不到那里去、至于.NET框架上

ServiceController类提供的ExecuteCommand当然它不止是调用

服务命令且可以使用驱动命令、它包含一个命令参数且必须在128

~256之间,而且不存在返回值、着实有些令人摸不到头脑、当然通

是通过DeviceIoControl调用命令、在本文中通过EnumDeviceDri

vers、GetDeviceDriverBaseName获取到被映射的驱动程序、通过

这两个函数可以更快更节省资源的获取到我们想要的信息 如果需要

获取到复杂信息 那可以通过ServiceController.GetDevices()获取

当然这或许会是个好办法


外部函数注解:

// 枚举驱动设备映像

Declare Sub EnumDeviceDrivers Lib "psapi.dll" (ByVal lpImageBase As IntPtr(),// 欲获取驱动映像地址缓冲区

ByVal cbSize As Integer,// 欲获取驱动映像地址缓冲区长度
ByRef lpcbNeeded As Integer) // 欲获取驱动映像地址缓冲区尺寸

// 取驱动设备基类名

Declare Sub GetDeviceDriverBaseName Lib "psapi.dll" Alias "GetDeviceDriverBaseNameA" (

ByVal ImageBase As IntPtr,// 欲获取信息的驱动映像地址

ByVal lpBaseName As String,// 欲获取驱动信息的字符缓冲区
ByVal nSize As Integer) // 欲获取驱动信息的字符缓冲区尺寸

// 取驱动设备文件

Declare Sub GetDeviceDriverFileName Lib "psapi.dll" Alias "GetDeviceDriverFileNameA" (

ByVal ImageBase As IntPtr,// 欲获取信息的驱动映像地址
ByVal lpBaseName As String,// 欲获取驱动信息的字符缓冲区
ByVal nSize As Integer)// 欲获取驱动信息的字符缓冲区尺寸

示例代码

Imports System.Runtime.InteropServices

Module MainModule

    Declare Sub EnumDeviceDrivers Lib "psapi.dll" (ByVal lpImageBase As IntPtr(),ByVal cbSize As Integer,ByRef lpcbNeeded As Integer)

    Declare Sub GetDeviceDriverBaseName Lib "psapi.dll" Alias "GetDeviceDriverBaseNameA" (ByVal ImageBase As IntPtr,ByVal lpBaseName As String,ByVal nSize As Integer)

    Declare Sub GetDeviceDriverFileName Lib "psapi.dll" Alias "GetDeviceDriverFileNameA" (ByVal ImageBase As IntPtr,ByVal nSize As Integer)

    Sub Main()
        Dim cbSize = vbNull,cbNeeded As Integer
        Dim pbaseAddr As IntPtr() = nothing
        Do
            cbSize = cbSize * 2
            ReDim pbaseAddr(cbSize)
            EnumDeviceDrivers(pbaseAddr,cbSize,cbNeeded)
        Loop While cbNeeded > cbSize
        cbSize = (cbNeeded / IntPtr.Size) - 1
        For i = 0 To cbSize Step 1
            Dim strBaseName As String = Space(8096)
            GetDeviceDriverBaseName(pbaseAddr(i),strBaseName,8096)
            Console.WriteLine(strBaseName.TrimEnd() & pbaseAddr(i).ToString("X"))
        Next
        Console.ReadKey(False)
    End Sub

End Module

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...