0

In my c++ code, I define a message to inform other class to do some action. The code is like below:

In mainFrm.h:

...
afx_msg LRESULT OnHandleDialog(WPARAM wParam, LPARAM lParam);
...

In mainFrm.cpp

LRESULT CMainFrame::OnHandleDialog(WPARAM wParam, LPARAM lParam)
{
    switch (wParam)
    {
        case Define::myCondition:
        {
            myFunction->doSomethingHere(static_cast<bool>(lParam)); //there is  warning C4800: 'LPARAM' : forcing value to bool 'true' or 'false' (performance warning)         
            return 0;
        }

    }
    return 0;
}

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
...
    ON_MESSAGE(WM_DEFINED_DIALOG, OnHandleDialog) //My Message Mapping here
....
END_MESSAGE_MAP()

In another file, MyCode.h

void sendDefinedMsg(DWORD_PTR wParam, DWORD_PTR data = 0)
{
    pNotifyWnd->PostMessage(WM_DEFINED_DIALOG, wParam, data);
}

In another file, Mycode.cpp

sendDefinedMsg( myCondition, false);
....
sendDefinedMsg( myCondition, true);
....

So, you can see in the above code, I want to get the message parameter here:

myFunction->doSomethingHere(static_cast<bool>(lParam));

The issue is: whatever I cast the LPARAM lParam to, using static_cast<bool> or reinterpret_cast<bool>, or (bool). All of them give me a warning:

warning C4800: 'LPARAM' : forcing value to bool 'true' or 'false' (performance warning)

So my question is: How should I cast lParam to my original passed parameter true/false?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Penny
  • 606
  • 1
  • 7
  • 15
  • 1
    Possible duplicate of [warning C4800: 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)](https://stackoverflow.com/questions/22498767/warning-c4800-bool-forcing-value-to-bool-true-or-false-performance-war) - Try `doSomethingHere(lParam != 0)` – Barmak Shemirani Mar 26 '18 at 05:36
  • 1
    LPARAM is a pointer type, and is often in fact used as a pointer, the compiler thinks that converting it to an integral type is a possible bug. It very often is, just not here. Comparing it to nullptr keeps it happy. Or using the message cracking macros, LOWORD(lparam) gets it done. – Hans Passant Mar 26 '18 at 08:08

2 Answers2

0

I make following changes for my code to realize my target:

In another file, Mycode.cpp

sendDefinedMsg( myCondition, static_cast<DWORD_PTR>(0));
....
sendDefinedMsg( myCondition, static_cast<DWORD_PTR>(1));
....

in Message Handling code, some following changes:

bool curValue = static_cast<DWORD_PTR>(lParam) == 0 ? false : true;
myFunction->doSomethingHere(curValue);

Then the warning is gone and value is correct.

Actually now passing true/false should be no problem. Maybe someone tell me how to pass other types of values, eg. string or int etc.

Penny
  • 606
  • 1
  • 7
  • 15
  • What was wrong with my suggestion `doSomethingHere(lParam != 0)` or `doSomethingHere(lParam != nullptr)`? Your casting is basically changing `LONG_PTR` to `ULONG_PTR`, it's not necessary if there is no sign comparison involved - To pass a string, start with `const wchar_t* buf = L"123";` then pass it as `::PostMessage(hwnd, WM_USER + 1, 0, reinterpret_cast(buf));` – Barmak Shemirani Mar 27 '18 at 09:31
  • @BarmakShemirani my thinking is just in my PostMessage functionvoid sendDefinedMsg(DWORD_PTR wParam, DWORD_PTR data = 0, the parameter is DWORD_PTR instead of LPARAM. So I guess maybe it is better to keep the argument type same as parameter – Penny Mar 27 '18 at 20:39
0

I have no warnings when I type just:

bool curValue = lParam;
AGR
  • 321
  • 6
  • 8