Receiving wParam from message received from DLL

0

I'm working on a program that needs to work with an imported DLL. I can load the DLL, and execute functions within it fine.

The problem comes up when I need to extract data it has sent with Send/PostMessage.

The documentation says:

MSG_PSKCHARRDY

Numeric Value is WM_USER+1001 or 0x400+0x3E9 or 0x7E9 or 2025. This message is sent from the DLL to the Window whose handle is passed when the fnStartSoundCard is called. It is sent whenever there is an ASCII character available from the receiver or if in the Transmit mode, when a character has been sent out the soundcard.

The following parameters are sent along with this message:

  • wParam = The ASCII character(0 to 255)

  • lParam = -1 if is a transmitted character, or the Receive channel number(0-49) that is sending the message.

I've added this as a case under my WindProc as case: WM_USER + 1001: and any code underneath is executed when the fnStartSoundCard function is called, so I know it's receiving the message.

But when I try to print the wParam as a char string, I always get an exception thrown at the end of my DispatchMessage(&msg) function here:

while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
return (int)msg.wParam;

Exception thrown at 0x76524463 (ucrtbase.dll) in PSK31.exe:
0xC0000005: Access violation reading location 0x0000004D.

My message handler looks like this:

case WM_USER + 1001:
    if (hWnd == GetActiveWindow()) {
        char textChar;
        SOMETHING = (char*)wParam;
        printf(SOMETHING);
        Sleep(1000);
    }
    break;

How can I get the char extracted from the message?

c++
post
get
message
asked on Stack Overflow Nov 13, 2018 by Billy Penley • edited Nov 13, 2018 by Remy Lebeau

1 Answer

0

There are two mistakes in your code.

  • The documentation you quote clearly says that the MSG_DATARDY message is defined as WM_USER+1000 but you are using WM_USER+1001 instead.

  • The documentation you quote does not say anything about the message's wParam and lParam values being pointers to strings, but you are trying to treat the wParam as if it were, and it is clearly not. Your code is crashing trying to read from address 0x0000004D, which is a memory address reserved by the OS, so wParam can't be a pointer to data in application-accessible memory. 0x4D (77) is a small number, certainly small enough to look like the actual frequency number, so the numeric values of wParam and lParam should be used as-is, not casted to string pointers.

Try this instead:

#define MSG_DATARDY (WM_USER + 1000)

case MSG_DATARDY:
    if (hWnd == GetActiveWindow()) {
        int freq = (int) wParam;
        int strength = (int) lParam;
        printf("RX frequency: %d Hz, signal strength: %d", freq, strength);
        Sleep(1000);
    }
    break;

UPDATE: in leau of the updated documentation you quoted, you are still making the same mistake regarding the wParam. It is NOT a pointer to an ASCII character. It is the actual character instead, so use the value as-is:

#define MSG_PSKCHARRDY (WM_USER + 1001)

case MSG_PSKCHARRDY:
    if (hWnd == GetActiveWindow()) {
        char textChar = (char) wParam;
        int channel = (int) lParam;
        if (channel == - 1) {
            printf("TX character: %c", textChar);
        }
        else {
            printf("RX channel: %d, character: %c, ", channel, textChar);
        }
        Sleep(1000);
    }
    break;
answered on Stack Overflow Nov 13, 2018 by Remy Lebeau • edited Nov 13, 2018 by Remy Lebeau

User contributions licensed under CC BY-SA 3.0