dongnaota6386 2017-12-24 13:55
浏览 116

Golang WMI查询XP

I'm using mattn's go-ole package to connect to WMI, I got this working using some examples I found on stack and all is fine until I try to make it work in Win XP. The Microsoft documentation says thatWbemScripting.SWbemLocator is only available for Vista so I've been trying the winmgmts method that Microsoft shows for use on VBScript but i'm not getting anywhere.

Here is what I have.

This works on Win 7:

wmiscriptObj, err := oleutil.CreateObject("WbemScripting.SWbemLocator")

This doesn't work in either XP or 7:

wmiscriptObj, err := oleutil.CreateObject("winmgmts:\\\\.\oot\\cimv2")

I get invalid class as an error.

Not sure if this helps explain things but in the past I've used Autoit to query WMI on XP and I can connect with this:

$objWMIService = ObjGet("winmgmts:\\" & $ComputerName & "oot\CIMV2")

Any help would be much appreciated.

  • 写回答

1条回答 默认 最新

  • dream890110 2018-11-06 11:01
    关注

    Stumbled upon this question and wanted to provide an answer.

    To connect to a namespace / winmgmts, you need to execute the Connect - method using the badly documented namespace argument.

    Sample code package main

    import (
        "github.com/go-ole/go-ole"
        "github.com/go-ole/go-ole/oleutil"
    )
    
    func main() {
        ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED)
        defer ole.CoUninitialize()
    
        unknown, err := oleutil.CreateObject("WbemScripting.SWbemLocator")
        if err != nil {
            log.Panic(err)
        }
        defer unknown.Release()
    
        wmi, err := unknown.QueryInterface(ole.IID_IDispatch)
        if err != nil {
            log.Panic(err)
        }
        defer wmi.Release()
    
        // Connect to namespace
        // root/PanasonicPC = winmgmts:\\.oot\PanasonicPC
        serviceRaw, err := oleutil.CallMethod(wmi, "ConnectServer", nil, "root/PanasonicPC")
        if err != nil {
            log.Panic(err)
        }
        service := serviceRaw.ToIDispatch()
        defer serviceRaw.Clear()
    
        // Interact with service object etc
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn