为了账号安全,请及时绑定邮箱和手机立即绑定

C#调用C++dll中方法,产生错误“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”

C#调用C++dll中方法,产生错误“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”

POPMUISE 2018-12-07 13:12:31
//Dll.h#ifdef BLOOMFILTER_API#else#define BLOOMFILTER_API extern "C" _declspec(dllimport)#endif#include<string>using namespace std;BLOOMFILTER_API int RSHash(string str, int nLen);//Dll.cpp/#include "stdafx.h"#define BLOOMFILTER_API extern "C"__declspec(dllexport)#include "VDDll.h"int RSHash(string str, int nLen)   {   int b = 378551;   int a = 63689;   int hash = 0;   for( int i = 0; i < nLen; i++)  {   hash = hash*a+str[i];   a = a*b;   }   return (hash & 0x7FFFFFFF);   }//在C#中调用 class testadd {  [DllImport("VDDLL.dll", EntryPoint = @"RSHash", CharSet = CharSet.Ansi)]  public static extern int RSHash(string str, int nLen);  }     static void Main(string[] args) {  Console.WriteLine(testadd.RSHash("asd",3)); }
查看完整描述

2 回答

?
杨魅力

TA贡献1811条经验 获得超5个赞

问题出在RSHash使用了stl对象,P/Invoke规范对这个调用形式没有民明确规定,估计会存在问题,最好使用Windows API的那些接口

C++调用可以是因为你用的都是VC的编译器,如果使用gcc的话也不行,srl这种结构的实现不同的语言和编译器还是有一定的差异的

查看完整回答
反对 回复 2019-01-21
?
Helenr

TA贡献1780条经验 获得超3个赞

怎么解决的给大家分享一下啊

查看完整回答
反对 回复 2019-01-21
  • 2 回答
  • 0 关注
  • 1054 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信