从 go 调用 Delphi dll

Call Delphi dll from go

我有一个 Delphi dll 需要从 golang (go) 调用。我可以使用 syscall.Syscall 或 windows.Call 加载和调用 dll。这两种方法都正确执行了 dll。 dll returns 通过更改传递给它的字符串缓冲区来获取结果。当我稍后检查字符串时,它是空的(在系统调用中)或在 windows.Call 中出现恐慌。我已经尝试了几种排列,但没有运气从应该存储结果的变量中获取我的值。

示例使用 windows.Call:


    // Package getSID calls SIDgenerator and generates a SID for Retail Pro
    package main

    import (
        "fmt"
        "unsafe"

        "golang.org/x/sys/windows"

        "unicode/utf16"
    )

    var (
        sidgeneratorDLL, _ = windows.LoadDLL("sidgenerator64.dll")
        getRandomSID, _    = sidgeneratorDLL.FindProc("GetRandomSID")
    )

    // StringToCharPtr converts a Go string into pointer to a null-terminated cstring.
    // This assumes the go string is already ANSI encoded.
    func StringToCharPtr(str string) *uint8 {
        chars := append([]byte(str), 0) // null terminated
        return &chars[0]
    }

    // GetRandomSID generates random SID, UPC SID or ALU SID
    func GetRandomSID(Buffer []uint16, BufSize uint64) (result byte) {
        // var nargs uintptr = 2
        fmt.Println(Buffer)
        ret, _, callErr := getRandomSID.Call(uintptr(unsafe.Pointer(&Buffer)),
            uintptr(unsafe.Pointer(&BufSize)))

        // fmt.Println("Buffer", Buffer)

        if callErr != nil {
            fmt.Println("===== CallErr =====")
            fmt.Println(ret, callErr)
            //  fmt.Println("Buffer", Buffer)
        }
        result = byte(ret)
        return
    }

    func main() {
        defer sidgeneratorDLL.Release()
        buffer := utf16.Encode([]rune("12345678901234567890[=11=]0"))
        bufSize := uint64(len(buffer))
        fmt.Printf("Result in: %v\n", buffer)

        err := GetRandomSID(buffer, bufSize)
        fmt.Printf("Result in: %v\n", buffer)
        fmt.Println("Err =", err)
        fmt.Println("Called GetRandomSID")
        fmt.Printf("Result out: %v\n", buffer)
    }

func init() {
    fmt.Print("Starting Up\n")
}

使用系统调用的示例:


    // Package getSID calls SIDgenerator and generates a SID for Retail Pro
    package main

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

    var (
        sidgeneratorDLL, _ = syscall.LoadLibrary("sidgenerator64.dll")
        getRandomSID, _ =  syscall.GetProcAddress(sidgeneratorDLL, "GetRandomSID")
    )

    // GetRandomSID generates random SID, UPC SID or ALU SID
    func GetRandomSID(Buffer *[]byte, BufSize *uint32) (result byte) {
        var nargs uintptr = 2
        fmt.Println(*Buffer)
        ret, _, callErr := syscall.Syscall(uintptr(getRandomSID),  
        nargs,  0,
        uintptr(unsafe.Pointer(Buffer)), 
        uintptr(unsafe.Pointer(BufSize)),
        )

        fmt.Println(*Buffer)

        if callErr != 0 {
            fmt.Println("===== CallErr =====")
            fmt.Println(callErr)
        }
        result = byte(ret)
        return
    }

    func main () {
        defer syscall.FreeLibrary(sidgeneratorDLL)
        buffer := []byte("1234567890123456789012")
        bufSize := uint32(len(buffer))
        fmt.Printf("Result in: %s\n", string(buffer))
        err := GetRandomSID(&buffer, &bufSize)
        fmt.Println("Err =", err)
        fmt.Println("Called GetRandomSID")
        fmt.Printf("Result out: %s\n", string(buffer))
    }

    func init() {
        fmt.Print("Starting Up\n")
    }

任何帮助将不胜感激。提前致谢!

C# 外部声明:--------------------------------

[DllImport("SIDGenerator.dll", CharSet = CharSet.Ansi)] static extern Boolean GetSIDFromALU(string ALU,  StringBuilder SID,  ref int buflen); 

 [DllImport("SIDGenerator.dll", CharSet = CharSet.Ansi)]  static extern Boolean GetSIDFromUPC( string UPC,  StringBuilder SID,  ref int buflen); 

 [DllImport("SIDGenerator.dll", CharSet = CharSet.Ansi)]  static extern Boolean GetRandomSID(StringBuilder SID, ref int buflen);   

Delphi 外部声明:------------------------------------

function GetSIDFromALU( ALU: Pchar;  Buffer: PChar;  var BufSize : integer ): LongBool; stdcall;  external ‘SIDGenerator.dll’; 

function GetSIDFromUPC( UPC: PChar;  Buffer: PChar; var BufSize : integer ): LongBool; stdcall; external ‘SIDGenerator.dll’; 

function GetRandomSID( Buffer: PChar;   var BufSize : integer ): LongBool; stdcall; external ‘SIDGenerator.dll’; 

在 Python 中,我用这段代码调用它,它运行得很漂亮:

# Get Retail Pro SID using dll
from ctypes import windll, create_string_buffer, c_int, byref
# os.chdir("c:\Users\irving\Documents\gitHub\POImport")
# print(os.getcwd())

# getSID is a Retail Pro dll use to generate SIDs
getSID = windll.SIDGenerator64
randomSID = getSID.GetRandomSID
aluSID = getSID.GetSIDFromALU
upcSID = getSID.GetSIDFromUPC

# Set this by hand now, later set it interactively or by parameter
subsidiary = "1"

def getRandomSID():
    """ Genera un SID aleatorio """
    # Generate a new random SID
    newSID = create_string_buffer(str.encode("12345678901234567890"))
    size = c_int(len(newSID))
    randomSID(newSID, byref(size))
    return newSID.value


def getALUSID(alu):
    """ Genera un SID basado en ALU """
    # Generate a new ALU base SID
    ALU = create_string_buffer(str.encode(alu))
    newSID = create_string_buffer(str.encode("12345678901234567890"))
    size = c_int(len(newSID))
    aluSID(ALU, newSID, byref(size))
    return newSID.value


def getUPCSID(upc):
    """ Genera un SID basado en UPC """
    # Generate a new UPC based SID
    UPC = create_string_buffer(str.encode(upc))
    newSID = create_string_buffer(str.encode("12345678901234567890"))
    size = c_int(len(newSID))
    upcSID(UPC, newSID, byref(size))
    return newSID.value

您传递的是指向切片的指针,而不是指向切片后备数组的指针。 使用您的 windows.Call 代码应该是。

ret, _, callErr := getRandomSID.Call(uintptr(unsafe.Pointer(&Buffer[0])),
                   uintptr(unsafe.Pointer(&BufSize)))

或者你的 syscall.Syscall 版本,它是一个指向切片的指针。

ret, _, callErr := syscall.Syscall(uintptr(getRandomSID),  
        nargs,  0,
        uintptr(unsafe.Pointer(&(*Buffer)[0])), 
        uintptr(unsafe.Pointer(BufSize)),
        )