32

How to use the Windows API in MinGW?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SomeUser
  • 1,976
  • 15
  • 42
  • 60

3 Answers3

23

Whenever I've done this, I just

#include <windows.h>

and start coding. MinGW comes with a windows.h file so you don't need to do anything extra.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 13
    It is wrong to tell that MinGW comes with windows.h. People like me, who prefers copying latest packages and building MinGW by yourself (not using installer), should copy w32api package from MinGW download site. – Andrejs Cainikovs Oct 11 '09 at 19:33
  • 3
    my mingw has that header file, but it fails to link – jondinham Oct 27 '12 at 11:12
8

I occasionally use the Windows API for Qt apps that I build using Qt Creator/MinGW - I just #include the appropriate Windows SDK header (the headers come with MinGW) and it just works. However, you may need to #define a few things in order that some API calls are exposed. For example, I recently needed to call SHGetSpecialFolderPath (found in shlobj.h) but needed to define _WIN32_IE to 0x0400 first.

Rob
  • 76,700
  • 56
  • 158
  • 197
  • The need to `#define _WIN32_IE` is described [here](https://learn.microsoft.com/en-us/windows/win32/winprog/using-the-windows-headers#macros-for-conditional-declarations). – Tanz87 Aug 19 '20 at 21:34
3

In case if you installed MinGW as a part of MSYS on Windows then you can install Windows.h and other Win-API headers through following command inside MSYS shell:

pacman -S msys2-w32api-headers msys2-w32api-runtime

then header is located in

c:/MSYS_PATH/usr/include/w32api/windows.h

also you can search any package name (or sub-name) through e.g.

pacman -Ss w32api


If you're on Linux then Windows.h can be installed through

sudo apt install mingw-w64-common

then header is located here

/usr/share/mingw-w64/include/windows.h

you may probably need to include compiler's option

-I/usr/share/mingw-w64/include/

Arty
  • 14,883
  • 6
  • 36
  • 69