如何获取一个文件的版本信息?

DataChat.Club 2013-02-19 02:17:02
右击一个文件时,会显示此文件的属性和详细信息,使用C/批处理怎样获得文件版本呢?
...全文
614 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-02-20
  • 打赏
  • 举报
回复
//接上帖
void DumpBuiltInProps(IPropertySetStorage *pPropSetStg) {// Dump's built-in properties of a property storage.
    printf("\n==================================================\n");
    printf("BuiltInProperties Properties...\n");
    printf("==================================================\n");

    IPropertyStorage *pPropStg = NULL;
    HRESULT hr;

    // Open summary information, getting an IpropertyStorage.
    hr = pPropSetStg->Open(FMTID_SummaryInformation,
    STGM_READ | STGM_SHARE_EXCLUSIVE, &pPropStg);
    if (FAILED(hr)) {
        printf("No Summary-Information.\n");
        return;
    }
    // Array of PIDSI's you are interested in.
    struct pidsiStruct {
        char *name;
        long pidsi;
    } pidsiArr[] = {
        {"Title"          ,PIDSI_TITLE              }, // VT_LPSTR
        {"Subject"        ,PIDSI_SUBJECT            }, // ...
        {"Author"         ,PIDSI_AUTHOR             },
        {"Keywords"       ,PIDSI_KEYWORDS           },
        {"Comments"       ,PIDSI_COMMENTS           },
        {"Template"       ,PIDSI_TEMPLATE           },
        {"LastAuthor"     ,PIDSI_LASTAUTHOR         },
        {"Revision Number",PIDSI_REVNUMBER          },
        {"Edit Time"      ,PIDSI_EDITTIME           }, // VT_FILENAME (UTC)
        {"Last printed"   ,PIDSI_LASTPRINTED        }, // ...
        {"Created"        ,PIDSI_CREATE_DTM         },
        {"Last Saved"     ,PIDSI_LASTSAVE_DTM       },
        {"Page Count"     ,PIDSI_PAGECOUNT          }, // VT_I4
        {"Word Count"     ,PIDSI_WORDCOUNT          }, // ...
        {"Char Count"     ,PIDSI_CHARCOUNT          },
        {"Thumpnail"      ,PIDSI_THUMBNAIL          }, // VT_CF
        {"AppName"        ,PIDSI_APPNAME            }, // VT_LPSTR
        {"Doc Security"   ,PIDSI_DOC_SECURITY       }, // VT_I4
        {0                ,0                        },
    };
    // Count elements in pidsiArr.
    int nPidsi = 0;
    for (nPidsi=0; pidsiArr[nPidsi].name; nPidsi++);

    // Initialize PROPSPEC for the properties you want.
    PROPSPEC *pPropSpec = new PROPSPEC[nPidsi];
    PROPVARIANT *pPropVar = new PROPVARIANT[nPidsi];

    for (int i=0; i<nPidsi; i++) {
        ZeroMemory(&pPropSpec[i], sizeof(PROPSPEC));
        pPropSpec[i].ulKind = PRSPEC_PROPID;
        pPropSpec[i].propid = pidsiArr[i].pidsi;
    }

    // Read properties.
    hr = pPropStg->ReadMultiple(nPidsi, pPropSpec, pPropVar);

    if (FAILED(hr)) {
        printf("IPropertyStg::ReadMultiple() failed w/error %08lx\n",hr);
    } else {
        // Dump properties.
        for (i=0; i<nPidsi; i++) {
            printf("%16s: ", pidsiArr[i].name);
            DumpPropVariant(pPropVar + i);
        }
    }

    // De-allocate memory.
    delete [] pPropVar;
    delete [] pPropSpec;

    // Release obtained interface.
    pPropStg->Release();

}
void DumpCustomProps(IPropertySetStorage *pPropSetStg) {// Dump's custom properties of a property storage.
    printf("\n==================================================\n");
    printf("Custom Properties...\n");
    printf("==================================================\n");

    IPropertyStorage *pPropStg = NULL;
    HRESULT hr;
    IEnumSTATPROPSTG *pEnumProp;

    // Open User-Defined-Properties, getting an IpropertyStorage.
    hr = pPropSetStg->Open(FMTID_UserDefinedProperties,
        STGM_READ | STGM_SHARE_EXCLUSIVE, &pPropStg);
    if (FAILED(hr)) {
        printf("No User Defined Properties.\n");
        return;
    }

    // Get property enumerator.
    hr = pPropStg->Enum(&pEnumProp);
    if (FAILED(hr)) {
        pPropStg->Release();
        printf("Couldn't enumerate custom properties.\n");
        return;
    }

    // Enumerate properties.
    STATPROPSTG sps;
    ULONG fetched;
    PROPSPEC propSpec[1];
    PROPVARIANT propVar[1];
    while(pEnumProp->Next(1, &sps, &fetched)   ==  S_OK)   {
        // Build a PROPSPEC for this property.
        ZeroMemory(&propSpec[0], sizeof(PROPSPEC));
        propSpec[0].ulKind = PRSPEC_PROPID;
        propSpec[0].propid = sps.propid;

        // Read this property.
        hr = pPropStg->ReadMultiple(1, &propSpec[0], &propVar[0]);
        if (!FAILED(hr)) {
            // Translate Prop name into ASCII.
            char dbcs[1024];
            char *pbstr = (char *)sps.lpwstrName;
            int i = wcstombs(dbcs, sps.lpwstrName,*((DWORD *)(pbstr-4)));
            dbcs[i] = 0;

            // Dump this property.
            printf("%16s: ", dbcs);
            DumpPropVariant(&propVar[0]);
        }
    }

    // Release obtained interface.
    pEnumProp->Release();
    pPropStg->Release();

}
void DumpProps(char *filename) {// Dump's custom and built-in properties of a compound document.
    // Translate filename to Unicode.
    WCHAR wcFilename[1024];
// setlocale( LC_ALL, "" );
    int i = mbstowcs(wcFilename, filename, strlen(filename));
// setlocale( LC_ALL, "C" );
    wcFilename[i]  = 0;

    IStorage *pStorage = NULL;
    IPropertySetStorage *pPropSetStg = NULL;
    HRESULT hr;

    // Open the document as an OLE compound document.
    hr = ::StgOpenStorage(wcFilename, NULL,
    STGM_READ | STGM_SHARE_EXCLUSIVE, NULL, 0, &pStorage);

    if (FAILED(hr)) {
        if (hr == STG_E_FILENOTFOUND)
            printf("File not found.");
        else if (hr == STG_E_FILEALREADYEXISTS) {
            printf("Not a compound file.\nTry use WinRAR to open docx/xlsx/pptx,view docProps\\core.xml and docProps\\app.xml\n");
            if ('.'==filename[strlen(filename)-5]) TryWinRAR(filename);
        } else
            printf("StgOpenStorage() failed w/error %08lx", hr);
        return;
    }

    // Obtain the IPropertySetStorage interface.
    hr = pStorage->QueryInterface(IID_IPropertySetStorage, (void **)&pPropSetStg);
    if (FAILED(hr)) {
        printf("QI for IPropertySetStorage failed w/error %08lx", hr);
        pStorage->Release();
        return;
    }

    // Dump properties.
    DumpBuiltInProps(pPropSetStg);
    DumpCustomProps(pPropSetStg);

    // Release obtained interfaces.
    pPropSetStg->Release();
    pStorage->Release();
}
void main(int argc, char **argv) {
    if (argc != 2) {
        printf("OLE/ZIP Document Property Viewer\n");
        printf("Usage: %s filename", argv[0]);
        return;
    }
    setlocale( LC_ALL, "chs" );
    printf("[File] %s\n",argv[1]);
    DumpProps(argv[1]);
}
赵4老师 2013-02-20
  • 打赏
  • 举报
