这个控制台程序为啥不能收到消息呢

我看你有戏 2010-06-10 11:37:31

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#define WM_MYMESSAGE (WM_USER + 1)
unsigned long WINAPI Thread(PVOID pvoid);
HWND GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
HWND hwndFound; // This is what is returned to the caller.
char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
// WindowTitle.
char pszOldWindowTitle[MY_BUFSIZE]; // Contains original
// WindowTitle.

// Fetch current window title.

GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);

// Format a "unique" NewWindowTitle.

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

// Change current window title.

SetConsoleTitle(pszNewWindowTitle);

// Ensure window title has been updated.

Sleep(40);

// Look for NewWindowTitle.

hwndFound=FindWindow(NULL, pszNewWindowTitle);

// Restore original window title.

SetConsoleTitle(pszOldWindowTitle);

return(hwndFound);
}

void main()
{
DWORD dwThreadId;
printf( "use timer in workthread of console application <masterz> \n ");

/*DWORD dwwait=WaitForSingleObject(hThread,1000*30);
switch(dwwait)
{
case WAIT_ABANDONED:
printf( "main thread WaitForSingleObject return WAIT_ABANDONED\n ");
break;
case WAIT_OBJECT_0:
printf( "main thread WaitForSingleObject return WAIT_OBJECT_0\n ");
break;
case WAIT_TIMEOUT:
printf( "main thread WaitForSingleObject return WAIT_TIMEOUT\n ");
break;
}
CloseHandle(hThread); */

HWND hwnd = GetConsoleHwnd();
HANDLE hThread=CreateThread(NULL,0,Thread,&hwnd,0,&dwThreadId);
while (true)
{
::SendMessage(hwnd,WM_MYMESSAGE,0,0);
Sleep(1000);
}
_getch();
}

unsigned long WINAPI Thread(PVOID pvoid)
{
MSG msg;
int count =0;
PeekMessage(&msg,*(HWND*)pvoid,WM_MYMESSAGE,WM_MYMESSAGE,PM_NOREMOVE);
//UINT timerid = SetTimer(NULL,111,3000,NULL);
while(1)
{
GetMessage(&msg,NULL,0,0);
// if(msg.message==WM_TIMER)
// {
// count++;
// printf( "WM_TIMER in work thread count=%d\n ",count);
// //if(count> 4)
// KillTimer(NULL,timerid); //break;
// }
if (msg.message==WM_MYMESSAGE)
{
printf("收到了自己的消息了,哈哈");
}

TranslateMessage(&msg);
DispatchMessage(&msg);
}

/*MSG msg;
PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE);
UINT timerid = SetTimer(NULL,111,3000,NULL);
BOOL bRet;
int count =0;
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
printf("handle the error and possibly exit \n");
}
else
if(msg.message==WM_TIMER)
{
count++;
printf( "WM_TIMER in work thread count=%d\n ",count);
if(count> 4)
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(NULL,timerid);
printf( "thread end here\n "); */

return 0;
}


...全文
184 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
你妹的特盗不 2010-06-11
  • 打赏
  • 举报
回复
楼主结贴吧,消息循环,都实现,发消息给主线程,主线程发给子线程

#include "stdafx.h"
#include <atlstr.h>
//#include <debug.h>
//using namespace UTIL_LFS09;
bool g_exit=false;
DWORD g_dwMainThreadID;
#define WM_MY WM_USER+11
DWORD index=0;
DWORD dwThreadId;

void Thread(PVOID pvoid);
BOOL WINAPI ConsoleCtrlhandler(DWORD dwCtrlType)
{
if (dwCtrlType==CTRL_CLOSE_EVENT)
{
PostThreadMessage(g_dwMainThreadID,WM_CLOSE,0,0);
return TRUE;
}
return FALSE;

}

int main(int argc, char *argv[])
{
g_dwMainThreadID=GetCurrentThreadId();
SetConsoleCtrlHandler(ConsoleCtrlhandler,TRUE);

HANDLE hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Thread,&g_dwMainThreadID,0,&dwThreadId);

MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
switch(msg.message)
{
case WM_MY:
printf("get custom message %d\r\n",msg.wParam);
break;
case WM_CLOSE:
g_exit=true;
PostThreadMessage(dwThreadId,WM_CLOSE,0,0);
PostThreadMessage(g_dwMainThreadID,WM_QUIT,0,0);
break;;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

void Thread(PVOID pvoid)
{
MSG msg;
SetTimer(NULL,11,1000,0);
while(GetMessage(&msg,NULL,0,0))
{
switch(msg.message)
{
case WM_TIMER:
PostThreadMessage(*(DWORD*)pvoid,WM_MY,index++,0);
break;
case WM_CLOSE:
KillTimer(NULL,11);
printf("get sub thread close \r\n");
PostThreadMessage(dwThreadId,WM_QUIT,0,0);
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
你妹的特盗不 2010-06-11
  • 打赏
  • 举报
回复

控制台程序没有hwnd吧,用要用 PostThreadMessage(g_dwMainThreadID,,,,,)
第一个参数在 main中函数中使用 g_dwMainThreadID=GetCurrentThreadId();
lixung 2010-06-11
  • 打赏
  • 举报
回复
线程接收不到发送给窗口的消息是因为创建窗口的线程和主线程不是一个线程

blpluto 2010-06-10
  • 打赏
  • 举报
回复
消息发送目标的问题,这个hwnd不是目标的句柄

你可以在堆栈中查看这个句柄值肯定不是你想要的值
lixung 2010-06-10
  • 打赏
  • 举报
回复
程序的问题,给窗口发消息,却想在线程(新开的)里面接受消息,当然接收不到了
我看你有戏 2010-06-10
  • 打赏
  • 举报
回复
既然是在windows上跑的话,应该有的吧
mayudong1 2010-06-10
  • 打赏
  • 举报
回复
控制台程序没有消息循环吧
我看你有戏 2010-06-10
  • 打赏
  • 举报
回复
看来也只能用线程间通信原理了

但是我总感觉应该是可以用窗口消息循环机制实现的

因为控制的句柄都可以获取到啊
lixung 2010-06-10
  • 打赏
  • 举报
回复
hwndFound; 
DWORD dwThreadId;

HWND GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
// This is what is returned to the caller.
char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
// WindowTitle.
char pszOldWindowTitle[MY_BUFSIZE]; // Contains original
// WindowTitle.

// Fetch current window title.

GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);

// Format a "unique" NewWindowTitle.

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

// Change current window title.

SetConsoleTitle(pszNewWindowTitle);

// Ensure window title has been updated.

Sleep(40);

// Look for NewWindowTitle.

hwndFound=FindWindow(NULL, pszNewWindowTitle);

// Restore original window title.

SetConsoleTitle(pszOldWindowTitle);

return(hwndFound);
}

void main()
{
hwndFound = GetConsoleHwnd();
HANDLE hThread=CreateThread(NULL,0,Thread,0,0,0);
MSG msg;

dwThreadId = GetCurrentThreadId();
while(GetMessage(&msg,NULL,0,0))
{
if (msg.message==WM_MYMESSAGE)
{
OutputDebugString("GetMessage");
}

TranslateMessage(&msg);
DispatchMessage(&msg);
}

}

unsigned long WINAPI Thread(PVOID pvoid)
{
while (true)
{
//::SendMessage(hwndFound,WM_MYMESSAGE,0,0);
PostThreadMessage(dwThreadId,WM_MYMESSAGE,0,0);
printf("DD\n");
Sleep(1000);
}
return 0;
}
lixung 2010-06-10
  • 打赏
  • 举报
回复
hwndFound;
DWORD dwThreadId;

HWND GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
// This is what is returned to the caller.
char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
// WindowTitle.
char pszOldWindowTitle[MY_BUFSIZE]; // Contains original
// WindowTitle.

// Fetch current window title.

GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);

// Format a "unique" NewWindowTitle.

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

// Change current window title.

SetConsoleTitle(pszNewWindowTitle);

// Ensure window title has been updated.

Sleep(40);

// Look for NewWindowTitle.

hwndFound=FindWindow(NULL, pszNewWindowTitle);

// Restore original window title.

SetConsoleTitle(pszOldWindowTitle);

return(hwndFound);
}

