My C# application program calls a C++ DLL and got System. AccessViolationException.
The DLL function snippet in C# are wrapped as below:
class CDllWrapper
{
#region Dll interface
public CDllWrapper() { }
[DllImport("FRV_Dll", //name of the dll
EntryPoint = "Face_Start", //name of function in dll
ExactSpelling = true,
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe int Face_Start();
....
[DllImport("FRV_Dll", //name of the dll
EntryPoint = "Face_Register", //name of function in dll
ExactSpelling = true,
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe int Face_Register(CppImportInfo imageData, ref int retCode, ref string retMessage);
.......
#endregion
}
Program snippet in C++ is as follows:
CPLUSPLUS_API int Face_Start()
{
......
}
CPLUSPLUS_API int Face_Register(struct ImportInfo imageData, int retCode, char** retMessage)
{
try
{
NBiometricClient biometricClient;
NFace face;
return 99;
.....
}*
Calling Face_Start() succeeded. But calling Face_Register(...) got an Exception. I inserted a 'return 99' statement for simplifying the issue. I found even it returns at the beginning, the access violation still occurred. So, why?
The calling sequence at C# program is as below:
......
int retCode = 0;
string retMessage = "";
CppImportInfo importInfo = new CppImportInfo();
importInfo.name = Data_In.name;
.........
importInfo.note = Data_In.note;
try
{
CDllWrapper.Face_Register(importInfo, ref retCode, ref retMessage);
}
catch (Exception Ex)
{
Trace.TraceMessage("[ Exception ] " + Ex.Message, true, ref log_cs);
return Return_Data;
}
......
In my opinion, I returned at the beginning without access to any variables, so why did it happen as Access Violation?