|
分類:[VB.NET/VB2005 以降]
VB2008で開発しています。
タイトルのエラーが出るのですがどうすればよいのかわかりません。
コード Public Function GetPNPDeviceID(ByVal strDriveLetter As String) As String GetPNPDeviceID = Nothing Try Dim objWMIService = GetObject("winmgmts:\\.\root\cimv2") Dim objDrive Dim colDiskDrives = objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive") For Each objDrive In colDiskDrives Dim objPartition Dim colPartitions = objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" + Replace(objDrive.DeviceID, "\", "\\") + """} WHERE AssocClass =Win32_DiskDriveToDiskPartition") For Each objPartition In colPartitions Dim objLogicalDisk Dim colLogicalDisks = objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" + objPartition.DeviceID & """} WHERE AssocClass = Win32_LogicalDiskToPartition") For Each objLogicalDisk In colLogicalDisks If CStr(objLogicalDisk.DeviceID) = strDriveLetter + ":" Then Dim varPNPDeviceID = Split(CStr(objDrive.PNPDeviceID), "\") GetPNPDeviceID = varPNPDeviceID(UBound(varPNPDeviceID)) Exit For End If Next Next Next Catch ex As Exception MsgBox(ex.Message) End Try End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) If WM_DEVICECHANGE = m.Msg Then Dim dbv As New DEV_BROADCAST_VOLUME dbv = Marshal.PtrToStructure(m.LParam, GetType(DEV_BROADCAST_VOLUME)) If dbv.dbcv_devicetype = DBT_DEVICETYPE.DBT_DEVICETYPE_VOLUME Then Dim drv = getDriveString(dbv.dbcv_unitmask) Select Case m.WParam.ToInt32 Case DBT.DBT_DEVICEARRIVAL MsgBox(drv + ": を取り付けた!" + vbCrLf + GetPNPDeviceID(drv)) Case DBT.DBT_DEVICEREMOVECOMPLETE MsgBox(drv + ": を取り外した!" + vbCrLf + GetPNPDeviceID(drv)) End Select End If End If MyBase.WndProc(m) End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click MsgBox(GetPNPDeviceID("E")) End Sub
一部省略してますが Button2_ClickのMsgBoxで実行するとシリアルIDが取得できるのですが WndProcの状態で取得しようとすると Dim objWMIService = GetObject("winmgmts:\\.\root\cimv2")の部分で 「ActiveXコンポーネントが作成できません」 となります。
|