0

I want to add function to my program to turn monitor off in Qt. I added this popular Winapi code from internet:

    void MainWindow::on_Start_TurnOffMonitor_clicked()
{
    SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
}

but while compiling I'm geting error:

mainwindow.obj:-1: error: LNK2019: unresolved external symbol __imp__SendMessageW@16 referenced in function "private: void __thiscall MainWindow::on_Start_TurnOffMonitor_clicked(void)" (?on_Start_TurnOffMonitor_clicked@MainWindow@@AAEXXZ)

How could I fix it?

km2442
  • 779
  • 2
  • 11
  • 31
  • SendMessageW doesn't exist in the linker. Does SendMessageA exist? if so, you your linker settings for the project are inconsistent and you probably mixed wide string and ASCII. Alternatively you might have a macro defined which replaces SendMessage with SendMessageW (this is what happens e.g. if you include windows.h), but you actually want to call SendMessage. – Peter Oct 16 '15 at 21:57
  • Yes, I have defined windows.h I see this is my problem. So, could I avoid it without undefining windows.h? – km2442 Oct 16 '15 at 22:27
  • 2
    Nothing to do with headers. Pass user32.lib to the linker. The function needs a definition. – David Heffernan Oct 16 '15 at 22:39
  • Will try this tomorrow, will write if this works. Thanks – km2442 Oct 16 '15 at 22:49
  • 1
    [21 answers about unresolved externals](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix). – theB Oct 17 '15 at 01:57
  • @DavidHeffernan Thank you, passing user32.lib fix my problem :) – km2442 Oct 17 '15 at 10:10
  • 1
    @Peter: `SendMessageA` and `SendMessageW` are exported from the same library (user32.lib). The `A`-suffix is short for ANSI, not ASCII. All of this is [documented](https://msdn.microsoft.com/en-us/library/windows/desktop/ms644950.aspx). – IInspectable Oct 17 '15 at 13:07

0 Answers0