Skip to main content
 首页 » 编程设计

python之为什么这个 Delphi DLL 在从 Python 调用时抛出 WindowsError

2024年10月25日8me-sa

环境:Windows 7 (x64) 上的 Python 2.7.9(32 位)

我在 Python 中使用 ctypes 来访问名为“tdbaccess.dll”(found here)的 DLL,根据作者的文档和 this info on the error I'm getting,我假设它是用 Delphi 编写的.不幸的是,我无权访问 DLL 的源代码。 DLL 用于打开旧版 Madden NFL PC 游戏使用的“数据库”花名册文件(我使用的是 2008 版本的花名册 - 不要判断:P)。

我的代码包含这两个结构:

class tdbTableProperties(Structure): 
    _fields_ = [ 
        ('Name', c_char_p), 
        ('FieldCount', c_int), 
        ('Capacity', c_int), 
        ('RecordCount', c_int), 
        ('DeletedCount', c_int), 
        ('NextDeletedRecord', c_int), 
        ('Flag0', c_bool), 
        ('Flag1', c_bool), 
        ('Flag2', c_bool), 
        ('Flag3', c_bool), 
        ('NonAllocated', c_bool), 
        ('HasVarchar', c_bool), 
        ('HasCompressedVarchar', c_bool), 
    ] 

和

class tdbFieldProperties(Structure): 
    _fields_ = [ 
        ('Name', c_char_p), 
        ('Size', c_int), 
        ('FieldType', c_int), 
    ] 

这些结构的实例通过 ref 传递给 DLL 中的两个函数,这两个函数依次填充其中的字段。首先,将 tdbTableProperties 的实例附加到列表中并按如下方式传递:

# Create a list to hold all our table properties structs. 
listTdbTableProperties = [] 
 
# Loop over the tables and get their properties. 
for i in range(intNumberOfTables): 
    listTdbTableProperties.append(tdbTableProperties(Name="ASDF")) #Initialize Name field per docs 
    boolGotTableProperties = tdbaccessDLL.TDBTableGetProperties(intDBIndex, i, byref(listTdbTableProperties[i])) 
    if boolGotTableProperties: 
        print("listTdbTableProperties[%d].Name = %r" % (i, listTdbTableProperties[i].Name)) 
        print("listTdbTableProperties[%d].FieldCount = %d\n" % (i, listTdbTableProperties[i].FieldCount)) 

这工作正常并提供以下输出:

listTdbTableProperties[0].Name = 'CITY' 
listTdbTableProperties[0].FieldCount = 21 
 
listTdbTableProperties[1].Name = 'COCH' 
listTdbTableProperties[1].FieldCount = 68 
 
[...] 
 
listTdbTableProperties[5].Name = 'INJY' 
listTdbTableProperties[5].FieldCount = 5 
 
listTdbTableProperties[6].Name = 'PLAY' 
listTdbTableProperties[6].FieldCount = 110 
 
[...] 

我真的只对“PLAY”表感兴趣,所以现在我尝试对 tdbFieldProperties 的实例进行类似的操作,以获取该表中每个字段的属性...

# A list to hold all our field properties structs for the player table. 
listPlayerTableTdbFieldProperties = [] 
 
# Get the properties of each of the fields for the PLAY table (index 6 in listTdbTableProperties). 
for i in range(listTdbTableProperties[6].FieldCount): 
    listPlayerTableTdbFieldProperties.append(tdbFieldProperties(Name="Blah")) #Initialize Name field per docs 
    print("listPlayerTableTdbFieldProperties[%d].Name BEFORE = %r" % (i, listPlayerTableTdbFieldProperties[i].Name)) 
    boolGotTableFieldProperties = tdbaccessDLL.TDBFieldGetProperties(intDBIndex, listTdbTableProperties[6].Name, i, byref(listPlayerTableTdbFieldProperties[i])) 
    if boolGotTableFieldProperties: 
        print("listPlayerTableTdbFieldProperties[%d].Name AFTER = %r" % (i, listPlayerTableTdbFieldProperties[i].Name)) 

... 然而,它最终还是失败了,总是在第六张表的 110 个字段中的第 70 个:

listPlayerTableTdbFieldProperties[0].Name BEFORE = 'Blah' 
listPlayerTableTdbFieldProperties[0].Name AFTER = 'TRV1' 
listPlayerTableTdbFieldProperties[1].Name BEFORE = 'TRV1' 
listPlayerTableTdbFieldProperties[1].Name AFTER = 'TEZ1' 
listPlayerTableTdbFieldProperties[2].Name BEFORE = 'TEZ1' 
listPlayerTableTdbFieldProperties[2].Name AFTER = 'TRV2' 
[...] 
listPlayerTableTdbFieldProperties[68].Name BEFORE = 'TAth' 
listPlayerTableTdbFieldProperties[68].Name AFTER = 'TAss' 
listPlayerTableTdbFieldProperties[69].Name BEFORE = 'TAss' 
Traceback (most recent call last): 
  File "DLLtest2.py", line 70, in <module> 
    boolGotTableFieldProperties = tdbaccessDLL.TDBFieldGetProperties(intDBIndex, listTdbTableProperties[6].Name, i, byref(listPlayerTableTdbFieldProperties[i])) 
WindowsError: [Error 250477278] Windows Error 0xEEDFADE 

根据 tdbaccess.dll 的文档,在 Delphi 或 Visual Basic .NET 实现中,tdbFieldProperties 结构将 FieldType 作为枚举,它在 Python 中没有确切的表示。来自文档:

Delphi Syntax   
  type 
    TtdbFieldProperties = packed record 
      Name: PWideChar; 
      Size: Integer; 
      FieldType: TtdbFieldType; 
  end; 
 
