Qt-获取EXE版本信息(Qt5+win10)(LPCWSTR、std::wstring)

 

相关资料:

https://blog.csdn.net/scz653037148/article/details/83307252         Qt 获取exe版本等详细信息

https://blog.csdn.net/qq_33233768/article/details/81665275          QString与std::wstring的转换问题

https://download.csdn.net/download/zhujianqiangqq/72408654        CSDN代码包下载

 

代码:

.pro

 1 QT       += core gui
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++11
 6 
 7 # The following define makes your compiler emit warnings if you use
 8 # any Qt feature that has been marked deprecated (the exact warnings
 9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12 
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
17 
18 SOURCES += \
19     main.cpp \
20     mainwindow.cpp
21 
22 HEADERS += \
23     mainwindow.h
24 
25 FORMS += \
26     mainwindow.ui
27 
28 #LIBS += -lUser32
29 LIBS += -lVersion
30 
31 # Default rules for deployment.
32 qnx: target.path = /tmp/$${TARGET}/bin
33 else: unix:!android: target.path = /opt/$${TARGET}/bin
34 !isEmpty(target.path): INSTALLS += target
View Code

main.h

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <Windows.h>
 5 #include "winver.h"
 6 #include <QMainWindow>
 7 #include <QDebug>
 8 
 9 using namespace std;
10 
11 QT_BEGIN_NAMESPACE
12 namespace Ui { class MainWindow; }
13 QT_END_NAMESPACE
14 
15 class MainWindow : public QMainWindow
16 {
17     Q_OBJECT
18 
19 public:
20     MainWindow(QWidget *parent = nullptr);
21     ~MainWindow();
22 
23 private slots:
24     void on_pushButton_clicked();
25 
26 private:
27     Ui::MainWindow *ui;
28 };
29 #endif // MAINWINDOW_H
View Code

