-2

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 );
}
TKX
  • 318
  • 1
  • 2
  • 7
NoName
  • 7,940
  • 13
  • 56
  • 108
  • 3
    We need a better description of the problem than "it didn't work." – Robert Harvey Apr 19 '13 at 17:28
  • What exactly doesn't work – makc Apr 19 '13 at 17:29
  • 3
    Was the window actually created or are you sending the message to `NULL`? We have no idea what's happening before you call `SendMessage` so it's hard to tell if the values you pass it are valid. – Captain Obvlious Apr 19 '13 at 17:30
  • @CaptainObvlious: I call it after `ShowWindow` and `UpdateWindow` – NoName Apr 19 '13 at 17:37
  • @TuyenTk Believe it or not, we already know from your code that you're calling it after `ShowWindow` and `UpdateWindow`. The point is that that isn't enough information. – JBentley Apr 19 '13 at 17:38
  • 1
    Show us how you create the window. Chances are the call to `CreateWindow` is failing and you are passing `SendMessage` an invalid window handle. – Captain Obvlious Apr 19 '13 at 17:38
  • 1
    You might want to familiarize yourself with the notion of a [Short Self Contained Correct/Compilable Example - SSCCE](http://sscce.org/). And post code that actually compiles. As it is now this is turning into an extended discussion on _how to ask a question_. – Captain Obvlious Apr 19 '13 at 17:49
  • 1
    Compilable example, or it didn't happen – David Heffernan Apr 19 '13 at 17:53
  • What's the return value of SendMessage? – user93353 Apr 19 '13 at 18:28
  • If you set a breakpoint on your WndProc, you'll see that SendMessage successfully delivered the message. The problem is not with SendMessage; please update the title accordingly. – Raymond Chen Apr 19 '13 at 19:17

2 Answers2

5

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));
alexrider
  • 4,449
  • 1
  • 17
  • 27
1

After some investigation I was able to get this working. The problem stems from passing an improperly formed lParam value. The command itself resides in the upper 16 bits of the value but you are not accounting for this. The following change should get you on your way:

SendMessage(hwnd, WM_APPCOMMAND, (WPARAM)hwnd, APPCOMMAND_VOLUME_UP << 16);
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74