2011-11-09 16 views
0

我想使用ctypes在Python源代碼中使用dll。我從Python文檔開始閱讀它,因爲我是新手。成功加載的dll在Python,當我嘗試將字符串傳遞到需要一個char *功能後,它給了我不能傳遞字符串char * arg函數調用在dll中的python

"ValueError: Procedure probably called with too many arguments (4 bytes in excess)".

我還試圖尋找其他職位,但未能解決問題。

我嘗試了不同的方法來傳遞這個字符串,例如使用byref()pointer(),但它沒有改變結果。我也嘗試過WINFUNCTYPE但失敗。我使用的DLL是windll。

這是一個測試程序中,我在python寫道:

from ctypes import * 

lpDLL=WinDLL("C:\some_path\myDll.dll") 
print lpDLL 

IP_ADDR = create_string_buffer('192.168.100.228') 
#IP_ADDR = "192.168.100.228" 
#IP_ADDR = c_char_p("192.168.100.228") 

print IP_ADDR, IP_ADDR.value 

D_Init=lpDLL.D_Init 
D_InitTester=lpDLL.D_InitTester 

#D_InitTesterPrototype = WINFUNCTYPE(c_int, c_char_p) 
#D_InitTesterParamFlags = ((1, "ipAddress", None),) 
#D_InitTester = d_InitTesterPrototype(("D_InitTester", lpDLL), D_InitTesterParamFlags) 

try: 
    D_Init() 
    D_InitTester("192.168.100.254") 
except ValueError, msg: 
    print "Init_Tester Failed" 
    print msg 

這裏的D_InitTester如何在CPP文件,該文件是在DLL導出可實現的,

D_API int D_InitTester(char *ipAddress) 
{ 
    int err = ERR_OK; 

    if (LibsInitialized) 
    { 
     ... 
     some code; 
     ... 

     else 
     { 
      err = hndl->ConInit(ipAddress); 
     } 

     if (0 < err) 
     { 
      err = ERR_NO_CONNECTION; 
     } 
     else 
     { 
     nTesters = 1; 
      InstantiateAnalysisClasses(); 
      InitializeTesterSettings(); 
      if(NULL != hndl->hndlFm) 
      { 
       FmInitialized = true; 
      } 
     } 
    } 
    else 
    { 
     err = ERR_NOT_INITIALIZED; 
    } 
    return err; 
} 

你的幫助是極大的讚賞。

回答

2

錯誤的原因很可能是調用約定不匹配。我猜你的C++ DLL導出函數的規範是cdecl,但你使用WinDLL意味着stdcall

創建您的圖書館這樣使用cdecl

lpDLL=CDLL("C:\some_path\myDll.dll") 
+0

非常感謝,改爲CDLL爲我工作。 – sks

相關問題