Visual Basic .NET Syntax 
  <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> 
  Structure TdbFieldProperties 
    Public Name As String 
    Public Size As Integer 
    Public FieldType As TdbFieldType 
  End Structure 

和

Delphi Syntax 
  type 
    TtdbFieldType = (tdbString = 0, tdbBinary = 1, tdbSInt = 2, tdbUInt = 3, tdbFloat = 4, tdbVarchar = $D, tdbLongVarchar = $E, tdbInt = $2CE); 
 
Visual Basic .NET Syntax 
  Enum TdbFieldType 
    tdbString = 0 
    tdbBinary = 1 
    tdbSInt = 2 
    tdbUInt = 3 
    tdbFloat = 4 
    tdbVarchar = &HD 
    tdbLongVarchar = &HE 
    tdbInt = &H2CE 
  End Enum 

因此,基于 answers to this question ,我尝试在 Python 中使用 c_int、c_uint(以及几乎所有其他 ctype)作为 FieldType 的类型。但无论我使用什么,我都会在第 6 个表的循环的第 70 次迭代时得到 Windows 错误 0xEEDFADE。不仅如此,我为 tdbFieldProperties 结构中的 Name 字段获取的值甚至在错误发生之前也不是它们应该的值。如果我扩展我的代码以获取每个表中每个字段的字段属性,那么我查询的每个表都会以相同的顺序将相同的值放入 Name 字段中,所以很明显,即使在错误本身之前也有错误:

# Loop over the fields in each tdbtpTableProperties and get the properties of each field. 
tdbfpTableFieldProperties = [[] for x in range(intNumberOfTables)] 
 
for i in range(intNumberOfTables): 
    print("\n") 
    for j in range(tdbtpTableProperties[i].FieldCount): 
        tdbfpTableFieldProperties[i].append(tdbFieldProperties(Name="Blah")) 
        print("tdbfpTableFieldProperties[%d][%d].Name BEFORE = %r" % (i, j, tdbfpTableFieldProperties[i][j].Name)) 
        boolGotTableFieldProperties = tdbaccessDLL.TDBFieldGetProperties(intDBIndex, tdbtpTableProperties[i].Name, j, byref(tdbfpTableFieldProperties[i][j])) 
        print("tdbfpTableFieldProperties[%d][%d].Name AFTER = %r" % (i, j, tdbfpTableFieldProperties[i][j].Name)) 

给出:

tdbfpTableFieldProperties[0][0].Name BEFORE = 'Blah' 
tdbfpTableFieldProperties[0][0].Name AFTER = 'TRV1' 
tdbfpTableFieldProperties[0][1].Name BEFORE = 'TRV1' 
tdbfpTableFieldProperties[0][1].Name AFTER = 'TEZ1' 
tdbfpTableFieldProperties[0][2].Name BEFORE = 'TEZ1' 
tdbfpTableFieldProperties[0][2].Name AFTER = 'TRV2' 
tdbfpTableFieldProperties[0][3].Name BEFORE = 'TRV2' 
tdbfpTableFieldProperties[0][3].Name AFTER = 'TEZ2' 
[...] 
tdbfpTableFieldProperties[0][20].Name BEFORE = 'CGID' 
tdbfpTableFieldProperties[0][20].Name AFTER = 'DGID' 
 
tdbfpTableFieldProperties[1][0].Name BEFORE = 'DGID' 
tdbfpTableFieldProperties[1][0].Name AFTER = 'TRV1' 
tdbfpTableFieldProperties[1][1].Name BEFORE = 'TRV1' 
tdbfpTableFieldProperties[1][1].Name AFTER = 'TEZ1' 
tdbfpTableFieldProperties[1][2].Name BEFORE = 'TEZ1' 
tdbfpTableFieldProperties[1][2].Name AFTER = 'TRV2' 
tdbfpTableFieldProperties[1][3].Name BEFORE = 'TRV2' 
tdbfpTableFieldProperties[1][3].Name AFTER = 'TEZ2' 
[...] 
tdbfpTableFieldProperties[1][67].Name BEFORE = 'TCTX' 
tdbfpTableFieldProperties[1][67].Name AFTER = 'TAth' 
 
tdbfpTableFieldProperties[2][0].Name BEFORE = 'TAth' 
tdbfpTableFieldProperties[2][0].Name AFTER = 'TRV1' 
tdbfpTableFieldProperties[2][1].Name BEFORE = 'TRV1' 
tdbfpTableFieldProperties[2][1].Name AFTER = 'TEZ1' 
tdbfpTableFieldProperties[2][2].Name BEFORE = 'TEZ1' 
tdbfpTableFieldProperties[2][2].Name AFTER = 'TRV2' 
tdbfpTableFieldProperties[2][3].Name BEFORE = 'TRV2' 
tdbfpTableFieldProperties[2][3].Name AFTER = 'TEZ2' 
[...] 

我确定这不是 DLL 中的错误,因为我知道有一个 Visual C# 项目可以做我想在 Python 中做的同样的事情,没有任何问题。任何想法我可能做错了什么?非常感谢!

请您参考如下方法:

第一个明显的错误是 c_char_p 是一个指向 8 位文本的指针。但 Delphi 和 VB.net 结构清楚地使用 16 位 Unicode 文本。将 c_char_p 替换为 c_wchar_p。

下一个问题与记录的布局有关。 Delphi 和 VB.net 代码不一致。 Delphi 记录已打包,VB.net 结构已对齐。您的 Python 结构已对齐。您可以尝试在 ctypes 结构中使用 _pack_ = 1 来打包记录。任何人都可以猜测记录的真实布局。

我们不确定枚举类型有多大。它们可以是 1、2 或 4 个字节。我们也没有要检查的所有结构声明,因此可能存在并且很可能存在超出我在这里所写内容的问题。