Native Windows API issue on Qt + win32 + mingw

I am trying to use native Windows API with Qt using the mingw toolbox. There are problems with some functions. What's happening? Is this a bug with the mingw name change?

#ifdef Q_WS_WIN
    HWND hwnd = QWidget::winId();
    HDC hdcEMF  = CreateEnhMetaFile(NULL, NULL, NULL, NULL ) ;
    Rectangle(hdcEMF,100,100,200,200);
    HENHMETAFILE hemf = CloseEnhMetaFile(hdcEMF);
    OpenClipboard(hwnd);
    EmptyClipboard();
    SetClipboardData(CF_ENHMETAFILE,hemf);
    CloseClipboard();
#else   
      

Errors:

undefined reference to "CreateEnhMetaFileW @ 16"

undefined reference to `Rectangle @ 20 '

undefined reference to `CloseEnhMetaFile @ 4 '

+1


source to share


4 answers


The functions CreateEnhMetaFileW()

and are CloseEnhMetaFile()

defined in the static library Gdi32.lib, so you must be sure to link it. Try adding -lgdi32

to the end of your command line that you are using to compile. If that doesn't work, you may need to provide the full path to Gdi32.lib by adding it instead -L/path/to/folder/containing/the/library -lgdi32

.



+4


source


If you want to use the Windows API in a Qt application then there is no need to declare the WinAPI functions extern "C", just include:

#include <qt_windows.h>

      



In the project file (.pro) add the libraries you are using:

LIBS += -luser32 -lshell32 -lgdi32

      

+2


source


It is possible that the functions are included but become crippled due to the C ++ assumption.

Look at the extern C {} expression. It is designed to declare functions that should not be named malformed to account for polymorphism / overloading. (IE has two functions with the same name).

0


source


@torn your solution worked for me.

I wanted to use win32 api call in my qt application.

#include #include your-win32-api-header

and finally the name LIBS + = -llibrary.

Note that you also need to specify -L for the correct paths.

0


source







All Articles