通过Python中的旧DLL检索数据

问题描述 投票:0回答:1

我尝试在 Python 中使用 2001 年的旧 DLL,但几乎没有留下任何文档。 经过一番工作后,我可以运行一些功能。但我无法接收数据。我对DLL和指针的了解相对有限。问题来了:

函数

TwixGetMainEntry
需要三个参数

    要检索的记录的
  • ID
  • 价值。指向字符串的指针: 该变量将被结果覆盖。这就是我的目标
  • 缓冲区长度

我确实得到了结果,但无法正确解码。数据可能不是 UTF-8。我必须使用 32 位版本的 python。

import ctypes
twix = ctypes.WinDLL("twx.dll")

# Some initialization stuff first
# ...

# Get data
value = ctypes.c_wchar_p("")
twix.TwixGetMainEntry(2, ctypes.byref(value), 1024)

value.value.encode('utf-8') # Returns things like: \xe3\xb9\xb0\xdf\x98\xe3\x90...

也用

c_char_p
尝试过,没有得到结果。

以下是该函数在其他语言中的一些定义

# VB.net
Public Declare Function TwixGetMainEntry Lib "twx.dll" (ByVal nr As Integer, ByVal value As String, ByVal k As Integer) As Integer
# C
int (*TwixGetMainEntry)(int, char*, int);

我可以尝试什么想法吗?

非常感谢!

python dll ctypes reverse-engineering
1个回答
0
投票

这是一个例子:

import ctypes as ct

# C - default calling convention is __cdecl
# int (*TwixGetMainEntry)(int, char*, int);

twix = ct.CDLL('./twx.dll')      # for __cdecl calling convention
# twix = ct.WinDLL('./twx.dll')  # for __stdcall calling convention

# Declaring argument types and result type helps ctypes check parameters.
# Make sure to match the C prototype.
twix.TwixGetMainEntry.argtypes = ct.c_int, ct.c_char_p, ct.c_int
twix.TwixGetMainEntry.restype = ct.c_int

value = ct.create_string_buffer(1024)  # Need to allocate a buffer, not just a pointer.
result = twix.TwixGetMainEntry(2, value, ct.sizeof(value))
print(value.raw)   # To view the whole buffer
print(value.value) # to see the null-terminated value of the buffer
© www.soinside.com 2019 - 2024. All rights reserved.