2014年3月3日 星期一

[Win32] How to get the app owner version?

Example:

    CString strResult;
    TCHAR szModPath[MAX_PATH];
    ZeroMemory(szModPath, sizeof(TCHAR) * MAX_PATH);
    GetModuleFileName(NULL, szModPath, sizeof(szModPath));
    DWORD dwHandle;

    DWORD dwSize = GetFileVersionInfoSize(szModPath, &dwHandle);
    if (dwSize > 0)
    {
        BYTE* pbBuf = static_cast<BYTE*>(alloca(dwSize));
        if (GetFileVersionInfo(szModPath, dwHandle, dwSize, pbBuf))
        {
            UINT uiSize;
            BYTE* lpb;
            if (VerQueryValue(pbBuf, L"\\VarFileInfo\\Translation", (void**)&lpb, &uiSize))
            {
                WORD* lpw = (WORD*)lpb;
                CString strQuery;
                strQuery.Format(L"\\StringFileInfo\\%04x%04x\\ProductVersion", lpw[0], lpw[1]);
                if (VerQueryValue(pbBuf, const_cast<LPTSTR>((LPCTSTR)strQuery), (void**)&lpb, &uiSize ) && uiSize > 0)
                {
                    strResult = (LPCTSTR)lpb;
                }
            }
        }
    }

[Win32] How to get the OS Version?

DWORD WINAPI GetVersion(void);

Examples:
#include <windows.h>
#include <stdio.h>

void main()
{
    DWORD dwVersion = 0; 
    DWORD dwMajorVersion = 0;
    DWORD dwMinorVersion = 0; 
    DWORD dwBuild = 0;

    dwVersion = GetVersion();
    // Get the Windows version.

    dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
    dwMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));

    // Get the build number.

    if (dwVersion < 0x80000000)              
        dwBuild = (DWORD)(HIWORD(dwVersion));

    printf("Version is %d.%d (%d)\n", 
                dwMajorVersion,
                dwMinorVersion,
                dwBuild);
}

Reference:http://msdn.microsoft.com/en-us/library/windows/desktop/ms724439(v=vs.85).aspx