Search code examples
c++qtmcisendstring

Using mciSendString in Qt project


I’m trying to use the mciSendString function to open and close the cd tray in Qt. Here is the code:

#include <windows.h>
MCIERROR mciSendString(LPCTSTR lpszCommand, LPTSTR lpszReturnString, UINT cchReturn, HANDLE hwndCallback);
void OpenCD()
{
    mciSendString((LPCTSTR)"set cdaudio door open",(LPTSTR)NULL, (UINT)0, (HANDLE)NULL);
}
void CloseCD()
{
    mciSendString((LPCTSTR)"set cdaudio door closed",(LPTSTR)NULL, (UINT)0, (HANDLE)NULL);
}    

But I get this error: “ undefined reference to `mciSendStringW(wchar_t const*, wchar_t*, unsigned int, void*)' ” How can I fix this error and use the mciSendString function without any problem?


Solution

  • Try add one of the next lines to pro-file:

    LIBS += path_to_lib/Winmm.lib
    

    or

    LIBS += path_to_lib/Winmm.dll
    

    Also you can include Mmsystem.h and Windows.h

    Link
    For example:
    Pro file inludes link to lib-file from installed SDK:

    LIBS += $$quote(C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0A\\Lib\\WinMM.lib)
    

    In cpp-file:

    #include "Windows.h"
    #include "mmsystem.h"
    //MCIERROR mciSendString(LPCTSTR lpszCommand, LPTSTR lpszReturnString, UINT cchReturn, HANDLE hwndCallback);
    ...
    void MainWindow::on_pushButton_clicked() {
       mciSendString((LPCTSTR)"set cdaudio door open",(LPTSTR)NULL, (UINT)0, (HWND__*)0);
    }