Search code examples
c++winapihandlesendmessagehwnd

C++ Why Sendmessage doesnt work?


In C#, this SendMessage function raise up volume successfull:

[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

SendMessage(Handle, 0x319, (int)Handle, APPCOMMAND_VOLUME_UP);

But in C++, this SendMessage function dont work, that is volume doesnt rise up:

// HWND hwnd = CreateWindow(...
SetWindowText(hwnd, "Hi"); // Worked
SendMessage(hwnd, WM_APPCOMMAND, (int)hwnd, APPCOMMAND_VOLUME_UP); // Don't work

What do I wrong? Can you help me how to fix it? Thank you!

Full code:

#include <windows.h>
HHOOK _hhook;
HWND hwnd;
HINSTANCE _hInstance;

KBDLLHOOKSTRUCT kbdStruct;

LRESULT __stdcall HookCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode >= 0)
    {
        if (wParam == WM_KEYDOWN)
        {
            // lParam is the pointer to the struct containing the data needed, so cast and assign it to kdbStruct.
            kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
            // a key (non-system) is pressed.
            if (kbdStruct.vkCode == VK_F1)
            {
                // F1 is pressed!
                SetWindowText(hwnd, "Hi");
                SendMessage(hwnd, WM_APPCOMMAND, (WPARAM)hwnd, APPCOMMAND_VOLUME_UP);
                //MessageBox(NULL, "F1 is pressed!", "key pressed", MB_ICONINFORMATION);
            }
        }
    }
    return CallNextHookEx(_hhook, nCode, wParam, lParam);
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow );

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow ) 
{
    WNDCLASS wc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
    wc.hCursor = LoadCursor( NULL, IDC_HAND );
    wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = TEXT("Philip");
    wc.lpszMenuName = 0;
    wc.style = CS_HREDRAW | CS_VREDRAW;

    RegisterClass( &wc );
    hwnd = CreateWindow(
        TEXT("Philip"),
        TEXT("Hello World!"),
        WS_OVERLAPPEDWINDOW,
        10, 10,
        500, 200,
        NULL, NULL,
        hInstance, NULL );

    ShowWindow(hwnd, iCmdShow );
    UpdateWindow(hwnd);

    _hhook = SetWindowsHookEx(WH_KEYBOARD_LL, HookCallback, NULL, 0);

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

LRESULT CALLBACK WndProc(   HWND hwnd,
                            UINT message,
                            WPARAM wparam,
                            LPARAM lparam )
{

    switch( message )
    {

    case WM_DESTROY:
        PostQuitMessage( 0 ) ;
        return 0;
        break;


    }
    return DefWindowProc( hwnd, message, wparam, lparam );
}

Solution

  • You need to use MAKELPARAM macro to pass APPCOMMAND_VOLUME_UP to SendMessage MAKELPARAM(0,APPCOMMAND_VOLUME_UP) Final version should look like

    SendMessage(hwnd, WM_APPCOMMAND, (WPARAM)hwnd, MAKELPARAM(0,APPCOMMAND_VOLUME_UP));