Question

I have been reading many threads about this but I can't fix it.

I'm trying to import this function (originally written in C++)

__declspec( dllexport ) int __stdcall GeekFunction(void *indices, 
    unsigned int *vertexRemap,
    unsigned int numIndices, 
    unsigned int numVertices, 
    int indexFormat,
    void *allocator);

To my C# project:

[DllImport("sce_psp2vertexcache.dll", 
           CallingConvention = CallingConvention.StdCall, 
           EntryPoint = "GeekFunction")]

unsafe private static extern int GeekFunction(
    [In, Out] IntPtr indices, 
    [Out] IntPtr vertexRemap, 
    [In] UInt32 numIndices, 
    [In] UInt32 numVertices, 
    [In] int indexFormat, 
    [In] void* allocator);

I am calling the function this way:

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();

Am I missing something?

Was it helpful?

Solution

Finally, I have found out what was happening.

I was calling several times to GeekFunction with the same handle:

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");

If I rebuild the handle between the calls I don't get the 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();
                        }

thank you for your help. The tests with the simpler function helped a lot.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top