mainwindow.cpp

  1 #include "mainwindow.h"
  2 #include "ui_mainwindow.h"
  3 
  4 MainWindow::MainWindow(QWidget *parent)
  5     : QMainWindow(parent)
  6     , ui(new Ui::MainWindow)
  7 {
  8     ui->setupUi(this);
  9 }
 10 
 11 MainWindow::~MainWindow()
 12 {
 13     delete ui;
 14 }
 15 
 16 std::wstring qToStdWString(const QString &str)
 17 {
 18 #ifdef _MSC_VER
 19  return std::wstring((const wchar_t *)str.utf16());
 20 #else
 21  return str.toStdWString();
 22 #endif
 23 }
 24 
 25 QString GetFileVertion(QString fullName){
 26     DWORD dwLen = 0;
 27     char* lpData=NULL;
 28     LPCWSTR  str_path;
 29     std::wstring ss = qToStdWString(fullName);
 30     str_path = ss.c_str();
 31     BOOL bSuccess = FALSE;
 32     QString fileInfomation;
 33     DWORD vHandle=0;
 34     //获得文件基础信息 【Q tea  原创】
 35     //--------------------------------------------------------
 36     dwLen = GetFileVersionInfoSize( str_path, &vHandle);
 37     qDebug()<<"GetLastError:" << GetLastError();
 38     if (0 == dwLen)
 39     {
 40     qDebug()<<"获取版本字节信息失败!";
 41     return"";
 42     }
 43     qDebug()<<"版本信息字节大小:"<<dwLen;
 44     lpData =(char*)malloc(dwLen+1);
 45     if (NULL == lpData)
 46     {
 47         qDebug()<<"分配内存失败";
 48      return "";
 49     }
 50     bSuccess = GetFileVersionInfo( fullName.toStdWString().c_str(),0, dwLen+1, lpData);
 51     if (!bSuccess)
 52     {
 53     qDebug()<<"获取文件版本信息错误!";
 54 
 55     return"";
 56     }
 57     LPVOID lpBuffer = NULL;
 58     UINT uLen = 0;
 59 
 60     //获得语言和代码页(language and code page),规定,套用即可
 61     //---------------------------------------------------
 62    bSuccess = VerQueryValue( lpData,
 63     (TEXT("\\VarFileInfo\\Translation")),
 64     &lpBuffer,
 65     &uLen);
 66     QString strTranslation,str1,str2;
 67     unsigned short int *p =(unsigned short int *)lpBuffer;
 68     str1.setNum(*p,16);
 69     str1="000"+ str1;
 70     strTranslation+= str1.mid(str1.size()-4,4);
 71     str2.setNum(*(++p),16);
 72     str2="000"+ str2;
 73     strTranslation+= str2.mid(str2.size()-4,4);
 74 
 75     QString str_value;
 76      QString code;
 77     //以上步骤需按序进行,以下步骤可根据需要增删或者调整
 78 
 79     //获得文件说明:FileDescription
 80     //---------------------------------------------------------
 81     code ="\\StringFileInfo\\"+ strTranslation +"\\FileDescription";
 82     bSuccess = VerQueryValue(lpData,
 83     (code.toStdWString().c_str()),
 84     &lpBuffer,
 85     &uLen);
 86     if (!bSuccess)
 87     {
 88     qDebug()<<"Get file verstion error!";
 89     }
 90     else
 91     {
 92     str_value="文件说明:" + QString::fromUtf16((const unsigned short int *)lpBuffer)+"\n";
 93     fileInfomation +=str_value;
 94 }
 95     //获得文件版本信息:FileVersion
 96     //-----------------------------------------------------
 97      code ="\\StringFileInfo\\"+ strTranslation +"\\FileVersion";
 98     bSuccess = VerQueryValue(lpData,
 99     (code.toStdWString().c_str()),
100     &lpBuffer,
101     &uLen);
102     if (!bSuccess)
103     {
104     qDebug()<<"获取文件版本信息错误!";
105 
106     }
107     else
108     {
109     str_value="文件版本信息:" + QString::fromUtf16((const unsigned short int *)lpBuffer)+"\n";
110     fileInfomation +=str_value;
111     }
112     //获得文件产品名称
113     //---------------------------------------------------------
114     code ="\\StringFileInfo\\"+ strTranslation +"\\ProductName";
115     bSuccess = VerQueryValue(lpData,
116     (code.toStdWString().c_str()),
117     &lpBuffer,
118     &uLen);
119     if (!bSuccess)
120     {
121     qDebug()<<"Get file ProductName error!";
122 
123     }
124     else
125     {
126     str_value="产品名称:" + QString::fromUtf16((const unsigned short int *)lpBuffer)+"\n";
127     fileInfomation +=str_value;
128    }
129     //获得产品版本信息:ProductVersion
130     //-----------------------------------------------------
131      code ="\\StringFileInfo\\"+ strTranslation +"\\ProductVersion";
132     bSuccess = VerQueryValue(lpData,
133     (code.toStdWString().c_str()),
134     &lpBuffer,
135     &uLen);
136     if (!bSuccess)
137     {
138     qDebug()<<"获取产品版本信息错误!";
139 
140     }
141     else
142     {
143     str_value="产品版本信息:" + QString::fromUtf16((const unsigned short int *)lpBuffer)+"\n";
144     fileInfomation +=str_value;
145     }
146     //获得文件内部名称 InternalName
147     //---------------------------------------------------------
148     code ="\\StringFileInfo\\"+ strTranslation +"\\InternalName";
149     bSuccess = VerQueryValue(lpData,
150     (code.toStdWString().c_str()),
151     &lpBuffer,
152     &uLen);
153     if (!bSuccess)
154     {
155     qDebug()<<"Get file InternalName error!";
156 
157     }
158     else
159     {
160     str_value="内部名称:" + QString::fromUtf16((const unsigned short int *)lpBuffer)+"\n";
161     fileInfomation +=str_value;
162    }
163 
164 
165     //获得原始文件名 OriginalFileName
166     //---------------------------------------------------------
167     code ="\\StringFileInfo\\"+ strTranslation +"\\OriginalFileName";
168     bSuccess = VerQueryValue(lpData,
169     (code.toStdWString().c_str()),
170     &lpBuffer,
171     &uLen);
172     if (!bSuccess)
173     {
174     qDebug()<<"Get file OriginalFileName error!";
175 
176     }
177     else
178     {
179     str_value="原始文件名:" + QString::fromUtf16((const unsigned short int *)lpBuffer)+"\n";
180     fileInfomation +=str_value;
181 }
182     //获得合法版权 LegalTradeMarks 【Q tea  原创】
183     //---------------------------------------------------------
184     code ="\\StringFileInfo\\"+ strTranslation +"\\Type";
185     bSuccess = VerQueryValue( lpData,
186     (code.toStdWString().c_str()),
187     &lpBuffer,
188     &uLen);
189     if (!bSuccess)
190     {
191     qDebug()<<"Get file LegalTradeMarks error!";
192 
193 
194     }
195     else
196     {
197     str_value="版权:" + QString::fromUtf16((const unsigned short int *)lpBuffer)+"\n";
198     fileInfomation +=str_value;
199    }
200 
201 delete lpData;
202 
203 return fileInfomation;
204 }
205 
206 void MainWindow::on_pushButton_clicked()
207 {
208     ui->textEdit->append(GetFileVertion(QString("D:/vrtsconApp.exe")));
209 }
View Code

 

问题描述:
在qt工程中调用QString::fromStdWString()时,老是报错error LNK2019: 无法解析的外部符号...QString::fromStdWString(...)...

原因:
在Qt库中 wchar_t 不是内置类型,所以QT建议我们构建基于Qt的软件时,也不要将wchar_t作为内置类型。但是在一些情况下,其他的库构建时可能已经将wchar_t作为了内置类型。当使用std::wstring、QString::toStdWString()和String::fromStdWString() 将导致链接错误.

解决方案:
QString转成std::wstring:
/*! Convert an QString to a std::wstring */
std::wstring qToStdWString(const QString &str)
{
#ifdef _MSC_VER
return std::wstring((const wchar_t *)str.utf16());
#else
return str.toStdWString();
#endif
}

std::wstring转成QString:
/*! Convert an std::wstring to a QString */
QString stdWToQString(const std::wstring &str)
{
#ifdef _MSC_VER
return QString::fromUtf16((const ushort *)str.c_str());
#else
return QString::fromStdWString(str);
#endif

 

posted on 2021-12-30 22:18  疯狂delphi  阅读(542)  评论(0编辑  收藏  举报

导航