在没有CGo的情况下从Go调用COM对象方法 [英] Call COM object method from Go without CGo

查看:177
本文介绍了在没有CGo的情况下从Go调用COM对象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Go中创建了一个 Direct3D9包装器,它使用CGo与C中的COM对象进行交互。

我想摆脱Windows下C编译器的依赖,因此用户不必安装MinGW或Cygwin即可使用Go中的DirectX。 p>

问题是d3d9.dll不公开C函数,但使用COM。加载DLL(使用 syscall.LoadLibrary(d3d9.dll))后可以直接调用的唯一函数是 Direct3DCreate9 。这将返回一个COM对象,它将所有功能公开为方法。



如何在没有CGo的情况下从纯Go中调用DLL中的COM对象方法? b

我知道 Go-OLE 资料库,其中声明了它调用没有CGo的COM接口,但我不能从源头上看到我将如何去为Direct3D9做同样的事情。

解决方案

我问过go-ole的家伙,就像@



以下是解决方案:


d3d9没有一般是COM vtbl。例如,它没有
IDispatch接口。所以你不能使用d3d9的go-ole。但是你可以通过编写所有接口来完成



 包主

导入(
fmt
log
系统调用
不安全


常量(
D3D9_SDK_VERSION = 32


var(
libd3d9 = syscall.NewLazyDLL(d3d9.dll)
procDirect3DCreate9 = libd3d9.NewProc(Direct3DCreate9)


type IDirect3D struct {
lpVtbl * IDirect3DVtbl
}

type IDirect3DVtbl struct {
QueryInterface uintptr
AddRef uintptr
发布uintptr

RegisterSoftwareDevice uintptr
GetAdapterCount uintptr
GetAdapterIdentifier uintptr
GetAdapterModeCount uintptr
EnumAdapterModes uintptr
GetAdapterDisplayMode uintptr
CheckDeviceType uintptr
CheckDeviceFormat uintptr
CheckDeviceMultiSampleType uintptr
CheckDept hStencilMatch uintptr
CheckDeviceFormatConversion uintptr
GetDeviceCaps uintptr
GetAdapterMonitor uintptr $ b $ CreateDevice uintptr

$ b func(v * IDirect3D)AddRef()int32 {
ret,_,_:= syscall.Syscall(
v.lpVtbl.AddRef,
1,
uintptr(unsafe.Pointer(v)),
0 ,
0)
return int32(ret)
}

func(v * IDirect3D)Release()int32 {
ret,_,_: = syscall.Syscall(
v.lpVtbl.Release,
1,
uintptr(unsafe.Pointer(v)),
0,
0)
返回int32(ret)
}

func(v * IDirect3D)GetAdapterCount()uint32 {
ret,_,_:= syscall.Syscall(
v .lpVtbl.GetAdapterCount,
1,
uintptr(unsafe.Pointer(v)),
0,
0)
return uint32(ret)
}

func main(){
v,r,err := procDirect3DCreate9.Call(uintptr(D3D9_SDK_VERSION))
if r!= 0&& err!= nil {
log.Fatal(err)
}
d3d:= *((** IDirect3D)(unsafe.Pointer(& v)))

d3d.AddRef()
defer d3d.Release()

fmt.Println(d3d.GetAdapterCount())
}

(c)mattn


I have created a Direct3D9 wrapper in Go which uses CGo to interface with the COM objects in C.

I would like to get rid of the dependency on a C-compiler under Windows so the user would not have to install MinGW or Cygwin to use DirectX from Go.

The problem is that d3d9.dll does not expose C-functions but uses COM. The only function that can be called directly after loading the DLL (with syscall.LoadLibrary("d3d9.dll")) is Direct3DCreate9. This returns a COM object which exposes all functionality as methods.

How can I call COM object methods in a DLL from pure Go without CGo?

I know of the Go-OLE library which states it calls COM interfaces without CGo but I cannot, from the sources, see how I would go about doing the same thing for Direct3D9. A simple example with only the relevant parts would be of great help.

解决方案

I asked the guys from go-ole, like @kostix suggested.

Here is the solution:

d3d9 doesn't have generally COM vtbl. for example, it doesn't have IDispatch interface. So you can't use go-ole for d3d9. But you can do it with writing all interface in go.

package main

import (
    "fmt"
    "log"
    "syscall"
    "unsafe"
)

const (
    D3D9_SDK_VERSION = 32
)

var (
    libd3d9             = syscall.NewLazyDLL("d3d9.dll")
    procDirect3DCreate9 = libd3d9.NewProc("Direct3DCreate9")
)

type IDirect3D struct {
    lpVtbl *IDirect3DVtbl
}

type IDirect3DVtbl struct {
    QueryInterface uintptr
    AddRef         uintptr
    Release        uintptr

    RegisterSoftwareDevice      uintptr
    GetAdapterCount             uintptr
    GetAdapterIdentifier        uintptr
    GetAdapterModeCount         uintptr
    EnumAdapterModes            uintptr
    GetAdapterDisplayMode       uintptr
    CheckDeviceType             uintptr
    CheckDeviceFormat           uintptr
    CheckDeviceMultiSampleType  uintptr
    CheckDepthStencilMatch      uintptr
    CheckDeviceFormatConversion uintptr
    GetDeviceCaps               uintptr
    GetAdapterMonitor           uintptr
    CreateDevice                uintptr
}

func (v *IDirect3D) AddRef() int32 {
    ret, _, _ := syscall.Syscall(
        v.lpVtbl.AddRef,
        1,
        uintptr(unsafe.Pointer(v)),
        0,
        0)
    return int32(ret)
}

func (v *IDirect3D) Release() int32 {
    ret, _, _ := syscall.Syscall(
        v.lpVtbl.Release,
        1,
        uintptr(unsafe.Pointer(v)),
        0,
        0)
    return int32(ret)
}

func (v *IDirect3D) GetAdapterCount() uint32 {
    ret, _, _ := syscall.Syscall(
        v.lpVtbl.GetAdapterCount,
        1,
        uintptr(unsafe.Pointer(v)),
        0,
        0)
    return uint32(ret)
}

func main() {
    v, r, err := procDirect3DCreate9.Call(uintptr(D3D9_SDK_VERSION))
    if r != 0 && err != nil {
        log.Fatal(err)
    }
    d3d := *((**IDirect3D)(unsafe.Pointer(&v)))

    d3d.AddRef()
    defer d3d.Release()

    fmt.Println(d3d.GetAdapterCount())
}

(c) mattn

这篇关于在没有CGo的情况下从Go调用COM对象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