I was trying to call GetPhysicalCusrorPos() compiling with g++ (mingw), and it turned out that I my sdk is outdated. So I went to download the last MinGW with latest w32api, but this function is still missing from the headers... So I started googling around about the following questions, only to get more and more lost :
_ When you want to work with the windows API but with MinGW, is w32api the good and only way to reach it ?
_ What is exactly the relationship between the Windows API, the windows SDK, w32api, MinGW and the w32api ???
_ Can the gnu tools (MinGW so gcc, g++ etc) do the same as VisualC/WinSDK (cl.exe) ???
Please don't suppose that I didn't search for answers, I surely did but all the different answers I found lost me more than they helped me.
If someone could clarify all that would be very nice for all the lost beginners like me.
Thanks a lot !
As I understand it, it is not practical to use the MS SDK with the mingw compiler. You have to use a mingw specific SDK. And even the most up-to-date mingw releases appear not to support GetPhysicalCursorPos
.
Since you only need this one function, or perhaps this one and a handful of others, it is easy enough to supply the missing parts of the SDK yourself. For example:
#define WINVER 0x0600
#include <windows.h>
#include <iostream>
extern "C"
{
__declspec(dllimport) BOOL WINAPI GetPhysicalCursorPos(LPPOINT lpPoint);
}
int main()
{
POINT pt;
if (GetPhysicalCursorPos(&pt))
{
std::cout << pt.x << ", " << pt.y << std::endl;
}
}
Here we include a declaration of the API we wish to call. When you compile this you get the following error:
>g++ main.cpp -o main.exe
>C:\Users\xxx\AppData\Local\Temp\ccCCZZEk.o:main.cpp:(.text+0x1d): undefined
reference to `__imp_GetPhysicalCursorPos'
collect2.exe: error: ld returned 1 exit status
So we need to find a way for the linker to be able to resolve the symbol. In theory you can ask the linker, ld, to link to DLLs directly. But I have not been able to get that to work. Instead you can use a somewhat more laborious approach.
Make a .def file to define the exports of the user32 library where this function lives:
LIBRARY user32.dll
EXPORTS
GetPhysicalCursorPos = __imp_GetPhysicalCursorPos
Save that to a file named myuser32.def. Now, that's not all the functions that user32 exports, but we only want the ones that are missing from the import library that comes with mingw.
Now use dlltool to make an import library:
>dlltool -d myuser32.def -l myuser32.a
And then compile passing the import library:
>g++ main.cpp myuser32.a -o main.exe
Success this time. When we run the program:
>main
1302, 670