How to use Windows API in Qt (A simple example)

Qt is a cross-platform framework that encapsulates API across many Operating Systems but one always faces situations in which he/she needs to access and use OS specific capabilities. Windows API is a massive collection of interfaces that allow a programmer to use and include Windows features in their programs.  Below is a simple example on how to use Windows API in Qt.

Let’s say we want to use GetSystemTime function in our Qt program. Searching Google for “GetSystemTime API” you’ll get the API Documentation from MSDN website. Parameters and return type values should be obvious:

void WINAPI GetSystemTime(_Out_ LPSYSTEMTIME lpSystemTime);


As seen in the definition, there are not return values and the input parameter is a pointer to SYSTEMTIME structure:

typedef struct _SYSTEMTIME {
	WORD wYear;
	WORD wMonth;
	WORD wDayOfWeek;
	WORD wDay;
	WORD wHour;
	WORD wMinute;
	WORD wSecond;
	WORD wMilliseconds;
} SYSTEMTIME, * PSYSTEMTIME;

Search for the requirements on the page and you’ll see the following:
Requirements:
Minimum supported client: Windows 2000 Professional [desktop apps | Windows Store apps]
Minimum supported server: Windows 2000 Server [desktop apps | Windows Store apps]
Header: Winbase.h (include Windows.h)
Library: Kernel32.lib
DLL: Kernel32.dll

This means you need to include “Windows.h” in your program by adding the following line:

#include "Windows.h"

You also need to add the library reference to your project by adding the following line to your PRO file:

LIBS += -lKernel32


And finally you need to have Kernel32.DLL where your application can access it. You need to either copy it to the folder where your application resides or add it to PATH. (in this case, Kernel32 is in Windows System folder which is already added to path so you can just skip doing this step.)

In fact the same procedure applies to any other framework or API that you want to use in C++. You need to do three simple steps:
1. Add include files to your program’s source
2. Add Library reference to your project
3. Make the DLL files visible to your application’s executable (This step can be omitted if you have access to Static libraries of the framework you are using)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偶是江湖中人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值