qt中调用windows api需要注意的

在qt中调用windows api除了引入头文件还要引入lib文件
比如调用FindWindow,只引入头文件(windows.h)则编译时会报错
需要在.h或.cpp文件中加上
#pragma comment(lib,"user32")

可以使用Qt的QSettings类来读写注册表数据,也可以直接调用Windows API来读写注册表数据。 以下是调用Windows API读写注册表数据的示例代码: ```c++ #include <windows.h> // 读取注册表的字符串值 bool readRegistryString(HKEY hKey, const QString& subKey, const QString& valueName, QString& value) { HKEY hSubKey = NULL; DWORD dwType = REG_SZ; DWORD dwSize = 0; if (RegOpenKeyEx(hKey, subKey.toStdWString().c_str(), 0, KEY_READ, &hSubKey) != ERROR_SUCCESS) return false; if (RegQueryValueEx(hSubKey, valueName.toStdWString().c_str(), NULL, &dwType, NULL, &dwSize) != ERROR_SUCCESS) { RegCloseKey(hSubKey); return false; } if (dwType != REG_SZ) { RegCloseKey(hSubKey); return false; } WCHAR* pBuffer = new WCHAR[dwSize / sizeof(WCHAR)]; if (RegQueryValueEx(hSubKey, valueName.toStdWString().c_str(), NULL, NULL, (LPBYTE)pBuffer, &dwSize) != ERROR_SUCCESS) { RegCloseKey(hSubKey); delete[] pBuffer; return false; } value = QString::fromWCharArray(pBuffer, dwSize / sizeof(WCHAR)); delete[] pBuffer; RegCloseKey(hSubKey); return true; } // 写入字符串值到注册表 bool writeRegistryString(HKEY hKey, const QString& subKey, const QString& valueName, const QString& value) { HKEY hSubKey = NULL; if (RegCreateKeyEx(hKey, subKey.toStdWString().c_str(), 0, NULL, 0, KEY_WRITE, NULL, &hSubKey, NULL) != ERROR_SUCCESS) return false; if (RegSetValueEx(hSubKey, valueName.toStdWString().c_str(), 0, REG_SZ, (LPBYTE)value.toStdWString().c_str(), (value.length() + 1) * sizeof(wchar_t)) != ERROR_SUCCESS) { RegCloseKey(hSubKey); return false; } RegCloseKey(hSubKey); return true; } int main(int argc, char *argv[]) { // 读取注册表的字符串值 QString value; if (readRegistryString(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer", "Shell Folders", value)) qDebug() << value; // 写入字符串值到注册表 if (writeRegistryString(HKEY_CURRENT_USER, "Software\\MyCompany\\MyApp", "MyValue", "Hello World")) qDebug() << "Write registry success."; return 0; } ``` 以上代码演示了如何使用Windows API来读写注册表数据。其,readRegistryString函数用于读取注册表的字符串值,writeRegistryString函数用于将字符串值写入注册表。在main函数,我们首先使用readRegistryString函数读取注册表的Shell Folders值,并输出到控制台上,然后使用writeRegistryString函数将字符串值Hello World写入到注册表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

宇龍_

若帮助到你,希望能给予鼓励!

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

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

打赏作者

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

抵扣说明:

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

余额充值