从 go 调用函数指针。 (Windows 管理基础设施客户端)

问题描述 投票:0回答:1

我为 Windows 管理基础设施 (MI) 开发了 go pure go 客户端。它是 WMI 的继承者。

初始化时缩进工作

package main

import (
    "unsafe"

    "golang.org/x/sys/windows"
)

var (
    modMi = windows.NewLazySystemDLL("mi.dll")

    procMIApplicationInitialize = modMi.NewProc("MI_Application_InitializeV1")
)

type MI_Application struct {
    reserved1 uint64
    reserved2 uintptr
    ft        *MI_ApplicationFT
}

type MI_ApplicationFT struct {
    Close                          uintptr
    NewSession                     uintptr
    NewHostedProvider              uintptr
    NewInstance                    uintptr
    NewDestinationOptions          uintptr
    NewOperationOptions            uintptr
    NewSubscriptionDeliveryOptions uintptr
    NewSerializer                  uintptr
    NewDeserializer                uintptr
    NewInstanceFromClass           uintptr
    NewClass                       uintptr
}

// MI_Application_Initialize initializes the MI application.
// https://docs.microsoft.com/en-us/windows/win32/api/miapi/nf-miapi-mi_application_initializev1
// It is recommended to have only one MI_Application per process.
func MI_Application_Initialize() (*MI_Application, uint64) {
    flags := uint32(0)

    application := &MI_Application{}

    r0, _, _ := procMIApplicationInitialize.Call(
        uintptr(flags), 0, 0, uintptr(unsafe.Pointer(application)),
    )

    return application, uint64(r0)
}

func main() {
    app, statusCode := MI_Application_Initialize()

    if statusCode != 0 {
        panic("Failed to initialize MI application")
    }

    if app.ft == nil {
        panic("MI_Application is not initialized")
    }
}

我在设置新会话时遇到一些问题。像 MI_Application_NewSession 这样的函数被称为 header 函数,我不知道如何从 go 中调用此类函数指针。

我尝试通过系统调用接口调用该函数,但结果违反了访问策略

r0, _, _ := syscall.SyscallN(application.ft.NewSession,
        7,
        uintptr(unsafe.Pointer(application)),
        uintptr(unsafe.Pointer(protocolUTF16)),
        0,
        0,
        0,
        0,
        uintptr(unsafe.Pointer(session)),
    )

如何从 go 中调用此类函数指针?我正在寻找一个纯粹的解决方案。

windows go winapi system-calls cgo
1个回答
0
投票

在这种情况下,

syscall.SyscallN
效果很好。我把前面的7去掉了。

© www.soinside.com 2019 - 2024. All rights reserved.