duanaidang6197
2013-06-09 04:40
浏览 239
已采纳

在win7-64中调用dll时,golang syscall出现问题

Here my code:

package main

import (
    "fmt"
    "syscall"
    "unsafe"
)

var (
    WinSCard, _                  = syscall.LoadLibrary("C:\\windows\\system32\\WinSCard.dll")
    procSCardListReaders, _      = syscall.GetProcAddress(WinSCard, "SCardListReaders")
    procSCardEstablishContext, _ = syscall.GetProcAddress(WinSCard, "SCardEstablishContext")
)

func abort(funcname string, err error) {
    panic(fmt.Sprintf("%s failed: %v", funcname, err))
}

func SCardEstablishContext(dwScope uint32, pvReserved1 *uint32, pvReserved2 *uint32, phContext uintptr) int32 {
    ret, _, callErr := syscall.Syscall6(uintptr(procSCardEstablishContext),
        4,
        uintptr(unsafe.Pointer(&dwScope)),
        uintptr(unsafe.Pointer(pvReserved1)),
        uintptr(unsafe.Pointer(pvReserved2)),
        phContext,
        0,
        0)
    if callErr != 0 {
        abort("Err:", callErr)
    }
    return int32(ret)
}

func main() {
    var Hwd uintptr
    defer syscall.FreeLibrary(WinSCard)
    rt := SCardEstablishContext(0, nil, nil, Hwd)
    fmt.Println(rt)
}

Return -2146435068 means 0x80100004 means SCARD_E_INVALID_PARAMETER. One or more of the supplied parameters could not be properly interpreted.

The func SCardEstablishContext:

LONG WINAPI SCardEstablishContext(
    _In_   DWORD dwScope,
    _In_   LPCVOID pvReserved1,
    _In_   LPCVOID pvReserved2,
    _Out_  LPSCARDCONTEXT phContext
)

What's wrong with my parameters? Thx a lot!!

How about

func SCardListReaders(hContext *syscall.Handle, mszGroups string, mszReaders *byte, pcchReaders *uint32) (err error) {
r1, _, e1 := syscall.Syscall6(
    uintptr(procSCardListReaders),
    4,
    uintptr(unsafe.Pointer(hContext)),
    uintptr(unsafe.Pointer(syscall.StringBytePtr(mszGroups))),
    uintptr(unsafe.Pointer(mszReaders)),
    uintptr(unsafe.Pointer(pcchReaders)),
    0,
    0,
)
if r1 != 0 {
    if e1 != 0 {
        err = error(e1)
    } else {
        err = syscall.EINVAL
    }
}
return
}

var Groups string
var Readers byte
var pcchReaders uint32
er1 := SCardListReaders(&context, Groups, &Readers, &pcchReaders)
if er1 != nil {
    fmt.Println("SCardListReaders:", er1)
    return
}

return: invalid argument

1条回答 默认 最新

相关推荐 更多相似问题