I need to call a function from unmanaged dll (C++) from my C# code. The function raise a callback function and I have to implement the function in my project.
The C++ code is:
/**
Definition: callback funcion of ReceiveStream
Input:
hHandle: return value of LoadFile function
stream: real infrared data stream
len: stream len
nTime: stream time
Return: =0 success < 0 fail
*/
typedef int (WINAPI *FILESTREAMCALLBACK)(HANDLE hHandle, void *stream, int len, int nTime);
// extern STREAMCALLBACK m_GetStream;
/**
Definition: to get real-time data stream
Input:
hHandle: return value of LoadFile function
GetStream: call back function
Return: =0 success < 0 fail
*/
int IFR_API ReceiveStream(HANDLE hHandle, FILESTREAMCALLBACK GetStream);
and my C# code is:
public delegate int FILESTREAMCALLBACK(IntPtr hHandle, IntPtr stream, int len, int nTime);
[SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
[DllImport("Test.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
public static extern int ReceiveStream(IntPtr hHandle, FILESTREAMCALLBACK GetStream);
private void button1_Click(object sender, EventArgs e)
{
int iFileStream = ReceiveStream(new IntPtr(iResult), TestFunc);
}
static int TestFunc(IntPtr hHandle, IntPtr stream, int len, int nTime)
{
MessageBox.Show("hello");
return 1;
}
Bu the TestFunc wont fire. How can I fix it? I would be happy if you let me know the right answer for that.