将大型结构数组从C#unity脚本传递到C++;使用GCHandle的dll在C++;函数执行 我想把一个结构数组传递给C++的统一脚本中的C++本地插件。我做了如下的操作,我可以访问数据,但是在执行C++函数后,我的应用程序崩溃了,我不知道为什么。

将大型结构数组从C#unity脚本传递到C++;使用GCHandle的dll在C++;函数执行 我想把一个结构数组传递给C++的统一脚本中的C++本地插件。我做了如下的操作,我可以访问数据,但是在执行C++函数后,我的应用程序崩溃了,我不知道为什么。,c#,c++,unity3d,native,C#,C++,Unity3d,Native,C侧: C++方面: extern "C" EXPORT_API int getSomeInt() { return 42; } extern "C" EXPORT_API bool PopulateVerts(SimpleVector3* verts, int numofVert) { char buffer[50]; for (int i = 0; i < numofVert; i++) { sprintf(buffer, "x

C侧:

C++方面:

extern "C" EXPORT_API int getSomeInt()
 {
     return 42;
 }

 extern "C" EXPORT_API bool PopulateVerts(SimpleVector3* verts, int numofVert) {
     char buffer[50];
     for (int i = 0; i < numofVert; i++) {
         sprintf(buffer, "x %f , y %f , z %f \n nx %f , ny %f , nz %f ", (verts->Vx), 
             (verts->Vy), (verts->Vz), (verts->Nx), (verts->Ny), (verts->Nz));
         Debug(buffer);
         if(i < (numofVert-1)){
             verts++;
         }
     }
     return true;
 }
这是在全息镜头上运行的。我不确定我做错了什么,错误到底在哪里


谢谢。

问题可能在于返回类型
bool
。。。C++ <代码> BOOL 为1字节,C<代码>布尔O/COD>为4字节。 尝试:

然后,缓冲区的长度出现问题:

extern "C" EXPORT_API bool PopulateVerts(SimpleVector3* verts, int numofVert) {
    char buffer[250];
    for (int i = 0; i < numofVert; i++) {
        _snprintf(buffer, 250, "x %f , y %f , z %f \n nx %f , ny %f , nz %f ", (verts->Vx), 
            (verts->Vy), (verts->Vz), (verts->Nx), (verts->Ny), (verts->Nz));
        // See the security note at https://msdn.microsoft.com/en-us/library/2ts7cx93(v=vs.71).aspx
        buffer[sizeof(buffer) - 1] = '\0';

        Debug(buffer);
        if(i < (numofVert-1)){
            verts++;
        }
    }
    return true;
}
extern“C”EXPORT\u API bool PopulateVerts(SimpleVector3*verts,int numofert){
字符缓冲区[250];
对于(int i=0;iVx),
(垂直->垂直方向),(垂直->垂直方向),(垂直->Nx),(垂直->纽约),(垂直->新西兰));
//请参阅https://msdn.microsoft.com/en-us/library/2ts7cx93(v=vs.71)
缓冲区[sizeof(buffer)-1]='\0';
调试(缓冲);
如果(i<(努莫弗特-1)){
verts++;
}
}
返回true;
}

谢谢您的回复。我试过了,但还是失败了。实际上我也试过了,没有返回,在发布之前是void,但仍然崩溃了。@AliaAdly和
缓冲区[50]太小了。。。仅从复制粘贴输出到记事本,它大约是80个字符。为了确保使用长度参数的
\u snprintf
(但如果您使用
\u snprintf
,请阅读相关内容),请务必使用200左右。非常感谢!成功了!!问题确实出在缓冲区的大小上!
 the number is 42

 (Filename: C:/buildslave/unity/build/artifacts/generated/Metro/runtime/DebugBindings.gen.cpp Line: 51)

 CallBack : x 15.000000 , y 10.000000 , z 3.000000 
  nx 5.000000 , ny 10.000000 , nz 6.000000 

 (Filename: C:/buildslave/unity/build/artifacts/generated/Metro/runtime/DebugBindings.gen.cpp Line: 51)

 CallBack : x -0.011494 , y 0.069487 , z 0.090230 
  nx 9.988506 , ny 10.069487 , nz 10.090230 

 (Filename: C:/buildslave/unity/build/artifacts/generated/Metro/runtime/DebugBindings.gen.cpp Line: 51)

 The program '[2640] dllimporttrial.exe' has exited with code -1073740791 (0xc0000409).
[DllImport(DLL, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool PopulateVerts([In] IntPtr verts, int numOfVertices);
extern "C" EXPORT_API bool PopulateVerts(SimpleVector3* verts, int numofVert) {
    char buffer[250];
    for (int i = 0; i < numofVert; i++) {
        _snprintf(buffer, 250, "x %f , y %f , z %f \n nx %f , ny %f , nz %f ", (verts->Vx), 
            (verts->Vy), (verts->Vz), (verts->Nx), (verts->Ny), (verts->Nz));
        // See the security note at https://msdn.microsoft.com/en-us/library/2ts7cx93(v=vs.71).aspx
        buffer[sizeof(buffer) - 1] = '\0';

        Debug(buffer);
        if(i < (numofVert-1)){
            verts++;
        }
    }
    return true;
}