调用C++;来自C#-System.AccessViolationException的dll 当试图调用C++中的C++ DLL时,我遇到了一个异常(System .Access Read Exchange异常),我不知道为什么。在X64中编译C++程序和C++项目。在Dll.cpp中,将导出Dll方法。在这个方法中,使用了一个名为OpcClient的类,它位于同一个项目中。我的IDE是Visual Studio Express 2012。 有人能帮忙吗
C#代码: C++代码(Dll.cpp):调用C++;来自C#-System.AccessViolationException的dll 当试图调用C++中的C++ DLL时,我遇到了一个异常(System .Access Read Exchange异常),我不知道为什么。在X64中编译C++程序和C++项目。在Dll.cpp中,将导出Dll方法。在这个方法中,使用了一个名为OpcClient的类,它位于同一个项目中。我的IDE是Visual Studio Express 2012。 有人能帮忙吗,c#,c++,pinvoke,dllimport,C#,C++,Pinvoke,Dllimport,C#代码: C++代码(Dll.cpp): #包括“stdafx.h” #包括 #包括“OpcClient.h” 使用名称空间std; extern“C”{//我们需要导出C接口 std::thread*output_thread=NULL; _declspec(dllexport)int uu cdecl Connect(常量字符*主机名,常量字符**标记名,int数量标记,int缓冲类型,int刷新率) { OpcClient*opc_client=OpcClient::getInstanc
#包括“stdafx.h”
#包括
#包括“OpcClient.h”
使用名称空间std;
extern“C”{//我们需要导出C接口
std::thread*output_thread=NULL;
_declspec(dllexport)int uu cdecl Connect(常量字符*主机名,常量字符**标记名,int数量标记,int缓冲类型,int刷新率)
{
OpcClient*opc_client=OpcClient::getInstance();
向量向量标记名;
对于(int i=0;i连接(主机名、vec_标记名、刷新率、缓冲区类型);
}
捕获(例外和e)
{
std::cout stop_uz=true;
输出线程->连接();
返回1;
}
}
我们不知道哪个函数失败。我建议你做一些调试。你有所有的代码。找出AV发生的地方。使用免费Express版来处理粗糙的互操作项目,可以和木匠使用瓶子敲击钉子。找到适合你的工作需要的工具,你需要调试本机C++代码,这样你就可以了。我想知道AVE的原因。我们不知道哪个功能失败。我建议你做一些调试。你有所有的代码。找出AV发生的地方。使用免费速成版来处理棘手的互操作项目就像木匠用瓶子敲钉子一样。获得工作所需的适当工具,你需要调试本机C++代码,这样你就可以诊断AVE.的原因了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace OCTTester
{
class Program
{
static void Main(string[] args)
{
string[] array = new string[2];
array[0] = "Tag1";
array[1] = "Tag2";
Connect("IBHSoftec.IBHOPC.DA", array, 2, 0, 100);
Add("Tag1", 10);
Stop();
}
[DllImport(@"OPCClient.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void Connect(string hostname, string[] tagnames, int amount_tags, int buffertype, int refreshrate);
[DllImport(@"OPCClient.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void Add(string tagname, double value);
[DllImport(@"OPCClient.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void Stop();
}
}
#include "stdafx.h"
#include <exception>
#include "OpcClient.h"
using namespace std;
extern "C" { // we need to export the C interface
std::thread *output_thread = NULL;
_declspec(dllexport) int __cdecl Connect(const char *hostname, const char **tagnames, int amount_tags, int buffertype, int refreshrate)
{
OpcClient* opc_client = OpcClient::getInstance();
vector<string> vec_tagnames;
for (int i = 0; i < amount_tags; i++)
{
vec_tagnames.push_back(tagnames[i]);
}
try
{
opc_client->Connect(hostname, vec_tagnames, refreshrate, buffertype);
}
catch(exception &e)
{
std::cout << e.what() << "\n";
return -1;
}
output_thread = new thread(&OpcClient::Start, opc_client);
return 0;
}
_declspec(dllexport) int __cdecl Add(const char *tagname, double value)
{
OpcClient* opc_client = OpcClient::getInstance();
opc_client->AddValueToBuffer(tagname, value);
return 1;
}
_declspec(dllexport) int __cdecl Stop(void)
{
OpcClient* opc_client = OpcClient::getInstance();
opc_client->stop_ = true;
output_thread->join();
return 1;
}
}