关于 sendmessage发送消息后 peekmessage获取不到的问题

D_Future 2015-09-29 04:00:25

#define WM_COMM_RXCHAR WM_USER+7

DWORD WINAPI ThreadProc(LPVOID lpParam)
{
CSouthh* cs = (CSouthh*)lpParam;
HWND m_hwnd = getOsgHwnd(cs->getview());
while (m_hwnd)
{
MSG msg;
if (PeekMessage(&msg, m_hwnd, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break ;
if (msg.message == WM_COMM_RXCHAR)
{
int a = 1;
}
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
return 0;
}
void main()
DWORD threadID;
HANDLE hThread;
hThread = CreateThread(NULL,0,ThreadProc,cs,0,&threadID); // 创建线程

::Sleep(1000);
string str("$000.0,0002,0.5\n");
char* a = (char*)str.data();
PostMessage(getOsgHwnd(viewer),WM_COMM_RXCHAR, (WPARAM)a, (LPARAM)5555);

viewer->setSceneData(root);
viewer->run();
return 0;
}


代码 我简略的发了出来 我这个win32开发osg程序, getOsgHwnd可以获得osg窗口句柄,为什么我send后 peek没有接收到任何消息, 看了网上很多关于这两个函数的帖子,但是没有完全明白原理
难道是因为peek在线程里面, 但是窗口句柄是一个啊,期待得到回复~
...全文
639 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
D_Future 2015-09-30
  • 打赏
  • 举报
回复
引用 8 楼 fly_dragon_fly 的回复:
消息队列是属于线程的, 另一个线程无论get或者peek只能得到本线程, 要得到消息只能hwnd由thread本身创建
我准备用LRESULT CALLBACK WindowProc(*****)来响应消息,直接加在main函数上面,但是没有什么效果 ,这回用的是控制台窗口本身的句柄
fly_dragon_fly 2015-09-30
  • 打赏
  • 举报
回复
消息队列是属于线程的, 另一个线程无论get或者peek只能得到本线程, 要得到消息只能hwnd由thread本身创建
D_Future 2015-09-30
  • 打赏
  • 举报
回复
引用 5 楼 yfqvip 的回复:
我现在需要接受一个dll中的sendmessage 而dll的传入参数是一个hwnd类型的变量, 而之前的人写的使用mfc,传入this->m_Hwnd,之后ON_MESSAGE就可以获取到消息了,而我用的是win32写的程序,就想到用peekmessage,但是失败了
D_Future 2015-09-30
  • 打赏
  • 举报
回复
引用 5 楼 yfqvip 的回复:
看了图我发现 是不是sendmessage发送的消息用peekmessage获取不到,直接由系统进行处理,我刚刚也试了下,若果参数hwnd处写NULL,send发送的peek能接受到,而send发送的接收不到
fly_dragon_fly 2015-09-30
  • 打赏
  • 举报
回复
引用 13 楼 u011371217 的回复:
[quote=引用 11 楼 fly_dragon_fly 的回复:] [quote=引用 10 楼 u011371217 的回复:] [quote=引用 8 楼 fly_dragon_fly 的回复:] 消息队列是属于线程的, 另一个线程无论get或者peek只能得到本线程, 要得到消息只能hwnd由thread本身创建
我准备用LRESULT CALLBACK WindowProc(*****)来响应消息,直接加在main函数上面,但是没有什么效果 ,这回用的是控制台窗口本身的句柄[/quote]代码是什么? 同线程send是不通过消息队列,直接到wndproc的, 其它情况通过消息队列才可以peek[/quote] 我这个是控制台应用程序 不是控制台项目 是不是 弹出来的cmd窗口 就算获取到句柄也是没有消息循环的[/quote]那是主线程的, 在thread中自己调用createwindows的才行
D_Future 2015-09-30
  • 打赏
  • 举报
回复
引用 11 楼 fly_dragon_fly 的回复:
[quote=引用 10 楼 u011371217 的回复:] [quote=引用 8 楼 fly_dragon_fly 的回复:] 消息队列是属于线程的, 另一个线程无论get或者peek只能得到本线程, 要得到消息只能hwnd由thread本身创建
我准备用LRESULT CALLBACK WindowProc(*****)来响应消息,直接加在main函数上面,但是没有什么效果 ,这回用的是控制台窗口本身的句柄[/quote]代码是什么? 同线程send是不通过消息队列,直接到wndproc的, 其它情况通过消息队列才可以peek[/quote] 我这个是控制台应用程序 不是控制台项目 是不是 弹出来的cmd窗口 就算获取到句柄也是没有消息循环的
D_Future 2015-09-30
  • 打赏
  • 举报
回复
引用 11 楼 fly_dragon_fly 的回复:
[quote=引用 10 楼 u011371217 的回复:] [quote=引用 8 楼 fly_dragon_fly 的回复:] 消息队列是属于线程的, 另一个线程无论get或者peek只能得到本线程, 要得到消息只能hwnd由thread本身创建
我准备用LRESULT CALLBACK WindowProc(*****)来响应消息,直接加在main函数上面,但是没有什么效果 ,这回用的是控制台窗口本身的句柄[/quote]代码是什么? 同线程send是不通过消息队列,直接到wndproc的, 其它情况通过消息队列才可以peek[/quote]

LRESULT CALLBACK WindowProc(
							HWND hwnd,      // handle to window
							UINT uMsg,      // message identifier
							WPARAM wParam,  // first message parameter
							LPARAM lParam   // second message parameter
							)
{
	switch(uMsg)
	{
	case WM_COMM_RXCHAR:
		{
			//OnMmWimData(wParam,lParam);
			int a = 1;
			return ERROR_SUCCESS;
		}			
	default:
		return DefWindowProc(hwnd,uMsg,wParam,lParam);
	}
}


DWORD WINAPI ThreadProc(LPVOID lpParam)
{
	ding* cs = (ding*)lpParam;
	while (cs->m_hwnd) 
	{ 
		MSG msg;
		if (PeekMessage(&msg, cs->m_hwnd, 0, 0, PM_REMOVE)) 
		{ 
			if (msg.message == WM_QUIT) 
				break ; 
			if (msg.message == WM_COMM_RXCHAR)
			{
				int a = 1;
			}
			TranslateMessage (&msg) ; 
			DispatchMessage (&msg) ; 
		} 
	} 
	return 0;
}

ding::ding()
{
	da = new dddd;
	m_hwnd = GetConsoleHwnd();
	DWORD threadID = 0;
	HANDLE hThread;
	hThread = CreateThread(NULL,0,ThreadProc,this,0,&threadID); // 创建线程
}
void ding::startDll()
{	
	da->test(m_hwnd);
}
HWND ding::GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 
	HWND hwndFound; 
	char pszNewWindowTitle[MY_BUFSIZE]; 
	char pszOldWindowTitle[MY_BUFSIZE]; 

	GetConsoleTitle((LPWSTR)pszOldWindowTitle, MY_BUFSIZE);

	sprintf(pszNewWindowTitle, "%d/%d", 
		GetTickCount(),
		GetCurrentProcessId());

	SetConsoleTitle((LPCWSTR)pszNewWindowTitle);
	Sleep(40);
	hwndFound=FindWindowW(NULL, (LPCWSTR)pszNewWindowTitle);

	SetConsoleTitle((LPCWSTR)pszOldWindowTitle);
	return(hwndFound);
}

void main()
{
	ding* d = new ding();
	d->startDll();

	char strTitle[255];
	GetConsoleTitle((LPWSTR)strTitle, 255);
	HWND hw = FindWindow(L"ConsoleWindowClass",(LPWSTR)strTitle);
	int a =1;

	Sleep(1000000);
}
fly_dragon_fly 2015-09-30
  • 打赏
  • 举报
回复
引用 10 楼 u011371217 的回复:
[quote=引用 8 楼 fly_dragon_fly 的回复:] 消息队列是属于线程的, 另一个线程无论get或者peek只能得到本线程, 要得到消息只能hwnd由thread本身创建
我准备用LRESULT CALLBACK WindowProc(*****)来响应消息,直接加在main函数上面,但是没有什么效果 ,这回用的是控制台窗口本身的句柄[/quote]代码是什么? 同线程send是不通过消息队列,直接到wndproc的, 其它情况通过消息队列才可以peek
满衣兄 2015-09-29
  • 打赏
  • 举报
回复
满衣兄 2015-09-29
  • 打赏
  • 举报
回复
1、当一个线程向该线程所建立的窗口SendMessage消息时,它只是调用指定窗口的消息处理过程,并不将消息入队列 (图1-2) 2、当一个线程向另一个线程所建立的窗口SendMessage时,该消息要追加到接收消息线程的发送消息队列,然后发送消息的线程进入等待状态,接收消息的线程处理完该消息后,由系统唤醒发送消息的线程,这时发送线程继续进行 3、一个线程的消息队列实际上分为四种不同的消息队列:Post消息队列、Send消息队列、输入消息队列、应答消息队列。PostMessage是将消息追加到Post消息队列,SendMessage是追加到Send消息队列,两个队列处理的优先级并不一样。 详见 <windows核心编程>窗口消息这一章。
D_Future 2015-09-29
  • 打赏
  • 举报
回复
引用 2 楼 iyomumx 的回复:
请结合楼上回帖一同食用
引用
Syntax C++ BOOL WINAPI PeekMessage( _Out_ LPMSG lpMsg, _In_opt_ HWND hWnd, _In_ UINT wMsgFilterMin, _In_ UINT wMsgFilterMax, _In_ UINT wRemoveMsg ); Parameters lpMsg [out] Type: LPMSG A pointer to an MSG structure that receives message information. hWnd [in, optional] Type: HWND A handle to the window whose messages are to be retrieved. The window must belong to the current thread. If hWnd is NULL, PeekMessage retrieves messages for any window that belongs to the current thread, and any messages on the current thread's message queue whose hwnd value is NULL (see the MSG structure). Therefore if hWnd is NULL, both window messages and thread messages are processed. If hWnd is -1, PeekMessage retrieves only messages on the current thread's message queue whose hwnd value is NULL, that is, thread messages as posted by PostMessage (when the hWnd parameter is NULL) or PostThreadMessage.
我是在一个dll中sendmessage 然后控制台程序写个线程不停PeekMessage 接收消息,这样算是不同线程么 我认为他们之间传递消息的桥梁就是我传入的窗口句柄啊,就算放到main函数里面,也是接收不到消息
iyomumx 2015-09-29
  • 打赏
  • 举报
回复
请结合楼上回帖一同食用
引用
Syntax C++ BOOL WINAPI PeekMessage( _Out_ LPMSG lpMsg, _In_opt_ HWND hWnd, _In_ UINT wMsgFilterMin, _In_ UINT wMsgFilterMax, _In_ UINT wRemoveMsg ); Parameters lpMsg [out] Type: LPMSG A pointer to an MSG structure that receives message information. hWnd [in, optional] Type: HWND A handle to the window whose messages are to be retrieved. The window must belong to the current thread. If hWnd is NULL, PeekMessage retrieves messages for any window that belongs to the current thread, and any messages on the current thread's message queue whose hwnd value is NULL (see the MSG structure). Therefore if hWnd is NULL, both window messages and thread messages are processed. If hWnd is -1, PeekMessage retrieves only messages on the current thread's message queue whose hwnd value is NULL, that is, thread messages as posted by PostMessage (when the hWnd parameter is NULL) or PostThreadMessage.
赵4老师 2015-09-29
  • 打赏
  • 举报
回复
Multiple Threads in the User Interface http://msdn.microsoft.com/zh-cn/library/ms810439.aspx PostThreadMessage The PostThreadMessage function places (posts) a message in the message queue of the specified thread and then returns without waiting for the thread to process the message. BOOL PostThreadMessage( DWORD idThread, // thread identifier UINT Msg, // message to post WPARAM wParam, // first message parameter LPARAM lParam // second message parameter );

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