Hello everyone
I have a project that transfers data back and forth to a third party software. The communication is made possible via a DLL. One of the functions in the DLL is to send a stream of data with a known length when the data is available (data is a frame). In order for my code to get this data properly I have to use a pointer to the frame. I have tried to use a string variable but I get garbage.
In order to use a pointer in c# I have to use it in unsafe context.
Here is how I create a delegate and point it to the dll:
unsafe delegate int delOnNewFrame(void *a, int b);
delOnNewFrame OnNewFrame;
[System.Runtime.InteropServices.DllImport("someDLL.DLL", EntryPoint = "_SetCallback_OnNewFrame@4")]
static extern int SetCallback_OnNewFrame(delOnNewFrame ptr);
Everything compile other than the part that I set the call back to the function in my code (onNew_Frame). Right here:
OnNewFrame = new delOnNewFrame(OnNew_Frame);
SetCallback_OnNewFrame(OnNewFrame);
"new delOnNewFrame(OnNew_Frame);" becomes underlined and I get:
"error CS0214: Pointers and fixed size buffers may only be used in an unsafe context"
Here is my function which compiles fine as well:
private unsafe int OnNew_Frame(char * pBuffer, int frameCounter)
{
return New_Frame((short*)(pBuffer), frameCounter);
}
My question is how do I correct this issue? how do I assign onNewFrame in unsafe context?
Is there a better way of using pointers in C# in this case?
The code is in .Net 4.0 VS2010. I have checked the "compile unsafe code" in the build option.
Thanks in advance.