AccessViolationException从托管C (ASP.NET的包装器)调用本机C 代码
AccessViolationException calling native C++ code from managed C++ (wrapper for ASP.NET)
我有一个ASP.NET Web应用程序,需要访问本机C DLL中的函数。为此,我使用托管的C DLL包装了本机C 代码。但是,从托管代码调用本机函数会导致System.AccessViolationException
。我不需要本机代码,但它将写入文件系统(日志(,这是我的第一个猜测,但是根据其他答案,AccessViolationException
是由内存问题引起的。
代码非常简单,这是未交换版本:
#include <msclrmarshal_cppstd.h>
#define AS_NATIVE_STRING(managed_string) msclr::interop::marshal_as<std::string>(managed_string)
#define AS_MANAGED_STRING(native_string) msclr::interop::marshal_as<String^>(native_string)
ReturnStruct ManagedClass::execute_managed(String ^param1, String ^param2)
{
auto param1_native = AS_NATIVE_STRING(param1);
auto param2_native = AS_NATIVE_STRING(param2);
// here I get the exception:
auto result = _wrapped_api->execute_native(param1_native, param2_native);
if (is_error_string(result.first))
throw gcnew System::Exception(AS_MANAGED_STRING(result.second));
auto result1 = AS_MANAGED_STRING(result.first);
auto result2 = AS_MANAGED_STRING(result.second);
return ReturnStruct(result1, result2);
}
有什么暗示可能导致它?我确实确实看过类似的问题,但是似乎没有答案真的很适合我的问题。
编辑:使用HandleProcessCorruptedStateExceptionsAttribute
我能够确定AccessViolationException
的错误消息:"尝试读取或写下受保护的内存。这通常表明其他内存是损坏的。"
进一步调查后,我得出的结论是,我基本上无法在我的末尾解决这个问题。我本人创建了DLL的模拟版本,以便能够介入并查看类型转换是否正确,并且参数包含正确的字符串。就是这样。这并没有真正帮助我,但是也许其他人可能会在他/她的代码中找到问题。
我只会联系包装API的作者。
相关文章:
- c++\CLI dll包装器,用于调用c++类中的虚拟成员
- C++,当函子不是一个选项时,我如何编写带有自定义函数调用的模板化 RAII 包装器?
- C 函数调用包装器包含类成员功能作为模板参数
- 尝试构造包装器测量函数调用时间时出现问题
- 使用带有 MEX 包装器的帮助程序 C 文件从 MATLAB 2016 调用C++代码时出现问题
- 模板化类包装成员函数调用的行为
- 带有默认参数的宏包装函数调用
- AccessViolationException从托管C (ASP.NET的包装器)调用本机C 代码
- 如何在 C++14 中编写用于调用 Fortran 函数的通用包装器(按引用调用 --按值调用>)
- 包装标准::线程调用函数
- 在没有包装程序类的情况下,在ActiveX接口上调用方法
- 如何检测处理程序是否是 ASIO 链包装并通过链调用它?
- C# 使用包装类中的字符串参数调用 C++ 方法
- 运行并发的CUDA内核,从C 包装器功能调用
- 使用SWIG包装C 类,该类调用另一个对象成员函数
- 包装函数只有在链接为静态时才从链接库调用
- 返回一个 std::函数包装的 lambda,该 lambda 调用指向成员函数的指定指针
- 从包装器调用导出C++ C 函数时LNK2028错误
- 用SWIG包装对象从C++调用Python函数的最干净方法是什么
- 如何用try/catch块包装调用