我有一个在IE上使用的VC++的控件,在界面上我想将当前文件的版本显示出来,以便给用户确认版本是否正确。
我一般要在资源里面修改版本信息,然后还需要修改源程序中显示给用户看的版本字符串,这样实在太麻烦了。我希望能够通过程序获取当前文件的版本号,但是我发现使用GetFileVersionInfo函数只能够获得IE进程的版本号,根本就不是我自身的ocx文件的版本。

解决方案 »

  1.   

    以前写的取描述信息的函数,只要把里面的FileDescription换成FileVersion即可。CString GetFileDescription(LPCTSTR lpszFile)
    {
    HRESULT hr; struct LANGANDCODEPAGE {
    WORD wLanguage;
    WORD wCodePage;
    } *lpTranslate; CString strReturn(lpszFile);
    TCHAR *pFilename = strReturn.GetBuffer(MAX_PATH); PathStripPath(pFilename);
    PathRemoveExtension (pFilename);
    strReturn.ReleaseBuffer();
    DWORD dwSize = GetFileVersionInfoSize(lpszFile,NULL);
    BYTE *pBlock = new BYTE[dwSize];
    if (!GetFileVersionInfo(lpszFile,0,dwSize,pBlock))
    {
    delete[] pBlock;
    return strReturn;
    }
    // Read the list of languages and code pages.
    UINT cbTranslate;
    VerQueryValue(pBlock, 
    TEXT("\\VarFileInfo\\Translation"),
    (LPVOID*)&lpTranslate,
    &cbTranslate); // Read the file description for each language and code page. TCHAR SubBlock[50];
    for(int i=0; i < (cbTranslate/sizeof(struct LANGANDCODEPAGE)); i++ )
    {
    hr = StringCchPrintf(SubBlock, 50,
    TEXT("\\StringFileInfo\\%04x%04x\\FileDescription"),
    lpTranslate[i].wLanguage,
    lpTranslate[i].wCodePage);
    if (FAILED(hr))
    {
    delete[] pBlock;
    }
    LPVOID lpBuffer;
    UINT dwBytes;
    // Retrieve file description for language and code page "i". 
    if (VerQueryValue(pBlock, 
    SubBlock, 
    &lpBuffer,
    &dwBytes))
    {
    //CString strR((char*)lpBuffer,dwBytes);
    strReturn.SetString((TCHAR*)lpBuffer,dwBytes);
    delete[] pBlock;
    return strReturn;
    }
    }
    return strReturn;
    }
      

  2.   

    if (FAILED(hr))
            {
                delete[] pBlock;
            }
    里面少了一个break;
      

  3.   

    需要那么麻烦么...你自己在 ocx 里面做个 ver 的函数不就行了,还非要分析文件版本...
      

  4.   

    刚才没仔细看,应该是需要使用OCX的HMODULE来取得文件名,然后使用GetFileVersionInfo。
      

  5.   


    是啊,那怎么获得ocx的hmodule呢?
    GetModuleHandle(NULL)返回的是IE的
      

  6.   

    如果你使用的是CWinApp,那么就使用m_hInstance,否则需要保存DllMain的那个hInstance
      

  7.   

    ActiveX的工程里面没有m_hInstance的,至少我没有找到不过我现在知道了:GetModuleFileName( AfxGetInstanceHandle(), szttt, MAX_PATH );用这个可以获得当前我的控件的OCX文件路径,然后再用GetFileVersionInfo即可。谢谢大家。此帖结束了。