dongwen7423 2019-02-13 17:48
浏览 133

如何使流程所有者使用WMI请求

I try to receive the list of processes using go-ole library:


    package main

    import (
        "fmt"
        "github.com/go-ole/go-ole"
        "github.com/mattn/go-ole/oleutil"
    )


    func main() {
        ole.CoInitialize(0)
        defer ole.CoUninitialize()

        unknown, _ := oleutil.CreateObject("WbemScripting.SWbemLocator")
        defer unknown.Release()

        wmi, _ := unknown.QueryInterface(ole.IID_IDispatch)
        defer wmi.Release()

        serviceRaw, _ := oleutil.CallMethod(wmi, "ConnectServer")
        service := serviceRaw.ToIDispatch()
        defer service.Release()

        resultRaw, _ := oleutil.CallMethod(service, "ExecQuery", "SELECT * FROM Win32_Process")
        result := resultRaw.ToIDispatch()
        defer result.Release()

        countVar, _ := oleutil.GetProperty(result, "Count")
        count := int(countVar.Val)

        for i :=0; i < count; i++ {
            itemRaw, _ := oleutil.CallMethod(result, "ItemIndex", i)
            item := itemRaw.ToIDispatch()
            defer item.Release()

            processName, _ := oleutil.GetProperty(item, "Name")
            fmt.Println(processName.ToString())
        }
    }

but I cannot receive the owner of process, call method GetOwner

ownerRaw, _ := oleutil.CallMethod(item, "GetOwner")
fmt.Println(ownerRaw)

because GetOwner return value is int32


    uint32 GetOwner(
      [out] string User,
      [out] string Domain
    );

https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/getowner-method-in-class-win32-process

How it is possible to receive the owner as string for each process?

  • 写回答

1条回答 默认 最新

  • doulun1666 2019-02-21 22:42
    关注

    It took me a while to figure this out.

    The GetOwner method is expecting you to pass two string parameters. These strings will contain the results of the method call.

    var user ole.VARIANT
    var domain ole.VARIANT
    res, err := oleutil.CallMethod(item, "GetOwner", &user, &domain)
    fmt.Printf("user: %v\tdomain: %v", user.ToString(), domain.ToString())
    

    To get the SID you do the same thing:

     var sid ole.VARIANT
     res, err := oleutil.CallMethod(item, "GetOwnerSid", &sid)
     fmt.Printf("sid: %v
    ", sid.ToString())
    

    Hope this helps.

    Sopwith

    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?