C ++函数:
DLLENTRY int VTS_API
SetParamValue( const char *paramName, void *paramValue ) //will return zero(0) in the case of an error
{
rtwCAPI_ModelMappingInfo* mmi = &(rtmGetDataMapInfo(PSA_StandAlone_M).mmi);
int idx = getParamIdx( paramName );
if (idx<0) {
return 0; //Error
}
int retval = capi_ModifyModelParameter( mmi, idx, paramValue );
if (retval == 1 ) {
ParamUpdateConst();
}
return retval;
}
和我的Python代码:
import os
from ctypes import *
print(os.getcwd())
os.chdir(r"C:\MY_SECRET_DIR")
print(os.getcwd())
PSAdll=cdll.LoadLibrary(os.getcwd()+"\PSA_StandAlone_1.dll")
setParam=PSAdll.SetParamValue
setParam.restype=c_int
setParam.argtypes=[c_char_p, c_void_p]
z=setParam(b"LDOGenSpdSetpoint", int(20) )
回报
z=setParam(b"LDO_PSA_GenSpdSetpoint01", int(20) )
WindowsError: exception: access violation reading 0x00000020
知道什么可以帮忙吗?我已经尝试过POINTER
和byref()
,但我得到了相同的输出
SetParamValue需要一个指针(void *或c_void_p)作为第二个参数,但是你传递了一个int。 该函数将其解释为指针(内存地址),并且在尝试取消引用它(获取其内容)时,它将发生段错误(Access Violation),因为该进程没有对该地址的权限。
要解决此问题,请将正确的参数传递给函数:
z = setParam(b"LDOGenSpdSetpoint", pointer(c_int(20)))
你可以在[Python 3]: ctypes - A foreign function library for Python找到更多细节。