程序无法在其他 windows 台机器上正常运行

Program not working right on other windows machines

我的应用程序出现问题,我正在尝试获取 运行 所在系统的所有网络配置。最终目标是找到具有最高优先级的 MAC 地址。

当我使用 QtCreator 运行 时,代码 运行 可以正常工作,当我创建包含 dll 文件和 exe 文件的文件夹时,运行 也可以正常工作。

但问题是,当我在其他 windows 机器(7 和 10)上 运行 这个程序时,它 运行 但不 return 或显示任何东西.我尝试 运行 以管理员身份对其进行设置,但均无效,此代码应该能够在所有 windows 平台上运行。

有什么建议吗?

我目前在 Windows 10 并使用 Qt 5.8 MSVC 2015

带有这些 dll 的 exe 文件 运行s Windows 10:

windows7:

也应该有这些 dll

Link 下面是exe和dll文件的合集:

https://ufile.io/e9htu

如果需要,这是我的代码:

main.cpp

#include <QCoreApplication>
#include "macfinder.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    MACFinder macFinder;
    macFinder.findMAC();
    return a.exec();
}

macfinder.h

#ifndef MACFINDER_H
#define MACFINDER_H

#include <QObject>
#include <QNetworkConfiguration>
#include <QNetworkConfigurationManager>
#include <QNetworkInterface>
#include <QNetworkSession>
#include <QDebug>

class MACFinder : public QObject
{
    Q_OBJECT
public:
    explicit MACFinder(QObject *parent = 0);
    void findMAC();
private:
    QNetworkConfigurationManager ncm;
    QString filterMAC(QList<QNetworkConfiguration> configs);

signals:
    void foundMAC(QString MAC);
private slots:
    void configurationsUpdateCompleted();
};

#endif // MACFINDER_H

macfinder.cpp

#include "macfinder.h"

MACFinder::MACFinder(QObject *parent) : QObject(parent)
{
}

QString MACFinder::filterMAC(QList<QNetworkConfiguration> configs)
{
    qDebug() << "MAC and Index: ";
    QString MAC;
    int index;
    QNetworkConfiguration nc;
    foreach(nc,configs)
    {
        QNetworkSession networkSession(nc);
        QNetworkInterface netInterface = networkSession.interface();
        QString debugStr = QString::number(netInterface.index())
                + " | " + netInterface.humanReadableName() + " | "
                + nc.name() + " | " + netInterface.hardwareAddress();
        if(netInterface.hardwareAddress().isEmpty())
        {
            qDebug() << "--> No MAC: " << debugStr;
            continue;
        }
        if(netInterface.name().isEmpty())
        {
            qDebug() << "--> NO NAME: " << debugStr;
            continue;
        }
        if(netInterface.index() == 0)
        {
            qDebug() << "--> NO INDEX: " << debugStr;
            continue;
        }
        if(netInterface.flags() & QNetworkInterface::IsLoopBack)
        {
            qDebug() << "--> loopBack: " << debugStr;
            continue;
        }
        if(netInterface.flags() & (QNetworkInterface::IsRunning | QNetworkInterface::IsUp))
        {
            qDebug() << "*** Accepted: " << debugStr;
            if(MAC.isEmpty())
            {
                qDebug() << "setting MAC:" << debugStr;
                MAC = netInterface.hardwareAddress();
                index = netInterface.index();
            }
            else
            {
                if(netInterface.index() < index)
                {
                    qDebug() << "setting MAC:" << debugStr;
                    MAC = netInterface.hardwareAddress();
                    index = netInterface.index();
                }
                else
                    qDebug() << "index is not lower: " << debugStr;
            }
        }
    }
    return MAC;
}

void MACFinder::findMAC()
{
    qDebug() << "MACFinder::findMAC | updating all configurations";
    connect(&ncm,SIGNAL(updateCompleted()),this,SLOT(configurationsUpdateCompleted()));
    ncm.updateConfigurations();
}

void MACFinder::configurationsUpdateCompleted()
{
    qDebug() << "MACFinder::configurationsUpdateCompleted";
    disconnect(&ncm,SIGNAL(updateCompleted()),this,SLOT(configurationsUpdateCompleted()));
    QNetworkConfiguration nc;
    QList<QNetworkConfiguration> configs = ncm.allConfigurations(QNetworkConfiguration::Active);
    qDebug() << "\nAllConfigs: ";
    foreach (nc,configs)
    {
        qDebug() << nc.identifier() << nc.name() << nc.state();
    }
    QString MAC = filterMAC(configs);
    qDebug() << "\nMAC:" << MAC;
    if(MAC.isEmpty())
    {
        qDebug("no MAC address found");
    }
    emit foundMAC(MAC);
}

我下载了你的应用程序并在我的电脑上进行了分析。

问题是您缺少一些 dll,您的应用程序 运行 没有错误但无法正常工作。 (缺少 qgenericbearer.dllqnativewifibearer.dll 和文件夹 bearer )。

您可以使用 windeploy 命令来部署您的项目。

转到 OS 上的 Qt 编译器目录,例如:

C:\Qt\Qt5.7.0.7\msvc2013\bin

Shift+right click mouse 然后 click open command window here

输入windeployqt.exe c:\你的app目录 例如:

windeployqt.exe C:\Users\Mofrad\Downloads\macfindertest\macFinderTest\macAddressTest.exe

现在一些 dll 将复制到您的应用程序目录。

现在再次尝试您的应用程序,您会发现它正在运行。