dongwen1871 2016-08-26 08:43
浏览 117
已采纳

在没有CGo的情况下从Go调用COM对象方法

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.

  • 写回答

2条回答 默认 最新

  • dongyi7966 2017-07-01 19:51
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?