当从c++代码发送回调到c#代码时,访问冲突读取位置0x00000004
Access violation read location 0x00000004 when sending calllback from C++ code to C# code
我正在开发一个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";
}
}
}
相关文章:
- 写入位置0x0000000C时发生访问冲突
- 为什么示例代码访问IUnknown中已删除的内存
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 链表中写入访问冲突的未知原因
- C++中的openCV Mat访问冲突
- 在 C 代码中调用 Python 函数时第三次出现访问冲突写入位置错误
- 在链接的程序集文件中,我想从 c++ 调用代码访问变量.是否可以在不触发访问冲突的情况下执行此操作?
- 使用简单的 Indy 服务器代码进行访问冲突
- 使用JNI从Java调用本机(C++)函数时发生访问冲突:这是我的代码问题,还是已知问题
- 为什么此代码会导致访问冲突
- VS2010 访问冲突,同时使用 F10 或 F11 单步执行代码
- 导致访问冲突的最短C++代码
- 我的代码中存在死锁/访问冲突,即使我已经相互排除了它
- 为什么此代码会引发访问冲突异常
- c++返回元素在没有代码的行上给出访问冲突
- 在鼠标钩子代码中调用方法会导致访问冲突
- opengl / glsl / sdl代码中的c++访问冲突
- 在代码的不同部分中获取访问冲突错误
- 访问计算机指针时发生访问冲突(c系统异常代码:c0000005),导致应用程序崩溃
- 当从c++代码发送回调到c#代码时,访问冲突读取位置0x00000004