dongpao5658 2019-05-12 15:04
浏览 97

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

I'm trying to convert a program from Python (2.7, 32-bit) to Go (1.12.5, 32bit), and failing miserably with an access violation. The program calls a function in a 32-bit dll (ehlapi32.dll).

The following clips of the Python code appear to work perfectly (I'm not affirming that they are correct!):

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))

Now here's what I'm trying as the Go equivalent. This fails:

// 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
}

The failure looks like an access violation at s.Proc.Call in ecall().

I realise that syscall is deprecated; I have also tried with golang.org/x/sys/windows, without success. I have no constraints as to which I use.

I'll confess I'm way out of my depth here.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)