当从c++代码发送回调到c#代码时,访问冲突读取位置0x00000004
本文关键字:代码 访问冲突 读取 位置 0x00000004 c++ 回调 当从 | 更新日期: 2023-09-27 17:52:59
我正在开发一个windows 8手机应用程序,其中我需要从c++代码发送回调事件消息到c#中的UI代码。下面是c#代码,我在其中创建回调函数并将其指针发送给c++代码。c++代码可以使用这个函数指针向UI发送异步事件消息。
c#代码
public partial class MainPage
{
public MainPage()
{
InitializeComponent();
}
public delegate void CallBack([In][MarshalAs(UnmanagedType.LPStr)] string strParam);
[DllImport("Test.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void PerformActionWithCallBack(int x);
void DemonstrateCallBack()
{
int x;
CallBack callback_delegate = new CallBack(CallBackFunction);
// Converting callback_delegate into a function pointer that can be
// used in unmanaged code.
IntPtr intptr_delegate = Marshal.GetFunctionPointerForDelegate(callback_delegate);
//Getting the pointer into integer value and sending it to C++
x = (int)intptr_delegate;
testApp.PerformActionWithCallBack(x);
}
//CallBack function in which event messages will be received
void CallBackFunction([In][MarshalAs(UnmanagedType.LPStr)] string strParam)
{
if (Dispatcher.CheckAccess() == false)
{
//Updating the text block with the received message
Dispatcher.BeginInvoke(() => MyTextBlock.Text = strParam);
}
else
{
MyTextBlock.Text = "Update directly";
}
}
}
下面是将事件消息发送给c#的c++代码
//Callback function prototype
typedef void(__stdcall *PCallBack)(LPSTR s);
LPSTR cbMsg = NULL;
//Global instance callback function
PCallBack gUICallback;
void TestApp::PerformActionWithCallBack(int x)
{
//Assigning the received calllback function pointer from C# to global function pointer
gUICallback = (PCallBack)x;
}
//This function will send event messages to C#
void SendCallbackMsg()
{
while(1)
{
cbMsg = "Hello";
Sleep(100);
gUICallback(cbMsg);
cbMsg = "Hi";
Sleep(100);
gUICallback(cbMsg);
}
}
使用此代码,我能够成功地在c#中获得事件消息,但是在发送650-700回调后,我的应用程序给出了访问违反异常,之后没有任何工作。我怀疑我从c#传递函数指针到c++的方式,但无法解决它。
你的问题是,callback_delegate会被垃圾收集。
MSDN说
必须手动防止委托被垃圾收集器从托管代码中收集。垃圾收集器不跟踪对非托管代码的引用。
试试这个
public partial class MainPage
{
private CallBack _callBackDelegate = null;
public MainPage()
{
InitializeComponent();
}
public delegate void CallBack([In][MarshalAs(UnmanagedType.LPStr)] string strParam);
[DllImport("Test.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void PerformActionWithCallBack(int x);
void DemonstrateCallBack()
{
_callBackDelegate = new CallBack(CallBackFunction);
int x;
// Converting _callBackDelegate into a function pointer that can be
// used in unmanaged code.
IntPtr intptr_delegate = Marshal.GetFunctionPointerForDelegate(_callBackDelegate );
//Getting the pointer into integer value and sending it to C++
x = (int)intptr_delegate;
testApp.PerformActionWithCallBack(x);
}
//CallBack function in which event messages will be received
void CallBackFunction([In][MarshalAs(UnmanagedType.LPStr)] string strParam)
{
if (Dispatcher.CheckAccess() == false)
{
//Updating the text block with the received message
Dispatcher.BeginInvoke(() => MyTextBlock.Text = strParam);
}
else
{
MyTextBlock.Text = "Update directly";
}
}
}