Welcome to mirror list, hosted at ThFree Co, Russian Federation.

VersionInfo.h « 3rdparty - github.com/windirstat/windirstat.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 05ce4b009a28bd8fd721dc33820b0e061016a73e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
///////////////////////////////////////////////////////////////////////////////
///
/// Little class which reads the version info from a loaded PE file.
///
/// Licensed under the MIT license (see below).
///
///////////////////////////////////////////////////////////////////////////////
///
/// Copyright (c) 2016, 2017 Oliver Schneider (assarbad.net)
///
/// Permission is hereby granted, free of charge, to any person obtaining a
/// copy of this software and associated documentation files (the "Software"),
/// to deal in the Software without restriction, including without limitation
/// the rights to use, copy, modify, merge, publish, distribute, sublicense,
/// and/or sell copies of the Software, and to permit persons to whom the
/// Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
/// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
/// DEALINGS IN THE SOFTWARE.
///
///////////////////////////////////////////////////////////////////////////////

#ifndef __VERSIONINFO_H_VER__
#define __VERSIONINFO_H_VER__ 2017091820
#if (defined(_MSC_VER) && (_MSC_VER >= 1020)) || defined(__MCPP)
#pragma once
#endif // Check for "#pragma once" support

#include <Windows.h>
#include <tchar.h>
#pragma warning(disable:4995)
#include <cstdio>
#pragma warning(default:4995)
#pragma comment(lib, "delayimp")

class CVersionInfo
{
    LPVOID m_lpVerInfo;
    VS_FIXEDFILEINFO* m_pFixedFileInfo;
    DWORD m_useTranslation;
public:
    CVersionInfo(HINSTANCE hInstance)
        : m_lpVerInfo(NULL)
        , m_pFixedFileInfo(NULL)
        , m_useTranslation(0)
    {
        HRSRC hVersionResource = ::FindResource(hInstance, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
        if (NULL != hVersionResource)
        {
            if (DWORD dwSize = ::SizeofResource(hInstance, hVersionResource))
            {
                if (HGLOBAL hVersionResourceData = ::LoadResource(hInstance, hVersionResource))
                {
                    if (LPVOID pVerInfoRO = ::LockResource(hVersionResourceData))
                    {
                        if (NULL != (m_lpVerInfo = ::LocalAlloc(LPTR, dwSize)))
                        {
                            ::CopyMemory(m_lpVerInfo, pVerInfoRO, dwSize);
                            UINT uLen;
                            if (::VerQueryValue(m_lpVerInfo, _T("\\"), (LPVOID*)&m_pFixedFileInfo, &uLen))
                            {
#ifdef ATLTRACE2
                                ATLTRACE2(_T("%u.%u\n"), HIWORD(m_pFixedFileInfo->dwFileVersionMS), LOWORD(m_pFixedFileInfo->dwFileVersionMS));
#endif // ATLTRACE2
                                DWORD* translations;
                                if (::VerQueryValue(m_lpVerInfo, _T("\\VarFileInfo\\Translation"), (LPVOID*)&translations, &uLen))
                                {
                                    size_t const numTranslations = uLen / sizeof(DWORD);
#ifdef ATLTRACE2
                                    ATLTRACE2(_T("Number of translations: %u\n"), (UINT)numTranslations);
#endif // ATLTRACE2
                                    for (size_t i = 0; i < numTranslations; i++)
                                    {
#ifdef ATLTRACE2
                                        ATLTRACE2(_T("Translation %u: %08X\n"), (UINT)i, translations[i]);
#endif // ATLTRACE2
                                        switch (LOWORD(translations[i]))
                                        {
                                        case MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL): // fall through
                                        case MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US):
                                            if (1200 == HIWORD(translations[i])) // only Unicode entries
                                            {
                                                m_useTranslation = translations[i];
                                                return;
                                            }
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                m_pFixedFileInfo = NULL;
                            }
                        }
                    }
                }
            }
        }
    }

    virtual ~CVersionInfo()
    {
        ::LocalFree(m_lpVerInfo);
        m_lpVerInfo = NULL;
    }

    LPCTSTR operator[](LPCTSTR lpszKey) const
    {
        if (!m_lpVerInfo || !lpszKey)
        {
            return NULL;
        }
        size_t const addend = MAX_PATH;
        if(_tcslen(lpszKey) >= addend)
        {
            return NULL;
        }
        TCHAR const fmtstr[] = _T("\\StringFileInfo\\%04X%04X\\%s");
        size_t const fmtbuflen = sizeof(fmtstr)/sizeof(fmtstr[0]) + addend;
        TCHAR fullName[fmtbuflen] = {0};
        _stprintf_s(fullName, fmtbuflen, fmtstr, LOWORD(m_useTranslation), HIWORD(m_useTranslation), lpszKey);
        fullName[fmtbuflen-1] = 0;

#ifdef ATLTRACE2
        ATLTRACE2(_T("Full name: %s\n"), fullName);
#endif // ATLTRACE2
        UINT uLen = 0;
        LPTSTR lpszBuf = NULL;
        if (::VerQueryValue(m_lpVerInfo, fullName, (LPVOID*)&lpszBuf, &uLen))
        {
#ifdef ATLTRACE2
            ATLTRACE2(_T("Value: %s\n"), lpszBuf);
#endif // ATLTRACE2
            return lpszBuf;
        }
#ifdef ATLTRACE2
        ATLTRACE2(_T("Value: NULL\n"));
#endif // ATLTRACE2
        return NULL;
    }
};

#endif // __VERSIONINFO_H_VER__