【发布时间】:2011-06-27 09:05:59
【问题描述】:
我有这个错误
附加信息:试图读取或写入受保护的内存。这通常表明其他内存已损坏。
你知道为什么吗?我真的卡住了……
我的代码:
原生 c++
extern "C" void __declspec(dllexport) Mafonc(string nom);
void __declspec(dllexport) Mafonc(string nom)
{
string tom = nom;
}
c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security;
namespace TradeInterface
{
static class Program
{
[DllImport("TradeEngine.dll", CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall,
ExactSpelling = true),
SuppressUnmanagedCodeSecurity]
public static extern void Mafonc(string nom);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Mafonc("E:\\DossierProjet");
Application.Run(new Form1());
}
}
}
【问题讨论】:
-
非托管DLL中字符串的定义是什么?如果它是一个字符指针,正如我猜测的那样,那么你不能只是复制指针并在以后使用它,而是需要复制字符串本身(strcpy 或类似)。
-
我不明白你在说什么。在我的 C++ 代码中,字符串是来自 String.h 的真实字符串。
标签: c# c++ dll memory-leaks unmanaged