Skip to content
qiannian edited this page Aug 19, 2023 · 3 revisions

Golang

这里是 go 语言的示例

Code

package main

import (
	"os"
	"path/filepath"
	"syscall"
	"unsafe"

    "github.com/go-ole/go-ole"
	"github.com/go-ole/go-ole/oleutil"
)

var (
	op      *syscall.LazyDLL
	_setupA *syscall.LazyProc
	_setupW *syscall.LazyProc
)

func init(){
    // tool.dll路径
    op = syscall.NewLazyDLL("./tools_64.dll")
    _setupA = op.NewProc("setupA")
	_setupW = op.NewProc("setupW")
}

func SetupA(path string) bool {
	var _p0 *uint16
	_p0, _ = syscall.UTF16PtrFromString(path)
	ret, _, _ := _setupA.Call(uintptr(unsafe.Pointer(_p0)))
	return ret != 0
}

func SetupW(path string) bool {
	var _p0 *uint16
	_p0, _ = syscall.UTF16PtrFromString(path)
	ret, _, _ := _setupW.Call(uintptr(unsafe.Pointer(_p0)))
	return ret != 0
}

const (
	Ver = "Ver"
)

type COMObject struct {
	object *ole.IDispatch
}

func NewCOMObject(progID string) (*COMObject, error) {
	ole.CoInitialize(0)
	unknown, err := oleutil.CreateObject(progID)
	if err != nil {
		return nil, err
	}
	comObject, err := unknown.QueryInterface(ole.IID_IDispatch)
	if err != nil {
		return nil, err
	}
	return &COMObject{object: comObject}, nil
}
func (c *COMObject) Release() {
	c.object.Release()
	ole.CoUninitialize()
}

func (c *COMObject) Ver() string {
	ret, _ := oleutil.CallMethod(c.object, Ver)
	return ret.ToString()
}

func main(){
    // op插件路径
    isOK:=SetupW("bin/op_x64.dll")
    if !isOK {
        fmt.Println("op插件注册失败")
        return
    }

    op, _ := NewCOMObject("op.opsoft")
	defer op.Release()

	fmt.Println(op.Ver())
}
Clone this wiki locally