天天看點

Qt在windows與Mac OS中擷取執行程式版本号

1 windows中擷取執行檔案exe的版本号

QString GetFileVertion(QString aFullName)

{

         QString vRetVersion;

         string vFullName = QStringToString(aFullName);

         char vVersion[256] = {0};

         DWORD vHandle = 0;

         DWORD vInfoSize = 0;

         vInfoSize = ::GetFileVersionInfoSize(vFullName.c_str(), &vHandle);

         if(vInfoSize <= 0)

         {      

                   return false;

         }

         char *pData = new char[vInfoSize + 1];

         memset(pData, 0, vInfoSize + 1);

         if(!::GetFileVersionInfo(vFullName.c_str(), NULL, vInfoSize, pData))   

         {  

                   if(pData)

                   {

                            delete pData;

                   }

                   return   FALSE;  

         }  

         string vTempStr = "\\";

         VS_FIXEDFILEINFO *pFileInfo;  

         unsigned int vUInfoSize = 0; 

         if(!::VerQueryValue(pData, vTempStr.c_str(), (void**)&pFileInfo, &vUInfoSize))

         {

         WORD vVer[4];  

         vVer[0] = HIWORD(pFileInfo->dwProductVersionMS);    

         vVer[1] = LOWORD(pFileInfo->dwProductVersionMS);  

         vVer[2] = HIWORD(pFileInfo->dwProductVersionLS);  

         vVer[3] = LOWORD(pFileInfo->dwProductVersionLS);   

         sprintf(vVersion, "%d.%d.%d.%d", vVer[0], vVer[1], vVer[2], vVer[3]);

         if(pData)

                   delete pData;

         string vVerStr = vVersion;

         vRetVersion = StringToQString(vVerStr);

         return vRetVersion;

}

2 Mac Os中擷取執行檔案app的版本号

Mac Os 擷取執行檔案app的版本号需要從Info.plist檔案中擷取,

Info.plist檔案為XML格式 類似如下:

http://www.apple.com/DTDs/PropertyList-1.0.dtd">

 CFBundleExecutable

 VxActuator

 CFBundleGetInfoString

 Created by Qt/QMake

 CFBundleIconFile

 CFBundleIdentifier

 com.yourcompany.VxActuator

 CFBundlePackageType

 APPL

 CFBundleSignature

 ????

 CFBundleVersion

 V0.8.0.001

 NOTE

 This file was generated by Qt/QMake.

其中以下為檔案版本資訊

注意: 如無以上資訊,請在工程裡設定 Version 項

         QString vFullName = aFullName + "/Contents/Info.plist";

         if (!VxIsFileExist(vFullName))

                   return "";

         QFile vXmlFile(vFullName);

         if (!vXmlFile.open(QIODevice::ReadOnly))

                   vXmlFile.close();

         QTextStream vReadStream(&vXmlFile);

         QTextCodec *vCodec = QTextCodec::codecForName("UTF-8");

         vReadStream.setCodec(vCodec);

         QString vXmlDataStr = vReadStream.readAll();

         vXmlFile.close();

         QDomDocument vXmlDoc;

         if (!vXmlDoc.setContent(vXmlDataStr))

         QDomElement vXmlRoot = vXmlDoc.documentElement();

         if (QString::compare(vXmlRoot.tagName(), "plist", Qt::CaseInsensitive) != 0)

         QDomNode vDictNode = vXmlRoot.namedItem("dict");

         if (vDictNode.isNull())

         QDomElement vDictElement = vDictNode.toElement();

         QDomNode vChildNode = vDictElement.firstChild();

         while(!vChildNode.isNull())

                   QDomElement vChildElement = vChildNode.toElement();

                   if(QString::compare(vChildElement.tagName(), "key", Qt::CaseInsensitive) == 0)

                            if(QString::compare(vChildElement.text(), "CFBundleVersion", Qt::CaseInsensitive) == 0)

                            {

                                     vChildNode = vChildNode.nextSibling();

                                     vRetVersion = vChildNode.toElement().text();

                                     break;

                            }

                   vChildNode = vChildNode.nextSibling();

上一篇: 目錄