2016-02-19 36 views
-1

我创建了一个C++ .dll库,用于Unity5 C#程序。使用从DLL的CopyMemory并抛出访问冲突错误

但是,当我从Unity编辑器运行程序并尝试调用smCopy时,Unity崩溃并尝试发送错误报告;相反,我将该错误追踪为smCopy发生的访问冲突错误。

.dll文件库允许我连接到一台服务器,并通过此功能,将数据复制到一个给定的参数:

C++: 
void DLL_API smCopy(const VOID *dest, SIZE_T len) 
{ 
    CopyMemory((PVOID)(dest), (PVOID)(mapBuffer), len); 
} 

在我的统一脚本,我导入功能:

C#: 
[DllImport(DLL_NAME, EntryPoint = "smCopy")] 
private static extern void smCopyData(IntPtr dest, IntPtr len); 

并最终调用它:

C#: 
{ 
// create IntPtr for message 
    IntPtr msg_intptr = new IntPtr(); 

    smCopyData(msg_intptr, (IntPtr)Marshal.SizeOf(typeof(smMsg))); 

    // create actual data struct 
     // smMsg is a struct referenced in both .dll & Unity script 
    smMsg data_msg = new smMsg(); 

    // Set POINTER data to struct 
    Marshal.PtrToStructure(msg_intptr, data_msg); 
} 

我调试并跟踪从smCopyData发生的错误,与在日志中找到以下错误:

`VCRUNTIME140D.dll caused an Access Violation (0xc0000005) in module VCRUNTIME140D.dll at 0033:b4761c89.` 

.dll文件可与其他功能,我能够从这个发生错误连接到服务器成功,除了。


现在我不知道接下来可以做什么,因为我需要这个函数将数据从服务器复制到结构中。

我该如何将内存从服务器(mapBuffer)复制到C#中的参数变量?


Reference to Marshal & IntPtr

Editor.log堆栈跟踪

0x00007FFBB70A1C89 (VCRUNTIME140D) memcpy 
0x00007FFBB72819D9 (SharedMemoryClientDLL) [l:\projects\google\chapstick\software\working\abner\misc proj\vs\sharedmemoryclientdll\sharedmemoryclientdll\smclibrary.cpp:151] smCopy 
0x0000000012B65B29 (Mono JIT Code) (wrapper managed-to-native) SMCScript:smCopyData (intptr,intptr) 
0x0000000012B65793 (Mono JIT Code) [L:\Projects\Google\chapstick\Software\Working\Abner\Misc Proj\Unity\ChapstickProject\Assets\Scripts\SMCScript.cs:202] SMCScript:GetData() 
0x0000000012B62CA3 (Mono JIT Code) [L:\Projects\Google\chapstick\Software\Working\Abner\Misc Proj\Unity\ChapstickProject\Assets\Scripts\SMCScript.cs:176] SMCScript:CheckForEvent() 
0x0000000010B69232 (Mono JIT Code) [L:\Projects\Google\chapstick\Software\Working\Abner\Misc Proj\Unity\ChapstickProject\Assets\Scripts\SMCScript.cs:150] SMCScript:Update() 
0x00000000007D68E2 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr) 
0x00007FFB9B5F41BF (mono) [c:\buildslave\mono-runtime-and-classlibs\build\mono\mini\mini.c:4937] mono_jit_runtime_invoke 
0x00007FFB9B548435 (mono) [c:\buildslave\mono-runtime-and-classlibs\build\mono\metadata\object.c:2623] mono_runtime_invoke 
0x000000014033E20F (Unity) scripting_method_invoke 
+0

那么,看看C++代码,这不是问题。所以它必须是你如何编组/接口到C#中的函数# – PaulMcKenzie

+0

在将它传递给C++ DLL之前,你不需要先创建一个缓冲区吗?我确信创建一个IntPtr()不会这样做。 – PaulMcKenzie

+0

我很确定这是*问题,并且您未能为将要复制数据的缓冲区分配内存。如何在C#中做到这一点 - 你需要做的研究。只是创建一个IntPtr()什么也不做。 – PaulMcKenzie

回答

0

您需要分配一个缓冲区,它的指针传递给函数。您只是传递一个空指针,以便cpp函数正在尝试写入地址0,这会导致访问冲突异常。

var len = Marshal.SizeOf(typeof(smMsg)); 
IntPtr msg_intptr = Marshal.AllocHGlobal(len); 
try { 
    // call the function, convert output to struct, etc... 
} 
finally { 
    Marshal.FreeHGlobal(msg_intptr); 
} 
+0

非常感谢!我误解了IntPtr的确切使用和实现,但我现在看到了。 – Abner