Python C类型标识dll函数结果
根据DLL的文档,我有一些函数(还有更多): 到目前为止,我所做的是:Python C类型标识dll函数结果,python,windows,dll,ctypes,Python,Windows,Dll,Ctypes,根据DLL的文档,我有一些函数(还有更多): 到目前为止,我所做的是: 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,
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的定义
过程调用时没有足够的参数(缺少8个字节)或调用约定错误
”指的是调用DLL的方式。您使用了CDLL,但它上面写着“#define CALLTYPE u stdcall
”,因此应该改用windl()。这有时是可行的,但在更复杂的调用中似乎失败了
您不是在实例化npointernow
,您的意思可能是npointernow()
不能将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)
谢谢这对我很有帮助。实际上,在示例中,dNeeds是一个由7个元素组成的数组。我设法解决了这个问题。很棒=)7个双精度数组:
c\u double*7
,可以像这样实例化:(c\u double*7)(
)。
pLSenvL CALLTYPE LScreateEnvL()
pLSenvL __stdcall LScreateEnvL() (<- what the above line really means)
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))