المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Access Violation error in passing a pointer to an unmanagd DLL



C# Programming
04-29-2009, 03:11 PM
I am trying to use the rendered output of a video player in my C# code. The program (which is the VLC media player) is written in plain C and is wrapped in a DLL.
I should pass pointers to two callback functions (dolock and unlock ) as string arguments (argv ) to the player when it is instantiated through a DLL call. here is the function signatures in C:

void *dolock (void *struct)
void unlock(void * struct)

(the struct arguments are not used in my case).
they appear in my C# code as:

public static IntPtr dolock(IntPtr ctx)
public static void unlock(IntPtr ctx)

I use delegates and Marshal.GetFunctionPointerForDelegate to obtain the function pointers.

This works perfect and I can see in the debugger that the player successfully invokes my dolock function. But, an exception is raised after the function invocation, when the dolock function returns with a pointer to a buffer. I can not understand if it is because the player is not allowed to write in the memory which is allocated in my program or I am returning a wrong pointer. The error is a famous one:

The instruction at ... referenced memory at "0x000000". The memory could not be "written" .


I have tried different ways to allocate the memory buffer (which I should return its pointer in dolock), all failed with the same error.

* Allocating space in the unmanaged section of the program memory using AllocHGlobal.
* using stackalloc to allocate the buffer in stack (unmovable by GC).
* using Bitmap and BitmapData GDI+ classes and returning the bitmapdata.scan0 pointer after locking the bitmap.

Any idea what might be the cause?