python - 无法访问传递给 NASM 64 位 DLL 的数组指针

标签 python assembly dll nasm ctypes

我将两个数组的指针从 Python 程序(使用 ctypes)传递到 NASM 64 位 DLL。在 Windows 调用约定中,指针在 rcx 和 rdx 中传递。这两个数组都是Int64。这些数组是在 Python 中创建的,并通过以下方式传递给 DLL:

PassArrayType = ctypes.c_int64 * 10
PVarrNew = PassArrayType()
OutputArrayType = ctypes.c_int64 * 1000
arrNew = OutputArrayType()
retvar = SimpleTest(ctypes.byref(PVarrNew), ctypes.byref(arrNew))

在 DLL 中,我可以从 rcx 中的数组指针读取,但无法写入数组。

例如,从 rcx 指向的数组中读取值:

push qword [rcx+32]
pop qword [tempvar]

但是向 rcx 指向的数组写入值不起作用:

mov rax,1235
push rax
pop qword [rcx+32]

将相同的值写入变量时有效:

mov rax,1235
push rax
pop qword [tempvar]

我无法读取或写入 rdx 指向的数组。

所以我的问题是:

  1. 为什么我可以读取rcx指向的数组,但不能写入它?
  2. 为什么我无法读取或写入 rdx 指向的数组?

迈克尔,感谢您的回复。我将“最小”误解为仅指有问题的代码行。我对 Stack Overflow 还很陌生,但现在我知道要发布多少代码了。这是完整的 Python 代码和完整的 NASM 代码。 Python 是 3.6.2。 NASM 是 64 位的。

Python 代码:

OutputArrayType = ctypes.c_int64 * 1000
arrNew = OutputArrayType()

PassArrayType = ctypes.c_int64 * 10
PVarrNew = PassArrayType()
PVarrNew[0] = id(PVarrNew)
PVarrNew[1] = 2
PVarrNew[2] = len(PVarrNew)

ThisDll = ctypes.WinDLL(r"C:/Temp2/Std_Math_Formulas.dll")
SimpleTest = ThisDll.SimpleTest
SimpleTest.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
SimpleTest.restype = ctypes.c_int64

retvar = SimpleTest(ctypes.byref(PVarrNew), ctypes.byref(arrNew))

NASM 代码:

; Header Section
[BITS 64]

export SimpleTest
section .data
tempvar:  dq 0

section .text
finit

SimpleTest:

push rdi
push rbp

mov rdi,rcx
mov rbp,rdx

push qword [rcx+32]
pop qword [tempvar]

; this works with rcx, but not rdx
mov rdi,rcx
push qword [rdi+32]
pop qword [tempvar]

; this works with rcx, but not rdx
mov rax,1235
push rax
pop qword [rcx+32]

mov rax,[tempvar]

pop rbp
pop rdi
ret

我用以下方式组装和链接我的 DLL:

nasm -Z myfile.err -f Win64 C:\Temp2\Std_Math_Formulas.asm -l myfile.lst -F cv8 -g -o C:\Temp2\Std_Math_Formulas.obj 
GoLink Std_Math_Formulas.obj /dll /entry SimpleTest msvcrt.dll 

最佳答案

Michael Petch 上面提出的解决方案解决了这个问题。正如 Michael 提议的那样,我从 GoLink 链接器命令字符串中删除了“/entry SimpleTest”,现在我可以从 rcx 和 rdx 指向的数组中读取和写入。正确的命令字符串是:

GoLink Std_Math_Formulas.obj /dll msvcrt.dll

非常感谢您的解决方案;我非常感谢你的帮助。

关于python - 无法访问传递给 NASM 64 位 DLL 的数组指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48122238/

相关文章:

c++ - 如何将 RDRAND 指令添加到用 VS 2008 编译的 64 位代码中?

c# - 在没有 COM 的情况下从非托管 C++ 应用程序调用 C# dll

python - easy_install 和 pip 在尝试安装 numpy 时出错

numpy - 对单个图像进行预处理以进行预测。 (CNN 使用 Keras 构建和训练)

python - 根据距离和出现频率选择一个项目(从一组项目中)

assembly - 创建一个加载了 grub2 的简单多引导内核

java - 如何检查 Java 代码的汇编输出?

python - 神经网络中的 SciPy 优化警告

c# - 从用 C++ 编写的桌面应用程序过渡到基于 Web 的应用程序

c++ - 从 C DLL 调用重载的 C++ 函数