【问题标题】:Python ctypes identifying dll function resultPython ctypes识别dll函数结果
【发布时间】:2014-11-04 20:58:59
【问题描述】:

根据 DLL 的文档,我有一些功能(还有更多):

# there are 2 lines in the other example like this
# define CALLTYPE __stdcall
# define pLSenvL void*

pLSenvL CALLTYPE LScreateEnvL()
int LSopenLogFileL( pLSenvL pL, char *pcLogFile)
int CALLTYPE LSsetPointerL(pLSenvL pL, double* pdPointer, int* pnPointersNow)
int LSexecuteScriptL( pLSenvL pL, char* pcScript)
int LScloseLogFileL( pLSenvL pL)
void LSclearPointers( pLSenvL pL)

到目前为止我所做的是:

from ctypes import *

api = CDLL("PATH_TO_DLL")
pLEnv = api.LScreateEnvL()
script = "STRING FULL OF COMMANDS"

print api.LSexecuteScriptL(pLEnv, script)

它有效,但现在我想复制我找到的一个示例:

void CSDlg::OnSolve()
{
  int nError, nPointersNow;

  CString csScript, cs;
  double dNeeds[1], dStart[1];

  dNeeds[ 0] = (double) nNeedsM;

  pLSenvL pL;
  pL = LScreateEnvL();

  nError = LSopenLogFileL( pL, "log_file.log");
  nError = LSsetPointerL( pL, dNeeds, &nPointersNow);
  nError = LSsetPointerL( pL, dStart, &nPointersNow);

  csScript = "SET ECHOIN 1\n";

  // Run the script
  nError = LSexecuteScriptL( pL, (LPCTSTR) csScript);
  // Close the log file 
  LScloseLogFileL( pL);
  csStartM.Format( "%d", (int) dStart[0]);
}

到目前为止,我已经这样做了:

nError = c_int
nPointersNow = c_int

dNeeds = c_double()
#I'm just setting a random value
dNeeds = [c_double(10)]

pLEnv = api.LScreateEnvL()
nError = api.LSopenLogFileL(pLEnv, "log_file.log")
# here I got 
# Procedure called with not enough arguments (8 bytes missing) or wrong calling convention
nError = api.LSsetPointerL(pLEnv, byref(dNeeds), nPointersNow)
# and here I got
# byref argument must be a ctypes instance, not 'list'

所以我已经搜索过了,我不得不做这样的事情

#now here comes my problem
#according to documentation it is
#  int CALLTYPE LSsetPointerL(pLSenvL pL, double* pdPointer, int* pnPointersNow)
api.LSsetPointerL.restype = c_int
api.LSsetPointerL.argtypes = [ ¿?, c_double, c_int]
  • argtypes 数组中的第一个元素应该是什么?
  • 关于 CALLTYPE 定义我有什么需要担心的吗?

提前致谢

【问题讨论】:

    标签: python windows dll ctypes


    【解决方案1】:

    “Procedure called with not enough arguments (8 bytes missing) or wrong calling convention”是指您调用 DLL 的方式。您使用了 CDLL,但它显示“# define CALLTYPE __stdcall”,因此您应该改用 WinDLL (see ctypes docs)。有时这无论如何都有效,但似乎在更复杂的调用上失败了。

    你没有实例化nPointersNow,你可能是指nPointersNow()。

    您不能将dNeeds 设置为列表。也许你的意思是:

    somefunc.argtypes = [c_double()]
    dNeeds = c_double(10)
    nError = api.LSsetPointerL(pLEnv, byref(dNeeds), nPointersNow)
    

    plSenvL 类型应该在您的文档中定义。 LScreateEnvL 返回该类型,如下所示:

    pLSenvL CALLTYPE LScreateEnvL()
    pLSenvL __stdcall LScreateEnvL() (<- what the above line really means)
    

    所以你需要知道它是什么。我们可以猜测它是一个指向名为LSenvL 的指针(一个整数),您可能需要为其创建一个结构。不难,但你需要知道它是如何定义的。也就是说,您可以避免它,因为第一个函数返回它。因此,如果您不需要直接使用它,您可以尝试像这样躲避它:

    LScreateEnvL = api.LScreateEnvL
    LScreateEnvL.restype = c_int   # just assume it's an int
    
    LSopenLogFileL = api.LSopenLogFileL
    LSopenLogFileL.restype = c_int
    LSopenLogFileL.argtypes = (c_int,   # again just assume
                               c_char_p)
    
    LSsetPointerL = api.LSsetPointerL
    LSsetPointerL.restype = c_int
    LSsetPointerL.argtypes = (c_int,   # assume int
                              POINTER(c_double),
                              POINTER(c_int))
    
    pLSenvL = LScreateEnvL()
    
    dPointer = c_double()
    nPointersNow = c_int()
    nError = LSsetPointerL(pLSenvL,
                           byref(dPointer),
                           byref(nPointersNow))
    

    【讨论】:

    • 谢谢!,这对我有帮助。实际上在示例中 dNeeds 是一个包含 7 个元素的数组。我设法解决了这个问题。
    • Great =) 7 个双精度数组:c_double * 7 并且可以像这样实例化:(c_double * 7)()。
    猜你喜欢
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 2019-10-13
    • 2021-11-17
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    相关资源
    最近更新 更多