从C#调用C ++ DLL导致AccessViolationException

时间:2015-07-06 06:41:34

标签: c# c++ dll

我有一个C ++ DLL,我试图用C#调用。

这是C ++原型:

bool t_init(const char* a, const char* b, const char* c, const char* d);

这是我的C#声明:

[DllImport("a.dll")]
public static extern bool t_init(string a, string b, string c, string d);

但是,在代码中调用此函数会导致此访问冲突异常:

System.AccessViolationException was unhandled
HResult=-2147467261
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=WindowsFormsApplication1
StackTrace:
   at Test.t_init(String a, String b, String c, String d)
   at WindowsFormsApplication1.Form1..ctor() in c:\Users\rfonseka\Documents\Visual Studio 2013\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 20
   at WindowsFormsApplication1.Program.Main() in c:\Users\rfonseka\Documents\Visual Studio 2013\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 19
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
InnerException: 

有什么想法吗?我知道DLL加载好了,因为我调用了一个简单的函数,只是将版本号作为整数返回并成功执行。

0 个答案:

没有答案