Alternative discuss the difference between PostMessage and SendMessage

Copyright: Since May 13, 2019, all technical blog article on this website and Jane book synchronization. https://blog.csdn.net/y601500359/article/details/90756945

Foreword

Today encounter when using MFC messaging function of a problem, sleepy for a long time, finally finally found the reason.

Function Information

Take a look at two prototype:

LRESULT SendMessage(
        HWND hWnd,
        UINT Msg,
        WPARAM wParam,
        LPARAM lParam
);
BOOL PostMessage(
        HWND hWnd,
        UINT Msg,
        WPARAM wParam,
        LPARAM lParam
);

Four parameters of both the meaning is the same.
Definition:
hWnd: its window procedure receives a message window handle.
Msg: Specifies the message to be sent.
wParam: Specifies the message specifies additional information.
IParam: Specifies the message specifies additional information.

purpose

I use this message object that in the process of sending the message, the third parameter wParamneeds to take the message I need, the string is a string.

problem appear

I first used is PostMessage, after all, it does not clog the thread, and will not cause the program stuck in the course, I found a problem when I receive the message, parses found out parameter is always garbled, at first I always thought of the strong turn leads to the problem, but in fact I use the PostMessagefunction function in strong turn is normal. Process is as follows:

Use

//发送消息
void NotifyRequstQuery(const std::string& body)
{
	const char *cBody = body.c_str();
	PostMessage(WM_MYMESSAGE_ONQUREY, (WPARAM)cBody, 0);
}

//接收消息
afx_msg LRESULT OnMyMessageOnqurey(WPARAM wParam, LPARAM lParam)
{
	char *strBody = (char *)wParam;
	.....
	return 0;
}

test

In receiving the message can not function when parsing the message I wanted, that strong turn in addition to the problem, so I conducted the following tests:

void NotifyRequstQuery(const std::string& body)
{
	const char *cBody = body.c_str();
	WPARAM wParm = (WPARAM)cBody;
	const char *cResult = (const char *)wParm;

	z_pFrame->PostMessage(WM_MYMESSAGE_ONQUREY, (WPARAM)cBody, 0);
}

In fact, this time found strong turn back, no problem.

Replace message function

Then I will PostMessagefunction as a replacement SendMessage, then you find that the received message is a function of the received normal parameters.
In line with the purpose of thinking, checked a lot of difference between the two, is summed up: it does not have a corresponding return directly; but after the latter had to return to the message processing, or in a thread waits.
So what is the cause of both cases it appears different parameters.
This time will be remembered due to variable life cycle.
Then I conducted the following tests:

void NotifyRequstQuery(const std::string& body)
{
	//const char *cBody = body.c_str();
	const char *cBody = "This is a Test."
	z_pFrame->PostMessage(WM_MYMESSAGE_ONQUREY, (WPARAM)cBody, 0);
}

It is also found in normal reception. Very strange. But I'm not sure now that often use const pointer variable life cycle will be different?
To be honest, parameter, often a reference parameter, pointer variables, and so often, that a series of brain burn stuff, a lot of people do not necessarily really can not go wrong, then conducted the following tests:

#include <iostream>

std::string g_strTemp;
//返回指向常变量的常指针
const  char *TestStr1()
{
	const char *cTemp = "This is TestStr1.";
	return cTemp;
}
//返回常引用形参赋值常指针
const  char *TestStr2(const std::string &strTemp)
{
	const char *cTemp = strTemp.c_str();
	return cTemp;
}
//返回一般形参赋值常指针
const char *TestStr3(std::string strTmpe)
{
	const char *cTemp = strTmpe.c_str();
	return cTemp;
}
//局部变量传常变量实参
const char *TestStr4()
{
	const char *cTemp = "********5555********";

	return TestStr2(cTemp);
}
//全局变量传实参
const char *TestStr5()
{
	g_strTemp = "********6666********";
	return TestStr2(g_strTemp);
}

int main()
{
	const  char *cReturn1 = TestStr1();
	std::cout << cReturn1 << std::endl;

	g_strTemp = "********1111********";
	const char *cReturn2 = TestStr2(g_strTemp);
	std::cout << cReturn2 << std::endl;

	g_strTemp = "********2222********";
	cReturn2 = TestStr3(g_strTemp);
	std::cout << "MAIN:" << cReturn2 << std::endl;

	std::string strBody = "********3333********";
	const char *cReturn3 = TestStr2(strBody);
	std::cout << cReturn3 << std::endl;

	strBody = "********4444********";
	cReturn3 = TestStr3(strBody);
	std::cout << cReturn3  << std::endl;

	const char *cReturn4 = TestStr4();
	std::cout << cReturn4 << std::endl;

	const char *cReturn5 = TestStr5();
	std::cout << cReturn5 << std::endl;
	system("pause");
	return 0;
}

The results are shown running:
operation result
by the results we draw the following conclusions:

  • The constant variable assigned to the variable as a constant pointer member variables, member variables of the life cycle until the program ends.
  • Often cited by the variable cycle parameter assignment returns related with the incoming arguments, arguments change, the end of the cycle.

Error reason

Which shows that the problem lies in calling NotifyRequstQueryplaces, indicating the incoming argument has changed since 发消息and 接收消息in different threads, so this situation is entirely possible, because PostMessagenot blocked.

Solution

1, the call NotifyRequstQuerypassed when the argument is defined as global variables, but personally do not recommend;
2, SendMessageinstead of PostMessage, but according to the actual situation may be.

Guess you like

Origin blog.csdn.net/y601500359/article/details/90756945