0

C++ funtion:

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;
  }

and my Python code:

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) )

returns

z=setParam(b"LDO_PSA_GenSpdSetpoint01", int(20) )
WindowsError: exception: access violation reading 0x00000020

Any idea what can help? I already tried POINTER and byref(), but I am getting the same output

2
  • 1
    What does capi_ModifyModelParameter do with the paramValue pointer? Though passing a "random" integer like 20 is pretty much never correct Commented Sep 20, 2018 at 11:47
  • Just a quick hint: it is generally not recommended to use ctypes directly. cffi provides a much easier and robust interface. Commented Sep 20, 2018 at 11:51

1 Answer 1

1

SetParamValue expects a pointer (void* or c_void_p) as the 2nd argument, but you're passing an int.
The function will interpret it as a pointer (memory address), and when attempting to dereference it (to get its content), it will segfault (Access Violation), as the process doesn't have permissions on that address.

To fix the problem, pass the proper arguments to the function:

z = setParam(b"LDOGenSpdSetpoint", pointer(c_int(20)))

You can find more details on [Python 3]: ctypes - A foreign function library for Python.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.