PostMessage from a VBA app to an MFC app

Owen Ransen 541 Reputation points
2021-08-31T13:41:59.677+00:00

I have a VBA app which uses PostMessage to send data to an MFC app dialog.

I use PreTranslateMsg to catch the message in the MFC dialog.

What is strange is that it all works if I do not run inside the VS C++ IDE, i.e. run the MFC EXE on its own.

But if I run it from inside the IDE the message never arrives.

Fragment from the VBA app, sends the number 99 to the other EXE:

    PostMessage hWndCreaColl, WM_USER + 1, 99, ByVal "LPARAM message"

Fragment from the MFC app, watches for the message:

BOOL CMainDlg::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_USER + 1) {
        const int wParam = (const int)pMsg->wParam ;
        const char* const pszLParam = (const char* const)pMsg->lParam ;
        WalertBoxW (L"Message <%d>",wParam) ;

        return TRUE ;

    } else {
        // The normal thing
        return CDialogEx::PreTranslateMessage(pMsg);
    }

}

Is there some setting in the IDE that I have to set?

TIA...

Owen

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
728 questions
{count} votes

Accepted answer
  1. Owen Ransen 541 Reputation points
    2021-09-02T08:55:21.53+00:00

    I've found the problem. I ran the IDE in Admin mode for a different program I'm developing. But the ACCESS <-> VBA does not need Admin mode.

    When I run the IDE not in Admin mode it all works.

    Thanks all, your feedback pushed me into investigating more...

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Sam of Simple Samples 5,516 Reputation points
    2021-08-31T19:23:48.587+00:00

    First, see WM_APP and WM_USER. The following are the important parts.

    • WM_USER: for use by private window classes
    • WM_APP: used to define private messages

    So you need to use WM_APP+1.

    Also, do not use PreTranslateMessage. You can add a handler using the MFC Class Wizard. It has been a few years since I have done much MFC. Windows Message Handling - Part 2 - CodeProject appears to be a good article. At the end it shows use of Registered Message Ids (that is my article) that provide a way to make get a message id that is essentially guaranteed to be unique.

    2 people found this answer helpful.

  2. Xiaopo Yang - MSFT 11,501 Reputation points Microsoft Vendor
    2021-09-01T06:03:00.903+00:00

    I have received successfully with both x64 processes.
    128182-image.png