Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to return value from Message c++ mfc

Tags:

c++

winapi

mfc

i'm using c++ mfc and declare message in my dlg:

LRESULT CMyWnd2::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
    wParam=5;
    lParam=6;
    return 0;
}

using code:

WPARAM w=0;
LPARAM l=0;
SendMessage(hwnd,messageId,w,l);
cout<<w<<l<<endl;

print:

0
0

how can i change the values of w / l parameters?

like image 799
Lital Kapara Avatar asked Nov 28 '25 14:11

Lital Kapara


1 Answers

A function can not change the parameters passed in by value.

However, you can pass a pointer to whatever data structure you want in LPARAM, and modify that data structure in your message handler.

Here is how you can use it:

int myValueToBeUpdated = 0;
SendMessage(hwnd, messageId, 0, (LPARAM)&myValueToBeUpdated);
cout << myValueToBeUpdated << endl;

and the message handler:

LRESULT CMyWnd2::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
    int* p = (int*)lParam;
    *p = 42;
    return 0;
}
like image 179
Vlad Feinstein Avatar answered Nov 30 '25 05:11

Vlad Feinstein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!