首页 > 解决方案 > 在 Windows 7 下无法加载 QtBluetooth 5.12.0

问题描述

我将一些代码从 Qt 5.6.0 迁移到 Qt 5.12.0,两者均使用 Visual Studio 2015 编译。它有一些QtBluetooth用于常规(非“低能耗”)蓝牙的代码。使用 5.6.0,这曾经完美地工作。

使用 Qt 5.12.0,我的应用程序将无法加载。它报告丢失API-MS-WIN-CORE-WINRT-L1-1-0.DLLAPI-MS-WIN-CORE-WINRT-STRING-L1-1-0.DLL. 我不明白为什么需要这些 WinRT 文件。Dependency WalkerQtBluetooth.dll将这些库报告为缺失。

我尝试使用 Qt 5.12.0 编译我的 selft 并作为QtCreator安装的一部分下载。我尝试了 Windows 7 和 10,Windows 10 运行良好。总是收到此错误,我没有找到有关在哪里可以找到这些库或如何QtBluetooth不使用它们的信息。

我应该怎么做才能QtBluetooth在 Windows 下简单地运行基于应用程序?

编辑:提交的 Qt 错误:https ://bugreports.qt.io/browse/QTBUG-73272

标签: c++qtqtbluetooth

解决方案


如果您不需要低能耗并且可以打扰您的用户使用 Windows 系统设置对话框配对设备,那么我建议为不使用 QtBluetooth 的 Windows 编写包装器代码。IE

#include <Windows.h>

class win_con {
    ....

    HANDLE hcon;
    COMMTIMEOUTS *timeouts;

    // i.e. com_port = L"\\\\.\\COM1"; 
    void open_com(std::wstring com_port, int read_timeout, int write_timeout)
    {

        hcom = CreateFile(com_port.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, 
            OPEN_EXISTING, 0, nullptr);
        if (hcom == INVALID_HANDLE_VALUE) ...

        timeouts = new COMMTIMEOUTS();
        memset(timeouts, 0, sizeof(COMMTIMEOUTS));
        timeouts->ReadTotalTimeoutConstant = read_timeout;
        timeouts->WriteTotalTimeoutConstant = write_timeout;
        if (!SetCommTimeouts(hcom, timeouts)) ...

    }

    void write_data(QString data)
    {
        std::string stddata = data.toStdString();
        DWORD numwritten = 0;
        if (!WriteFile(hcom, stddata.c_str(),
                static_cast<DWORD>(stddata.length()), &numwritten, nullptr)) {
            ...
        }
    }

    QString read_data(int len)
    {
        #define BUFFER_SIZE 256
        char buffer[BUFFER_SIZE];
        DWORD data_read = 0;
        if (BUFFER_SIZE < len) ....
        for (int i = 0; i < BUFFER_SIZE; i++)
            buffer[i] = 0;

        ReadFile(hcom, &buffer, len, &data_read, nullptr);

        if (read == 0) ...
        if (read < len) ...

        return QString(buffer);
    }
}

推荐阅读