2017-12-24 13:55 浏览 111

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 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 (
    func main() {
        ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED)
        defer ole.CoUninitialize()
        unknown, err := oleutil.CreateObject("WbemScripting.SWbemLocator")
        if err != nil {
        defer unknown.Release()
        wmi, err := unknown.QueryInterface(ole.IID_IDispatch)
        if err != nil {
        defer wmi.Release()
        // Connect to namespace
        // root/PanasonicPC = winmgmts:\\.oot\PanasonicPC
        serviceRaw, err := oleutil.CallMethod(wmi, "ConnectServer", nil, "root/PanasonicPC")
        if err != nil {
        service := serviceRaw.ToIDispatch()
        defer serviceRaw.Clear()
        // Interact with service object etc
    点赞 评论 复制链接分享