简体   繁体   English

Python ctypes - dll 函数接受结构崩溃

[英]Python ctypes - dll function accepting structures crashes

I have to access a POS terminal under ms windows xp.我必须在 ms windows xp 下访问 POS 终端。 I am using python 2.7.我正在使用 python 2.7。 The crucial function in the DLL I load that does the payment accepts two pointer to structures, but it crashes returning 1 (Communication error) but without further messages.我加载的 DLL 中执行支付的关键函数接受两个指向结构的指针,但它崩溃返回 1(通信错误)但没有进一步的消息。 Please note that when the payment function is called, not all the elements of POSData structure receive a value.请注意,调用支付功能时,并非 POSData 结构的所有元素都会收到一个值。 Other function I tried (GetVersion) does work.我试过的其他功能 (GetVersion) 确实有效。 Here specifications and my code:这里的规格和我的代码:

typedef struct
{
  char IPAddress[16]; //xxx.xxx.xxx.xxx
  int Port;
} TETHParameters;   
typedef struct
{
  char TerminalId[8+1];
  char AcquirerId[11+1];
  char TransactionType[3+1];
  char TransactionResult[2+1];
  char KODescription[24+1];
  char CardType[1+1];
  char STAN[6+1];
  char PAN[19+1];
  char AuthorizationCode[6+1];
  char OperationNumber[6+1];
  char DataTrs[7+1];
} TPOSData;

typedef struct
{
  char Amount[8+1];
  char ECRId[8+1];
  char PaymentType[1+1];
  char TerminalId[8+1];
} TECRData;

__declspec(dllexport) void IAE17_GetVersion(char *Version);
__declspec(dllexport) void IAE17_InitEth(TETHParameters *ETHParameters);

__declspec(dllexport) void IAE17_Free(void);

__declspec(dllexport) int IAE17_Payment(TECRData *ECRData, TPOSData *POSData);

from ctypes import *
#da python 3.x sara' configparser
import ConfigParser  
import logging
from time import  localtime,  strftime

    #STRUTTURE DATI
class TETHParameters(Structure):
    _fields_ =  [("IPAddress" , c_char_p), ("Port" , c_int )]


class TECRData(Structure):
    _fields_ = [("Amount" , c_char_p),
    ("ECRId", c_char_p),
    ("PaymentType", c_char_p),
    ("TerminalId", c_char_p),
    ("Contract", c_char_p),
    ("PreauthorizationCode", c_char_p),
    ("STAN", c_char_p),
    ("Ticket2Ecr", c_char_p)]


class TPOSData(Structure):
    _fields_ = [
    ("TerminalId" , c_char_p),
    ("AcquirerId" , c_char_p),
    ("TransactionType" , c_char_p),
    ("TransactionResult" , c_char_p),
    ("KODescription" , c_char_p),
    ("CardType" , c_char_p),
    ("STAN" , c_char_p),
    ("POSBalance" , c_char_p),
    ("BankBalance" , c_char_p),
    ("PAN" , c_char_p),
    ("AuthorizationCode" , c_char_p),
    ("OperationNumber" , c_char_p),
    ("AmountAuth" , c_char_p),
    ("PreauthorizationCode" , c_char_p),
    ("ActionCode" , c_char_p),
    ("DataTrs" , c_char_p),
    ("AmountEcho" , c_char_p),
    ("Ticket" , c_char_p)
    ] 

ECRData = TECRData( ECRId = c_char_p( '012345678' ), 
                    Amount  = c_char_p( '00000000')  , 
                    TerminalID = c_char_p( '01234567' ), 
                    PaymentType = c_char_p ("0")
                       )    


POSData = TPOSData( KODescription = c_char_p('                        '),
                            TerminalId = c_char_p('        '),  
                            AcquirerId = c_char_p('           '), 
                            TransactionType = c_char_p('   '), 
                            TransactionResult = c_char_p('   '),
                            CardType = c_char_p('  '), 
                            STAN = c_char_p('      '),
                            PAN = c_char_p('                   '), 
                            AuthorizationCode = c_char_p('      '),
                            OperationNumber = c_char_p('      '), 
                            DataTrs = c_char_p('       ')  
                            )   
ETHParameters = TETHParameters( IPAddress = c_char_p( '192.168.127.190' ) ,  Port = c_int(45119))                           
iae17 = windll.LoadLibrary('iae17')     
iae17.IAE17_InitEth( byref( ETHParameters) )   
result =  iae17.IAE17_Payment( byref(ECRData), byref(POSData))                      
print result

c_char_p is a direct translation of a C's char * . c_char_p是 C 的char *的直接翻译。 So, it seems to me that while your C structure is所以,在我看来,虽然你的 C 结构是

typedef struct
{
  char TerminalId[8+1];
  char AcquirerId[11+1];
  char TransactionType[3+1];

&c

the allegedly-corresponding one you're making in ctypes is, instead, equivalent to您在 ctypes 中制作的据称对应的对应于

typedef struct
{
  char* TerminalId;
  char* AcquirerId;
  char* TransactionType;

&c

which is of course a drastically different thing.这当然是完全不同的事情。 Why are you using "pointers" instead of ctypes' arrays ?你为什么使用“指针”而不是 ctypes 的数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM