-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.go
75 lines (63 loc) · 1.63 KB
/
bootstrap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//go:build windows
// +build windows
package gdm
import (
"fmt"
"github.com/pkg/errors"
"syscall"
"unsafe"
ole "github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
)
var (
dmReg32 *syscall.LazyDLL
procSetDllPathA *syscall.LazyProc
procSetDllPathW *syscall.LazyProc
)
// DmSoft ...
type DmSoft struct {
dm *ole.IDispatch
IUnknown *ole.IUnknown
}
// New return *DmSoft.DmSoft
func New(dllPath, dllReg, dllDm string) (dm *DmSoft, err error) {
dmReg32 = syscall.NewLazyDLL(fmt.Sprintf(`%s\%s`, dllPath, dllReg))
procSetDllPathA = dmReg32.NewProc("SetDllPathA")
procSetDllPathW = dmReg32.NewProc("SetDllPathW")
if !SetDllPathW(fmt.Sprintf(`%s\%s`, dllPath, dllDm), 1) {
return nil, errors.New("load dm.dll failed")
}
var com DmSoft
// 创建对象
ole.CoInitialize(0)
com.IUnknown, err = oleutil.CreateObject("dm.dmsoft")
if err != nil {
return nil, err
}
// 查询接口
com.dm, err = com.IUnknown.QueryInterface(ole.IID_IDispatch)
if err != nil {
return nil, err
}
return &com, nil
}
// Release 释放
func (com *DmSoft) Release() {
com.IUnknown.Release()
com.dm.Release()
ole.CoUninitialize()
}
// SetDllPathA Ascii
func SetDllPathA(path string, mode int) bool {
var _p0 *uint16
_p0, _ = syscall.UTF16PtrFromString(path)
ret, _, _ := syscall.Syscall(procSetDllPathA.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(mode), 0)
return ret != 0
}
// SetDllPathW Unicode
func SetDllPathW(path string, mode int) bool {
var _p0 *uint16
_p0, _ = syscall.UTF16PtrFromString(path)
ret, _, _ := syscall.Syscall(procSetDllPathW.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(mode), 0)
return ret != 0
}