PostMessage(),SendMessage不工作在ATL dll(事件处理) [英] PostMessage(), SendMessage not working in ATL dll (event handling)
问题描述
对不起,我的英语技能很低。
sorry, my english skill is very low.
我做了一个ATL(C ++)dll。并由VB处理。
i在基础代码下。
i make a ATL(C++) dll. and handled by VB. i make under base code.
WaitAndReadData,Thread_WaitAndReadData正在工作。
WaitAndReadData, Thread_WaitAndReadData is working.
但是:: SendMessage ,:: PostMessage在Thread_WaitAndReadData或WaitAndReadData中不起作用。
和断点在Get_Data_Messagehandler中不起作用。
(+另一个函数调用)
but, ::SendMessage, ::PostMessage is not working in Thread_WaitAndReadData or WaitAndReadData. and breakpoint not working in Get_Data_Messagehandler. (+ another function call.)
#define WM_SERVERTHREADFIREEVENT (WM_USER+2)
BEGIN_MSG_MAP(CHello)
CHAIN_MSG_MAP(CComControl<CHello>)
MESSAGE_HANDLER(WM_SERVERTHREADFIREEVENT, GetData_Messagehandler)
DEFAULT_REFLECTION_HANDLER()
END_MSG_MAP()
-
static DWORD WINAPI Thread_WaitAndReadData(LPVOID pParam)
-
STDMETHODIMP CHello::WaitAndReadData(BSTR* ret_Result)
{
// TODO: Add your implementation code here
DWORD dwThreadID;
thread = CreateThread(NULL, 0, Thread_WaitAndReadData, (LPVOID)this, 0, &dwThreadID);
return S_OK;
}
-
DWORD WINAPI CHello::Thread_WaitAndReadData(LPVOID pParam)
{
CHello* hello = (CHello*)pParam;
::SendMessage(hello->m_hWnd, WM_SERVERTHREADFIREEVENT, (WPARAM)NULL, (LPARAM)NULL);
return S_OK;
}
-
LRESULT CHello::GetData_Messagehandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
MessageBox(L"GetData_Messagehandler", L"asd", MB_OK);
return 0;
}
推荐答案
-
即使MSDN声明在交叉进程发送中没有编组
WM_USER + x
消息,如果我的记忆为我服务,你可能会遇到麻烦,线程发送以及。在这种情况下,使用RegisterWindowMessage
API来获取可发送WM_xxx
标识符,而不是使用#define
Even though MSDN states that there is no marshaling of
WM_USER + x
messages in cross-process sending, if my memory serves me right you might have troubles with cross-thread sending as well. In this case useRegisterWindowMessage
API to obtain "sendable"WM_xxx
identifier rather than harcoding it using a#define
不要使用裸机 CreateThread
,使用 AtlCreateThread
代替(或 _beginthreadex
)。 查看原因。
Don't use bare CreateThread
, use AtlCreateThread
instead (or, _beginthreadex
). See why.
在窗口线程上不接收消息的另一个原因是线程上的线程死锁或窗口创建,以后不再有消息泵,在这两种情况下都是一条消息可能会发送,但没有派到窗口。您还可以使用Spy ++工具(Visual Studio Comment\Tools目录中的spyxx.exe)来确保相关消息确实被发送到窗口。
Another reason to not receive messages on window thread is thread deadlock or window creation on thread that does not have a message pump later on, in both cases a message might be sent but there is no dispatching it to window. You can also use Spy++ tool (spyxx.exe in Visual Studio Comment\Tools directory) to make sure that the message in question is indeed being sent to the window.
这篇关于PostMessage(),SendMessage不工作在ATL dll(事件处理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!