Click here to Skip to main content
16,021,765 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

SendMessage and PostMessage?

Rate me:
Please Sign up or sign in to vote.
3.47/5 (23 votes)
17 Oct 2015CPOL2 min read 94.3K   3K   22   20
This topic focuses on what is the difference between SendMessage and PostMessage and how to use them?

Introduction

This topic focuses on two things:

  1. What is the difference between SendMessage and PostMessage
  2. How to use SendMessage, PostMessage with WM_KEYDOWN. The way to find LParam and RParam parameters

To understand what is the difference between SendMessage and PostMessage, please look at the below table.

What is the Difference between SendMessage, PostMessage

SendMessage PostMessage
Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message (from MSDN) Places (posts) a message in the message queue associated with the thread that created the specified window and returns without waiting for the thread to process the message (from MSDN)
Sequentially Not sequentially
synchronous asynchronous

How to Use SendMessage, PostMessage?

C++
//
// ::PostMessage (HWND, WM_KEYDOWN, WPARAM, LPARAM);
//
C++
//
// ::SendMessage (HWND, WM_KEYDOWN, WPARAM, LPARAM);
//

Parameters?

  • HWND is a handle you want to seed message to this window
  • WPARAM, you can view here: The virtual-key code of the nonsystem key.
  • LPARAM, you can know through Notepad program and Microsoft spy++

How to Find LParam

  • First, you must download Microsoft spy++. If you install Microsoft Visual 2012, Microsoft spy ++ will be integrated
  • I will demo how to get LParam with VK_KEYDOWN, value through some basic steps
    (Same as, you can do it with VK_DOWN, VK_LEFT, .... key)
  • Now open Notepad by run -> notepad
  • Open your Microsoft Spy ++ (Built-in Microsoft Visual Studio 2012 ++)

http://learn-tech-tips.blogspot.com

Next, Click to menu Spy -> FindWindows, using cross circle on the red frame.

http://learn-tech-tips.blogspot.com

Drag this cross circle to Notepad on this edit area:

http://learn-tech-tips.blogspot.com

Click choose Messages properties same as the below picture. Then click ok.

http://learn-tech-tips.blogspot.com

Let's press RETURN key to Edit Area, you can easily see WM_KEYDOWN message on Microsoft Spy++ Dash Broad. Then, right click to that message and choose properties, you can easily know LPARAM and WPARAM value.

http://learn-tech-tips.blogspot.com

My LPARAM value found: 0x0001C001 and RPARAM value: 0x0000000D

C++
::PostMessage(hHandle, WM_KEYDOWN, VK_RETURN, 0x001C0001);

::SendMessage(hHandle, WM_KEYDOWN, VK_RETURN, 0x001C0001);

Using the Source Code

Using FindWindowA and FindWindowEx

API for get window handle:

C++
HWND hwnd = FindWindowA("Notepad", NULL );
HWND hWndChild = ::FindWindowEx(hwnd, NULL, L"Edit", NULL);
C++
/* *******************************************************************************
 Developer: zidane (huuvi168@gmail.com)
 Last Modified: 2015-09-01
 * ******************************************************************************/
BOOL CReadInfomationGameDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Add "About..." menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        CString strAboutMenu;
        strAboutMenu.LoadString(IDS_ABOUTBOX);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // Set the icon for this dialog.  The framework does this automatically
    // when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);   // Set big icon
    SetIcon(m_hIcon, FALSE);  // Set small icon
    
    // TODO: Add extra initialization here
    CString strCommand[] =  
    {

        L"learn-tech-tips",
        L"http://learn-tech-tips.blogspot.com",
        L"huuvi168@gmail.com",
        L"zidane"
        L"http://www.codeproject.com/Tips/1025019/Get-Server-Side-Data-time-with-Cplusplus",
    };

    int length = sizeof(strCommand) / sizeof(strCommand[0]);
    for (int i=0; i<length; i++)
        m_cbCommand.AddString(strCommand[i]);
 
 
    return TRUE;  // return TRUE  unless you set the focus to a control
}

HWND CReadInfomationGameDlg::GetHandleOfWindow(void)
{ 
    HWND hwnd = FindWindowA("Windows abc Class", NULL ); 

    if (!hwnd) 
    {
        MessageBox(_T("Window abc not found"), _T("Message"), MB_OK 
                                                    | MB_ICONINFORMATION);  
        return NULL;
    } 
 
    return hwnd;
}

