从Go调用DLL-我在做什么错?

时间:2019-05-12 15:04:41

标签: go dll

我试图将程序从Python(2.7,32位)转换为Go(1.12.5,32位),但由于访问冲突而失败。该程序在32位dll(ehlapi32.dll)中调用一个函数。

以下Python代码片段似乎运行良好(我不确定它们是正确的!):

class ehlapi32:
    hllDll       = "C:\Program Files (x86)\IBM\Personal Communications\EHLAPI32.dll"
    hllDll       = ctypes.WinDLL(hllDll)
    hllApiProto  = ctypes.WINFUNCTYPE(ctypes.c_int,ctypes.c_void_p,ctypes.c_void_p,
                                      ctypes.c_void_p,ctypes.c_void_p)
    hllApiParams = (1, "p1", 0), (1, "p2", 0), (1, "p3",0), (1, "p4",0),
    hllApi       = hllApiProto (("HLLAPI", hllDll), hllApiParams)
    pFunConnect  = ctypes.c_int(1)
...
    def connect(self):
        shlSession = self.session + b'\0\0\0'
        pText      = ctypes.c_char_p (shlSession)
        pLength    = ctypes.c_int (len(shlSession))
        pReturn    = ctypes.c_int (13)

        ehlapi32.hllApi (ctypes.byref (ehlapi32.pFunConnect), pText,
                         ctypes.byref (pLength), ctypes.byref (pReturn))

现在这就是我正在尝试的Go等效项。失败:

// ElFunc: EHLLAPI Functions
type ElFunc uint32

const (
    eConnect    ElFunc = 1  // Connect to a terminal
)
...
// Session: a 3270 session handle
type Session struct {
    SessId  string
    SysId   string
    Entry   string
    Result  []string
    Headers int
    Footers int
    Rows    int
    Cols    int
    Scroll  int
    Proc    *syscall.LazyProc
}
...
func (s Session) Connect() uint32 {
    // Load EHLLAPI DLL
    dll := syscall.NewLazyDLL(dllName)
    s.Proc = dll.NewProc(dllProc)
    // Connect to session
    rc := s.ecall(eConnect, s.SessId, 1, 0)
    if rc == 0 {
        defer s.Disconnect()
    }
...
// ecall: provides simplified EHLLAPI DLL calls

func (s Session) ecall(fun ElFunc, buffer string, count uint32, ps uint32) uint32 {
    rc := ps
    _, _, _ = s.Proc.Call(
        uintptr(unsafe.Pointer(uintptr(uint32(fun)))),
        uintptr(unsafe.Pointer(&buffer)),
        uintptr(unsafe.Pointer(uintptr(count))),
        uintptr(unsafe.Pointer(uintptr(rc))),
    )
    return rc
}

失败看起来像是s.Proc.Callecall()处的访问冲突。

我意识到syscall已过时;我也尝试过golang.org/x/sys/windows,但没有成功。我对使用哪个没有任何限制。

我要承认我在这里超出了我的深度。

0 个答案:

没有答案