gpt4 book ai didi

delphi - 从转到调用Delphi dll

转载 作者:行者123 更新时间:2023-12-01 20:22:59 24 4
gpt4 key购买 nike

我有一个需要从golang(go)调用的Delphi dll。我可以使用syscall.Syscall或windows.Call加载并调用dll。两种方法都可以正确执行dll。 dll通过更改传递给它的字符串缓冲区来返回其结果。当我以后检查字符串时,它是空的(在syscall中)或在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\000"))
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")
}


使用syscall的示例:

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

最佳答案

您正在将指针传递给 slice ,而不是将指针传递给 slice 的后备数组。
与您的windows.Call代码应该是。

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

或您的syscall.Syscall版本,其中是指向 slice 的指针。
ret, _, callErr := syscall.Syscall(uintptr(getRandomSID),  
nargs, 0,
uintptr(unsafe.Pointer(&(*Buffer)[0])),
uintptr(unsafe.Pointer(BufSize)),
)

关于delphi - 从转到调用Delphi dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60067389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com