栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

我问go-ole的家伙,就像@kostix建议的那样。

解决方法如下:

d3d9通常没有COM vtbl。例如,它没有IDispatch接口。因此,您不能对d3d9使用go-ole。但是您可以通过编写所有接口来实现。

package mainimport (    "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    CheckDeviceFormatuintptr    CheckDeviceMultiSampleType  uintptr    CheckDepthStencilMatch      uintptr    CheckDeviceFormatConversion uintptr    GetDeviceCaps    uintptr    GetAdapterMonitoruintptr    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)马特恩



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/436284.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号