有个 dll 的函数原型是这样
//C++原型 extern "C" __declspec(dllexport) void __stdcall EncryptBuff(int lengthbuff,void* pbuff);
c 代码里面会对 pbuff 做修改 py 代码
import os
from ctypes import *
PWD = os.path.dirname(os.path.abspath(__file__))
lib = windll.LoadLibrary(os.path.join(PWD, "buff1.dll")) # 要全路径
phone="18267919970".encode("utf-16").hex()[4:]
suff = "0001410001000600010002"
all = "0001410001000600010002"+phone
print("len:", len(suff))
print("all:", all)
cmp = b"000141000100060001000231003800320036003700390031003900390037003000"
print(all==cmp)
print("slice:", all[20:22])
index = 20
p=c_void_p(bytes.fromhex(all[index:]))
print("p.value:",p.value, ",size:", sizeof(p))
lib.EncryptBuff(c_int(sizeof(p)), byref(p))
# print(p.value)
print(all[:index]+p.value.hex()[4:])
代码运行成功了,但是结果和 dephi 调用的不一样呀,
1
wwqgtxx 2018-11-28 15:44:01 +08:00 1
python 的 bytes 是不可变对象,直接用 C 代码修改他的底层数组会导致不可预见的结果,正确做法应该用 ctypes.create_string_buffer
>You should be careful, however, not to pass them to functions expecting pointers to mutable memory. If you need mutable memory blocks, ctypes has a create_string_buffer() function which creates these in various ways. The current memory block contents can be accessed (or changed) with the raw property; if you want to access it as NUL terminated string, use the value property |
3
wwqgtxx 2018-11-28 16:41:02 +08:00 via iPhone 1
你先自己写个 dll 直接 printf pbuff 看看你传进去的数据是否和 dephi 传进去的一样的再找别的原因
|