void main()
{
hwndFound = GetConsoleHwnd();
HANDLE hThread=CreateThread(NULL,0,Thread,0,0,0);
MSG msg;

dwThreadId = GetCurrentThreadId();
while(GetMessage(&msg,NULL,0,0))
{
if (msg.message==WM_MYMESSAGE)
{
OutputDebugString("GetMessage");
}

TranslateMessage(&msg);
DispatchMessage(&msg);
}

}

unsigned long WINAPI Thread(PVOID pvoid)
{
while (true)
{
//::SendMessage(hwndFound,WM_MYMESSAGE,0,0);
PostThreadMessage(dwThreadId,WM_MYMESSAGE,0,0);
printf("DD\n");
Sleep(1000);
}
return 0;
}



unsigned long WINAPI Thread(PVOID pvoid)
{
while (true)
{
PostThreadMessage(dwThreadId,WM_MYMESSAGE,0,0);
printf("DD\n");
Sleep(1000);
}
return 0;
}

我看你有戏 2010-06-10
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 lixung 的回复:]
引用 13 楼 wltg2001 的回复:
这里有问题,你要没弄明白就把句柄做成全局的
==========
应该不是这个原因,hwnd虽然是局部变量,但是main函数并没有结束,hwnd并没有被收回


他传的方式不对吧
HANDLE hThread=CreateThread(NULL,0,Thread,&hwnd,0,&dwThreadId); //直接传hwnd……
[/Quote]

不是这种问题吧

传指针跟传整值在这里是一样的
lixung 2010-06-10
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 wltg2001 的回复:]
这里有问题,你要没弄明白就把句柄做成全局的
==========
应该不是这个原因,hwnd虽然是局部变量,但是main函数并没有结束,hwnd并没有被收回
[/Quote]

他传的方式不对吧
HANDLE hThread=CreateThread(NULL,0,Thread,&hwnd,0,&dwThreadId); //直接传hwnd就可以,没有必要传地址,就是传个整数值嘛



wltg2001 2010-06-10
  • 打赏
  • 举报
回复
这里有问题,你要没弄明白就把句柄做成全局的
==========
应该不是这个原因,hwnd虽然是局部变量,但是main函数并没有结束,hwnd并没有被收回
lixung 2010-06-10
  • 打赏
  • 举报
回复
HANDLE hThread=CreateThread(NULL,0,Thread,&hwnd,0,&dwThreadId);


while (true)
{
::SendMessage(*(HWND*)pvoid,WM_MYMESSAGE,0,0);
printf("DD\n");
Sleep(1000);
}


这里有问题,你要没弄明白就把句柄做成全局的
lixung 2010-06-10
  • 打赏
  • 举报
回复
 HANDLE   hThread=CreateThread(NULL,0,Thread,&hwnd,0,&dwThreadId); 


while (true)
{
::SendMessage(*(HWND*)pvoid,WM_MYMESSAGE,0,0);
printf("DD\n");
Sleep(1000);
}


这里有问题,你要没弄明白就把句柄做成全局
我看你有戏 2010-06-10
  • 打赏
  • 举报
回复
使用PostThreadMessage在Win32线程间传递消息

这个原理是可以的

我看你有戏 2010-06-10
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 visualeleven 的回复:]
在控制台程序窗口一个隐藏的窗口在接受消息
[/Quote]

不明白
Eleven 2010-06-10
  • 打赏
  • 举报
回复
在控制台程序窗口一个隐藏的窗口在接受消息
墨子翼 2010-06-10
  • 打赏
  • 举报
回复
不知道,顶起
我看你有戏 2010-06-10
  • 打赏
  • 举报
回复
获取句柄的函数是微软提供的

这个应该是不会错的
加载更多回复(1)

16,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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