导出 c++ 函数并使用 c# System.AccessViolationException 中的函数



您好,我尝试从 C++ 导出函数并尝试在 C# 上运行它,但我收到一个错误,System.AccessViolationException: An attempt to read or write protected memory. This is usually an indication that the other memory is corrupted.,我这样做要归功于微软的例子,但我得到了这个错误,我找不到任何关于这个的信息。我在任何地方都错了吗?

C++头文件

#ifndef KKKKK_V2_EXPORTS
#define KKKKK_V2_API __declspec(dllexport)
#else
#define KKKKK_V2_API __declspec(dllimport)
#endif
extern "C" KKKKK_V2_API can* create_can(string ip, int port);
extern "C" KKKKK_V2_API int connectCan(can* _c);

extern "C" KKKKK_V2_API string browseCan(can* _c,int x);
extern "C" KKKKK_V2_API void dataset_browseCan(can* _c);
extern "C" KKKKK_V2_API void dataset_signals_readingCan(can* _c);
extern "C" KKKKK_V2_API void GI_Can(can* _c);
extern "C" KKKKK_V2_API void ConcludeCan(can* _c);

CPP 文件

can* create_can(string ip, int port) {
const char* my_ip=ip.c_str();
can* _c = new can();
_c->connection = new connect_tcp((char*)my_ip, port);
_c->s = _c->connection->ConnectWithTcp();
_c->list = new LinkedList * [1000];
return _c;
}
int connectCan(can* _c) {
cotp_connection(_c->cotp, _c->connection, _c->response, _c->list, _c->s);
return 1;
}
string browseCan(can* _c,int x) {
int i = 0;
initiate_request(_c->cotp, _c->mms_obj, _c->connection, _c->response, _c->list, _c->size_encoder, _c->s);
confirmed_request(_c->cotp, _c->mms_obj, _c->connection, _c->response, _c->list, _c->size_encoder, _c->s);
continue_confirmed_request_with_more_follows(_c->cotp, _c->mms_obj, _c->connection, _c->response, _c->list, _c->size_encoder, _c->s);
all_object_browse(_c->cotp, _c->mms_obj, _c->connection, _c->response, _c->list, _c->size_encoder, _c->s);
auto it = _c->response.object_list.begin();
while (i < x) {
it++;
i++;
}
return *it; 
}
void dataset_browseCan(can* _c) {
dataset_browse(_c->cotp, _c->mms_obj, _c->connection, _c->response, _c->list, _c->size_encoder, _c->s);
dataset_signals_browse(_c->cotp, _c->mms_obj, _c->connection, _c->response, _c->list, _c->size_encoder, _c->s); 
_c->signals = new Signals[_c->response.real_signals_and_values.size()];
}
void dataset_signals_readingCan(can* _c) {
dataset_signals_reading(_c->cotp, _c->mms_obj, _c->connection, _c->response, _c->list, _c->size_encoder, _c->s, _c->signals);
}
void GI_Can(can* _c) {
write_data_pins(_c->cotp, _c->mms_obj, _c->connection, _c->response, _c->list, _c->size_encoder, _c->s, _c->signals);
general_information(_c->cotp, _c->mms_obj, _c->connection, _c->response, _c->list, _c->size_encoder, _c->s, _c->signals);
}
void ConcludeCan(can* _c) {
conclude(_c->cotp, _c->mms_obj, _c->connection, _c->response, _c->list, _c->size_encoder, _c->s, _c->signals);
release(_c->cotp, _c->mms_obj, _c->connection, _c->response, _c->list, _c->size_encoder, _c->s, _c->signals);
}

C# 文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace cppDllTest
{
class Program
{
[DllImport(@"C:Usersserhan.erkovansourcereposkkkkk_v2Debugkkkkk_v2.dll", CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr create_can(string my_ip_address, int my_port);
IntPtr _c;
_c = create_can("10.6.35.225", 102);   //ERROR IS HERE
}
}

我放了我的 cpp 文件,但我知道这不是必需的。也许我在 cpp 文件中错了,因为这个 ı 放那个。

在依赖沃克,我导出的所有函数在"E"部分似乎都是灰色的。意思是" 驻留在所选模块中的 C 导出函数"。

这里的问题是,正如这里解释的那样 https://stackoverflow.com/a/20752021/3852949:

不能跨互操作边界传递C++ std::string。不能在 C# 代码中创建其中一个

您可以更改C++函数签名,使其接受const char *(即can* create_can(const char * ip, int port)(,也可以创建一个调用原始函数的适配器函数:

can* create_can_adapter(const char * ip, int port)
{
std::string s(ip);
return (create_can(s, port));
}

相关内容

  • 没有找到相关文章

最新更新