路人甲v01 2022-08-08 09:50 采纳率: 33.3%
浏览 35

c# 向c++ dll 传递string 报错

报错:0x00007FFA603F4FD9 处(位于 path.exe 中)引发的异常: Microsoft C++ 异常: std::bad_alloc,位于内存位置 0x00000068EBDFC930 处。
引发的异常:“System.Runtime.InteropServices.SEHException”(位于 path.exe 中)
“System.Runtime.InteropServices.SEHException”类型的未经处理的异常在 path.exe 中发生外部组件发生异常。

c++ dll
pass.h
extern "C" __declspec(dllexport)
void passToCPlus(char* path1,int len)
{
char* a = new char;
for (int i = 0; i < len; i++) { a[i] = path1[i]; }
string path(a);
cout<<path;
}

c# 调用dll
[DllImport("path.dll",CharSet=CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void passToCPlus ( char[] path,int len);

private void 测试字符ToolStripMenuItem_Click(object sender, EventArgs e)
{
string a = "c:\path\001";
char[] b = a.ToCharArray();
int len = a.Length;
passToCPlus (b,len);
}
查询了很多解决方案,无一能够解决。

  • 写回答

4条回答 默认 最新

  • 石岩Maple 2022-08-08 10:03
    关注

    把char[] path改成string path

    评论

报告相同问题?

问题事件

  • 创建了问题 8月8日

悬赏问题

  • ¥15 关于stm32hal库驱动ft6336触摸屏遇到的问题
  • ¥15 需要手写数字信号处理Dsp三个简单题 不用太复杂
  • ¥15 数字信号处理考试111
  • ¥100 关于#audobe audition#的问题,如何解决?
  • ¥15 allegro17.2生成bom表是空白的
  • ¥15 请问一下怎么打通CAN通讯
  • ¥20 如何在 rocky9.4 部署 CDH6.3.2?
  • ¥35 navicat将excel中的数据导入mysql出错
  • ¥15 rt-thread线程切换的问题
  • ¥15 高通uboot 打印ubi init err 22