我使用外部导出构建了一个c ++ dll,以便从我的C#程序中调用它。调用对大多数函数都很有效,但是当我需要将一些字符串从C#传递给C ++时会出现问题。
我将它们作为普通字符串传递,并将它们作为const char *接收。它们都很好,所有的数据都在那里,但后来我继续从这些char数组中定义几个字符串。代码继续没有任何问题,直到我退出函数。然后它抛出一个异常说,最后定义的std :: string周围的堆栈已损坏,我真的不确定为什么会这样。我已经尝试了很多方法来定义字符串:复制它们,从P / Invoke定义中更改编码。
我从定时器线程调用此函数的一些额外信息;我发现这一点,在线程上std :: string可能会出现一些问题。这是在VS 2012 for x86中编译的多CPU机器上完成的。还附上以下相关代码
#define OPENCV2P4
#include "openfabmap.hpp"
#include <fstream>
#ifdef OPENCV2P4
#include <opencv2/nonfree/nonfree.hpp>
#endif
#include <stdio.h>
extern "C"
{
__declspec(dllexport) int generateBOWImageDescs( const char* _dataPath, const char* _bowImageDescPath,const char* _vocabPath ,int minWords)
{
std::string dataPath(_dataPath);
std::string bowImageDescPath( _bowImageDescPath);
std::string vocabPath(_vocabPath);
cv::FileStorage fs;
//ensure not overwriting training data
std::ifstream checker;
checker.open(bowImageDescPath.c_str());
if(checker.is_open()) {
checker.close();
return -1;
}
}
来自C#的电话
[DllImport(@"FabMap\FabMapCPP.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int generateBOWImageDescs( string _dataPath, string _bowImageDescPath, string _vocabPath, int minWords);
非常感谢任何帮助。这里也是第一个问题,所以对这个问题的错误陈述的任何评论都会受到赞赏。
编辑:好玩的事实问题是$ cv :: FileStorage fs;我基本上删除了它,它开始工作,可能是由于分配不当或其他原因。
所以这意味着opencv文件存储导致问题,仍然不确定原因。
答案 0 :(得分:-1)
看看这两个链接:
http://support.microsoft.com/kb/311259
http://msdn.microsoft.com/en-us/library/7b620dhe.aspx
基本上char *是C#中的IntPtr而不是字符串。