调用非托管C ++ DLL时C ++ .NET DLL挂起 [英] C++ .NET DLL hangs when calling unmanaged C++ DLL

查看:84
本文介绍了调用非托管C ++ DLL时C ++ .NET DLL挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

这是一个远景,但是经过2天的编码,我认为我无法解决我的问题,这是...

我有一个C#GUI,它引用对基础C ++(非托管)DLL进行调用的C ++ .NET DLL(托管).我的C ++ .NET DLL中的方法之一启动了一个线程,该线程调用我的基础C ++ DLL,该代码如下所示:

Hi All,

This is a long shot but after 2 days of coding I figure I''m unable to fix my problem, which is...

I have a C# GUI that references a C++ .NET DLL (managed) that makes calls to underlying C++ (unmanaged) DLL''s. One of the methods in my C++ .NET DLL starts a thread that calls to my underlying C++ DLL, that code looks like this:

m_oThread = gcnew Thread(gcnew ParameterizedThreadStart(this, &MyClass::MethodToRunOnThread));
m_oThread->Start(MyParameter);





void MyClass::MethodToRunOnThread(Object^ step)
{ //Call to underlying C++ DLL (unmanaged).
}



问题是我的基础DLL调用CWinThread::PumpMessage,当我碰到那一行时,我的应用程序崩溃了.感觉上像是某种定时/锁定问题,因为如果我执行代码而不是运行代码,它有时会超出该行.

更令人困惑的是,如果我在不将其放在线程上的情况下对其底层C ++ DLL进行调用,它将运行良好,即



The problem is my underlying DLL calls CWinThread::PumpMessage, and my app crashes when I hit that line. It feels like some kind of timing/locking problems because if I step the code as opposed to running it it sometimes gets past that line.

To add more confusion, if I make the call to my underlying C++ DLL without putting it on it''s on thread it runs fine, i.e.

MethodToRunOnThread(MyParameter);



希望我提供了足够的信息让您窥视!
问候,
克里斯



I hope I''ve provided enough info to get you peeps thinking!
Regards,
Chris

推荐答案

我怀疑问题是您的工作线程没有消息循环.因此,您不应从中调用PumpMessage .如果您无法轻松清理代码,可以尝试的一件事是先调用PeekMessage ,然后再调用PumpMessage来检查消息.


~~~~~~~

与其创建托管工作线程,不如尝试使用MFC的线程选项创建UI线程.您调用的MFC代码似乎依赖于在UI线程中被调用的假设.那可能会解决您的问题.


~~~~~~~~~

回应您的评论:是的,但是从非托管块创建MFC线程(使用#pragma unmanaged),然后让托管方法调用创建MFC线程的非托管函数.
I suspect the problem is that your worker thread does not have a message loop. So you should not be calling PumpMessage from it. One thing you could try, if you can''t clean up the code easily, is to call PeekMessage first to check for messages before calling PumpMessage.


~~~~~~~

Instead of creating a managed worker thread, try creating a UI thread using MFC''s threading options. It seems the MFC code you call rely on the assumption that it''s being called in a UI thread. That may fix your problem.


~~~~~~~~~

In response to your comment: Yes, but create the MFC thread from an unmanaged block (using #pragma unmanaged) and then have a managed method call the unmanaged function that creates the MFC thread. That should work for you.


如果所有内容都与PumpMessage调用有关,那么您不能删除该调用吗?

您是在当前线程还是在主应用程序线程上进行调用?在前一种情况下,应该不会有任何问题,但是在后一种情况下,您将从两个不同的线程中泵出主线程的消息……这会导致一些意外的结果.
If everything is related to the PumpMessage call, then can''t you just remove the call?

Are you doing the call on the current thread or the main app''s thread? In the former case, there shouldn''t be any problem, but in the later you would be pumping the main thread''s messages from 2 different threads... it would lead to some unexpected results.


这篇关于调用非托管C ++ DLL时C ++ .NET DLL挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