question

OwenRansen-2591 avatar image
0 Votes"
OwenRansen-2591 asked OwenRansen-2591 commented

PostMessage from a VBA app to an MFC app

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
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You are more likely to get help if you post a complete sample. I assume that your PreTranslateMessage is everything that must be added to a MFC dialog project. If you can provide a complete sample of the VBA, but only the minimum required to create the problem, then you are more likely to get help.

0 Votes 0 ·
OwenRansen-2591 avatar image
0 Votes"
OwenRansen-2591 answered

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...

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SimpleSamples avatar image
2 Votes"
SimpleSamples answered OwenRansen-2591 commented

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.


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'd already tried WM_APP with the same results.

My question was different. Why does it work when the MFC app is run external to the IDE, but not when it is run by the IDE?

0 Votes 0 ·

Maybe because you use PreTranslateMessage.

0 Votes 0 ·

Or I should say that you are using unsupported solutions. When you do things the way you want to instead of the way Microsoft says to do things then do not expect it to be logical. First ensure you are doing things the way Microsoft says to do it. Even if something works sometimes, if Microsoft does not support it then you can't expect anyone to be able to explain why it does not work when it does not.

0 Votes 0 ·

@SimpleSamples said: "Also, do not use PreTranslateMessage. You can add a handler using the"

I think you are right, now I've got the rest going I'll do that.

Thanks,

Owen

0 Votes 0 ·
XiaopoYang-MSFT avatar image
0 Votes"
XiaopoYang-MSFT answered XiaopoYang-MSFT commented

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


image.png (42.3 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Could it be that Access VBA is 32 bit?

0 Votes 0 ·

Also works I tested.

1 Vote 1 ·