回复
#pragma comment(lib,"ole32")
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <ole2.h>
#include <locale.h>
void DumpXML(char *xml) {
    static char b[8192];
    static WCHAR wstr[1024];
    char *p,*q;
    FILE *f;
    int i,L;

    f=fopen(xml,"rb");
    if (NULL==f) return;
    fread(b,1,8192,f);
    fclose(f);
    printf("================= %s =================\n",xml);
    p=b;
    for (i=0;i<3;i++) {//跳过前两个标签
        p=strchr(p,'<');
        if (!p) return;
        p++;
    }
    while (1) {
        if ('/'!=p[0]) {
            q=strchr(p,'>');
            if (!q) return;
            printf("%32.*s: ",q-p,p);
            p=q+1;
            q=strchr(p,'<');
            if (!q) return;
            L=q-p;
            for (i=0;i<L;i++) if ((unsigned char)p[i]>=0x80) break;
            if (i<L) {
                i=MultiByteToWideChar(CP_UTF8,0,p,L,wstr,1024);
                wstr[i] = 0;
                printf("%.*s UTF8(%S)\n",L,p,wstr);
            } else {
                printf("%.*s\n",L,p);
            }
            p=q+1;
        } else {
            p=strchr(p,'<');
            if (!p) return;
            p++;
        }
    }
}
void TryWinRAR(char *filename) {
    char cmd[1080];

    if (access("c:\\progra~1\\winrar\\winrar.exe",0)) return;
    system("rd /s /q docProps 1>NUL 2>NUL");
    sprintf(cmd,"c:\\progra~1\\winrar\\winrar.exe x \"%.1000s\" docProps\\*.xml",filename);
    system(cmd);
    if (!access("docProps\\core.xml",0)) DumpXML("docProps\\core.xml");
    if (!access("docProps\\app.xml",0)) DumpXML("docProps\\app.xml");
    system("rd /s /q docProps 1>NUL 2>NUL");
}
void DumpPropVariant(PROPVARIANT *pPropVar) {// Dumps simple PROPVARIANT values.
    if (pPropVar->vt & VT_ARRAY) {// Don't iterate arrays, just inform as an array.
        printf("%32s%s",""," (Array)\n");
        return;
    }
    if (pPropVar->vt & VT_BYREF) {// Don't handle byref for simplicity, just inform byref.
        printf("%32s%s",""," (ByRef)\n");
        return;
    }
    switch(pPropVar->vt) {
    case VT_EMPTY:printf("%32s%s",""," (VT_EMPTY)\n"                                                );break;
    case VT_NULL :printf("%32s%s",""," (VT_NULL)\n"                                                 );break;
    case VT_BLOB :printf("%32s%s",""," (VT_BLOB)\n"                                                 );break;
    case VT_BOOL :printf("%-32s" " (VT_BOOL)\n",         pPropVar->boolVal ? "TRUE/YES" : "FALSE/NO");break;
    case VT_I2   :printf("%-32d" " (VT_I2)\n"  , (int)   pPropVar->   iVal                          );break;
    case VT_I4   :printf("%-32d" " (VT_I4)\n"  , (int)   pPropVar->   lVal                          );break;
    case VT_R4   :printf("%-32.2lf (VT_R4)\n"  , (double)pPropVar-> fltVal                          );break;
    case VT_R8   :printf("%-32.2lf (VT_R8)\n"  , (double)pPropVar-> dblVal                          );break;
    case VT_BSTR: // OLE Automation string.
        {
            // Translate into ASCII.
            char dbcs[1024];
            char *pbstr = (char *)pPropVar->bstrVal;
            int i = wcstombs(dbcs, pPropVar->bstrVal, *((DWORD *)(pbstr-4)));
            dbcs[i] = 0;
            printf("%-32s (VT_BSTR)\n", dbcs);
        }
    break;
    case VT_LPSTR: // Null-terminated string.
        {
            char *pbstr = (char *)pPropVar->pszVal;
            int i,L;
            L=strlen(pbstr);
            for (i=0;i<L;i++) if ((unsigned char)pbstr[i]>=0x80) break;
            if (i<L) {
                WCHAR wstr[1024];
                int i = MultiByteToWideChar(CP_UTF8,0,pbstr,-1,wstr,1024);
                wstr[i] = 0;
                printf("%-32s (VT_LPSTR) UTF8(%S)\n", pPropVar->pszVal,wstr);
            } else {
                printf("%-32s (VT_LPSTR)\n", pPropVar->pszVal);
            }
        }
    break;
    case VT_LPWSTR:
        printf("%-32S (VT_LPWSTR)\n", pPropVar->pwszVal);
    break;
    case VT_FILETIME:
        {
            FILETIME lft;
            FileTimeToLocalFileTime(&pPropVar->filetime, &lft);
            SYSTEMTIME lst;
            FileTimeToSystemTime(&lft, &lst);

            printf("%d-%02d-%02d %02d:%02d.%02d              (VT_FILETIME)\n",
                lst.wYear, lst.wMonth,  lst.wDay,
                lst.wHour, lst.wMinute, lst.wSecond);
        }
    break;
    case VT_CF: // Clipboard format.
        printf("%32s%s",""," (Clipboard format)\n");
    break;
    default: // Unhandled type, consult wtypes.h's VARENUM structure.
        printf("%32s (Unhandled type: 0x%08lx)\n", "", pPropVar->vt);
        break;
    }
}
//未完待续
唯恐天下不乱 2013-02-20
  • 打赏
  • 举报
