C#回调函数中的非托管c ++ DLL

时间:2014-12-23 15:32:44

标签: c# c++ callback unmanaged

我需要从我的C#代码中调用非托管dll(C ++)中的函数。该函数引发了一个回调函数,我必须在我的项目中实现该函数。

C ++代码是:

/**
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);

我的C#代码是:

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 TestFunc不会开火。我该如何解决?如果你让我知道正确答案,我会很高兴。

0 个答案:

没有答案