void CReadInfomationGameDlg::OnBnClickedButtonAddmessage()
{
    // TODO: Add your control notification handler code here
    HWND hHandle = GetHandleOfWindow();

    CString strCBText;
    int nIndex = m_cbCommand.GetCurSel();
 
    if (nIndex == -1) // cannot found text in comboBox, post NULL
    {
        TCHAR strTemp[255];  
        memset(strTemp, 0x00, sizeof(strTemp));
        GetDlgItemText(IDC_COMBO_COMMAND, strTemp, sizeof(strTemp));
        PostChatMessage(hHandle, strTemp);
    }
    else // found text in comboBox
    {
        m_cbCommand.GetLBText(nIndex, strCBText); 
        PostChatMessage(hHandle, strCBText);
    }
}

void CReadInfomationGameDlg::PostChatMessage(HWND hHandle, LPCTSTR szChatMsg)
{
     ::PostMessage(hHandle, WM_KEYDOWN, VK_RETURN, 0x001C0001);
     ::PostMessage(hHandle, WM_KEYDOWN, VK_DOWN, 0x00500001);

    while (szChatMsg[0])
    {
        ::PostMessage(hHandle, WM_CHAR, LOBYTE(szChatMsg[0]),0);
        szChatMsg++;
    }

    ::PostMessage(hHandle, WM_KEYDOWN, VK_RETURN, 0x001C0001);
}

Points of Interest

I like coding, I like researching new technology.

My tech blog is http://learn-tech-tips.blogspot.com.

My blog sharing experiences about technology tips and trick include: language programming: C+, C#, ....
Design skills: photoshop, Office: Excel, Outlook and .... other things!

If you have any feedback about this topic, please leave your comment, we can discuss about it. Smile | :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Vietnam Vietnam
---------------------------------------------------
Thanks for code project, I joined code project's community in the begin of the year 2008. Code project helped me so much when I still a student.

Now I had working about 14 years (2023). Let Code project plentiful as thank its. So I decide sharing my little knowledge for help another students and developers.

Here is my technical blog: https://learn-tech-tips.blogspot.com
I will share many knowledge about Application, Web, Game, Programming languages C#, C++, Web(HTML, CSS, javascript). Office (Excel, Photoshop) and another useful things Smile | :)

---------------------------------------------------
I'm developer, I like code,
I like to learn new technology and want to be friend with people to improve my skills Smile | :)

Thanks and Best Regards!
Zidane (huuvi168@gmail.com)
https://learn-tech-tips.blogspot.com

Comments and Discussions

 
QuestionHow to click the checkbox Pin
Ponos Saska11-Sep-24 8:21
Ponos Saska11-Sep-24 8:21 
GeneralMy vote of 4 Pin
KarstenK19-Oct-15 0:15
mveKarstenK19-Oct-15 0:15 
GeneralRe: My vote of 4 Pin
zidane16819-Oct-15 0:55
zidane16819-Oct-15 0:55 
GeneralRe: My vote of 4 Pin
zidane16821-Oct-15 17:56
zidane16821-Oct-15 17:56 
GeneralThanks Pin
Member 1206830218-Oct-15 12:13
Member 1206830218-Oct-15 12:13 
GeneralRe: Thanks Pin
zidane16818-Oct-15 16:15
zidane16818-Oct-15 16:15 
GeneralMy vote of 5 Pin
Farhad Reza6-Oct-15 2:14
Farhad Reza6-Oct-15 2:14 
GeneralRe: My vote of 5 Pin
zidane1687-Oct-15 6:13
zidane1687-Oct-15 6:13 
GeneralRe: My vote of 5 Pin
Farhad Reza8-Oct-15 3:32
Farhad Reza8-Oct-15 3:32 
You are also welcome. Smile | :)
GeneralMy vote of 3 Pin
Loic URIEN15-Sep-15 11:08
Loic URIEN15-Sep-15 11:08 
AnswerRe: My vote of 3 Pin
zidane16815-Sep-15 15:39
zidane16815-Sep-15 15:39 
BugImportance of threaded usage Pin
Woody1461914-Sep-15 7:31
Woody1461914-Sep-15 7:31 
GeneralRe: Importance of threaded usage Pin
zidane16815-Sep-15 15:55
zidane16815-Sep-15 15:55 
QuestionPlease re-check the article... Pin
O'Doudarragh14-Sep-15 1:44
O'Doudarragh14-Sep-15 1:44 
AnswerRe: Please re-check the article... Pin
zidane16815-Sep-15 15:52
zidane16815-Sep-15 15:52 
GeneralRe: Please re-check the article... Pin
Philipp Sch24-Sep-15 7:46
Philipp Sch24-Sep-15 7:46 
GeneralRe: Please re-check the article... Pin
zidane16824-Sep-15 15:51
zidane16824-Sep-15 15:51 
GeneralRe: Please re-check the article... Pin
Albert Holguin19-Oct-15 8:50
professionalAlbert Holguin19-Oct-15 8:50 
GeneralRe: Please re-check the article... Pin
zidane16821-Oct-15 16:41
zidane16821-Oct-15 16:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.