回复
C语言只是一个语言,C语言中没有的。 批处理怎么获得我就不知道了 这个里面有个的C的代码 http://blog.csdn.net/cheungmine/article/details/4075479
帅得不敢出门 2013-02-20
  • 打赏
  • 举报
回复
只要掌握这些信息是存放在哪,结构如何,便可为所欲为了。
ForestDB 2013-02-19
  • 打赏
  • 举报
回复
没有找工具 找到个Windows API,希望有用。 http://msdn.microsoft.com/en-us/library/windows/desktop/ms647464(v=vs.85).aspx
always_learn 2013-02-19
  • 打赏
  • 举报
回复
文件头里面好像包含有版本信息,看下文件头的数据结构,2进制打开文件,找到版本信息的偏移地址直接取出即可。
whizer 2013-02-19
  • 打赏
  • 举报
回复
你首先要了解一下PE文件类型的具体定义, 如果你已经安装了VC开发环境,那么有一个应用程序叫dumpbin可以查看文件信息, 然后根据WinNT.h种定义的PE的结构可以提取出相关信息.
DataChat.Club 2013-02-19
  • 打赏
  • 举报
回复
引用 1 楼 Kaile 的回复:
pe文件格式,exe文件里包含版本信息
用C语言或者批处理如何获得这些版本信息呢?
Kaile 2013-02-19
  • 打赏
  • 举报
回复
pe文件格式,exe文件里包含版本信息

5,530

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