Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:尝试调用导入的C+时出现AccessViolationException+;功能_C#_C++_Dllimport_Access Violation - Fatal编程技术网

C#:尝试调用导入的C+时出现AccessViolationException+;功能

C#:尝试调用导入的C+时出现AccessViolationException+;功能,c#,c++,dllimport,access-violation,C#,C++,Dllimport,Access Violation,我已经读了很多关于这方面的文章,但我无法修复它 我正在尝试导入此函数(最初是用C++编写的) 到我的C#项目: 我这样调用函数: UInt32[] vertexRemap = new UInt32[locs.data.Length * 6]; GCHandle handleVertexRemap = GCHandle.Alloc(vertexRemap, GCHandleType.Pinned); GCHandle handleIndexdata = GCHandle.Alloc(indexda

我已经读了很多关于这方面的文章,但我无法修复它

我正在尝试导入此函数(最初是用C++编写的)

到我的C#项目:

我这样调用函数:

UInt32[] vertexRemap = new UInt32[locs.data.Length * 6];
GCHandle handleVertexRemap = GCHandle.Alloc(vertexRemap, GCHandleType.Pinned);
GCHandle handleIndexdata = GCHandle.Alloc(indexdata, GCHandleType.Pinned);

if (GeekFunction(
         GCHandle.ToIntPtr(handleIndexdata),
         GCHandle.ToIntPtr(handleVertexRemap),
         Convert.ToUInt32(indexdata.Length),
         Convert.ToUInt32(locs.data.Length), 1, null) != 0)
                            StatusOutput.FatalError("Geekfunction Failed");

handleIndexdata.Free();

我错过了什么吗?

最后,我发现了发生的事情

我用同一个句柄多次调用GeekFunction:

if (GeekFunction(
         GCHandle.ToIntPtr(handleIndexdata),
         GCHandle.ToIntPtr(handleVertexRemap),
         Convert.ToUInt32(indexdata.Length),
         Convert.ToUInt32(locs.data.Length), 1, null) != 0)
                            StatusOutput.FatalError("Geekfunction Failed");

...

if (GeekFunction(
         GCHandle.ToIntPtr(handleIndexdata),
         GCHandle.ToIntPtr(handleVertexRemap),
         Convert.ToUInt32(indexdata.Length),
         Convert.ToUInt32(locs.data.Length), 1, null) != 0)
                            StatusOutput.FatalError("Geekfunction Failed");
如果在调用之间重新生成句柄,则不会得到AccessViolationException:

try
                        {
                            if (GeekFunction(
                                handleBoneIndices.AddrOfPinnedObject(),
                                handleVertexRemap.AddrOfPinnedObject(),
                                Convert.ToUInt32(boneindices.data.Length),
                                Convert.ToUInt32(sizeof(VertexData.Index4)), null) != 0)
                                StatusOutput.FatalError("GeekFunctionFailed: boneIndices");
                        }
                        finally
                        {
                            handleBoneIndices.Free();
                            handleVertexRemap.Free();
                        }

**handleVertexRemap = GCHandle.Alloc(vertexRemap, GCHandleType.Pinned);**

try
                        {
                            if (scePsp2VertexCacheApplyVertexRemapping(
                                handleBoneIndices.AddrOfPinnedObject(),
                                handleVertexRemap.AddrOfPinnedObject(),
                                Convert.ToUInt32(boneindices.data.Length),
                                Convert.ToUInt32(sizeof(VertexData.Index4)), null) != 0)
                                StatusOutput.FatalError("scePsp2VertexCacheApplyVertexRemapping Failed: boneIndices");
                        }
                        finally
                        {
                            handleBoneIndices.Free();
                            handleVertexRemap.Free();
                        }

谢谢你的帮助。使用更简单的函数进行的测试帮助很大。

没有。您必须找到编写此函数的极客,并向他寻求帮助。很好,我看你别得很好。你试过调试它吗?是否有证据表明该函数已被调用——是否可以向其添加插装(良好的旧printf调试)?可以尝试从同一个DLL调用“simpler”函数吗?(即,添加一个零参数函数,调用它,并确保它使用上述技术运行)。我要尝试一下,thanksHans Passant,我不允许显示此代码,但请相信我,这是非常极客的:PI尝试了一个更简单的函数,似乎异常是由这些调用引起的:“GCHandle.ToIntPtr(handleIndexdata)”。如果使用handleIndexdata.AddrOfPinnedObject()可以正常工作。因此,我已将该函数的所有引用更改为AddRofPindedObject。现在我得到一个“System.ArgumentException:对象包含非基本数据或不可删除数据”。我将尝试在更简单的函数中重现异常,以了解发生了什么。
if (GeekFunction(
         GCHandle.ToIntPtr(handleIndexdata),
         GCHandle.ToIntPtr(handleVertexRemap),
         Convert.ToUInt32(indexdata.Length),
         Convert.ToUInt32(locs.data.Length), 1, null) != 0)
                            StatusOutput.FatalError("Geekfunction Failed");

...

if (GeekFunction(
         GCHandle.ToIntPtr(handleIndexdata),
         GCHandle.ToIntPtr(handleVertexRemap),
         Convert.ToUInt32(indexdata.Length),
         Convert.ToUInt32(locs.data.Length), 1, null) != 0)
                            StatusOutput.FatalError("Geekfunction Failed");
try
                        {
                            if (GeekFunction(
                                handleBoneIndices.AddrOfPinnedObject(),
                                handleVertexRemap.AddrOfPinnedObject(),
                                Convert.ToUInt32(boneindices.data.Length),
                                Convert.ToUInt32(sizeof(VertexData.Index4)), null) != 0)
                                StatusOutput.FatalError("GeekFunctionFailed: boneIndices");
                        }
                        finally
                        {
                            handleBoneIndices.Free();
                            handleVertexRemap.Free();
                        }

**handleVertexRemap = GCHandle.Alloc(vertexRemap, GCHandleType.Pinned);**

try
                        {
                            if (scePsp2VertexCacheApplyVertexRemapping(
                                handleBoneIndices.AddrOfPinnedObject(),
                                handleVertexRemap.AddrOfPinnedObject(),
                                Convert.ToUInt32(boneindices.data.Length),
                                Convert.ToUInt32(sizeof(VertexData.Index4)), null) != 0)
                                StatusOutput.FatalError("scePsp2VertexCacheApplyVertexRemapping Failed: boneIndices");
                        }
                        finally
                        {
                            handleBoneIndices.Free();
                            handleVertexRemap.Free();
                        }