在Golang中使用WMI类的参数调用方法

时间:2018-11-23 05:06:01

标签: go wmi

我正试图调用WmiMonitorBrightnessMethods类的WmiSetBrightness方法:

https://docs.microsoft.com/en-us/windows/desktop/wmicoreprov/wmisetbrightness-method-in-class-wmimonitorbrightnessmethods

借助https://github.com/go-ole/go-ole和来自Calling a method of a WMI Class的示例代码

我不知道为什么它不断返回错误:

wmi: error calling method WmiSetBrightness: Exception occurred. (Invalid method Parameter(s) )

我已经搜索了一段时间,但仍然找不到正确的方法来传递两个参数Timeout和Brightness。

我的代码如下:

func main() {
    err := ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED)
    if err != nil {
        log.Fatal(err)
    }

    unknown, err := oleutil.CreateObject("WbemScripting.SWbemLocator")
    if err != nil {
        log.Fatal(err)
    }
    defer unknown.Release()

    wmi, err := unknown.QueryInterface(ole.IID_IDispatch)
    if err != nil {
        log.Fatal(err)
    }
    defer wmi.Release()

    serviceRaw, err := oleutil.CallMethod(wmi, "ConnectServer", nil, `root/WMI`)
    if err != nil {
        log.Fatal(err)
    }
    service := serviceRaw.ToIDispatch()
    defer serviceRaw.Clear()

    typeName := "WmiMonitorBrightnessMethods"
    typeRaw, err := oleutil.CallMethod(service, "Get", typeName)
    if err != nil {
        log.Fatalf("wmi: error fetching type %v: %v", typeName, err)
    }
    typeResult := typeRaw.ToIDispatch()
    defer typeRaw.Clear()

    methodName := "WmiSetBrightness"
    // Set Brightness value to 20 with the Timeout 1
    methodRaw, err := oleutil.CallMethod(typeResult, methodName, 1, 20)
    if err != nil {
        log.Fatalf("wmi: error calling method %v: %v", methodName, err)
    }
    item := methodRaw.ToIDispatch()
    defer methodRaw.Clear()
    _ = item
}

0 个答案:

没有答案