简体   繁体   中英

Access Violation while calling a c++ dll function in C#

System Access violation error occurs when a call to one of the C++ dll functions is made. (Refer the code below - In the step int calresult =...ie,) The array values are all appropriately given. What part of the input might be causing this error?

Thanks !

C++ code-xyz.dll:

typedef struct model_parameters_t
{
    uint16_t *Input4;
    uint16_t *Input5;
} ;

typedef struct calibration_set_s
{
    uint16_t *input1;
    uint16_t *input2;
    uint16_t input3;
    model_parameters_t model;
} calibration_t;



extern "C"
{
    uint16_t compute(calibration_t);
}

C# code:

public struct model_parameters_t
{
    uint16_t [] Input4;
    uint16_t [] Input5;
} ;

public struct calibration_set_s
{
    uint16_t []input1;
    uint16_t []input2;
    uint16_t input3;
    model_parameters_t model;
} calibration_t;


[DllImport(@"xyz.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern int compute(calibration_set_s calibration_set_t);



public class trycompute
{
    calibration_set_s calibration_set_Cal_input = new calibration_set_s();
    calibration_set_Cal_input.input1= array;
    calibration_set_Cal_input.input2= array;
    calibration_set_Cal_input.input3= 12;
    calibration_set_Cal_input.model.input4 = array;
    calibration_set_Cal_input.model.input5 = array;

    int CalResult = computeHybridCalCoeffs(calibration_set_Cal_input);
}

Use actual pointers instead of arrays inside the structs in C#, declaring the class unsafe .

There is no way for C# to know how to marshal the arrays inside struct, for example, the size of arrays is undefined.

Thanks for your suggestions.

I had to create a wrapper in C++/CLI around my native C++ dll. I then called the functions in the wrapper from my C# code. This solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM