~ocsinventory-dev/ocsinventory-deploy-tool/trunk

30 by dliroulet
Add ExecCommand and FileVersion classes
1
// FileVersion.cpp: implementation of the CFileVersion class.
2
//
3
// This code comes from www.codeguru.com
4
// by Manuel Laflamme (mailto:mlaflamm@total.net)
5
//////////////////////////////////////////////////////////////////////
6
7
#include "stdafx.h"
8
9
#include "FileVersion.h"
10
11
#pragma comment(lib, "version")
12
13
#ifdef _DEBUG
14
#undef THIS_FILE
15
static char THIS_FILE[]=__FILE__;
16
#define new DEBUG_NEW
17
#endif
18
19
//////////////////////////////////////////////////////////////////////
20
21
CFileVersion::CFileVersion() 
22
{ 
23
    m_lpVersionData = NULL;
24
    m_dwLangCharset = 0;
25
}
26
27
CFileVersion::~CFileVersion() 
28
{ 
29
    Close();
30
} 
31
32
void CFileVersion::Close()
33
{
34
    delete[] m_lpVersionData; 
35
    m_lpVersionData = NULL;
36
    m_dwLangCharset = 0;
37
}
38
39
BOOL CFileVersion::Open(LPCTSTR lpszModuleName)
40
{
41
    ASSERT(_tcslen(lpszModuleName) > 0);
42
    ASSERT(m_lpVersionData == NULL);
43
44
    // Get the version information size for allocate the buffer
45
    DWORD dwHandle;     
46
    DWORD dwDataSize = ::GetFileVersionInfoSize((LPTSTR)lpszModuleName, &dwHandle); 
47
    if ( dwDataSize == 0 ) 
48
        return FALSE;
49
50
    // Allocate buffer and retrieve version information
51
    m_lpVersionData = new BYTE[dwDataSize]; 
52
    if (!::GetFileVersionInfo((LPTSTR)lpszModuleName, dwHandle, dwDataSize, 
53
	                          (void**)m_lpVersionData) )
54
    {
55
        Close();
56
        return FALSE;
57
    }
58
59
    // Retrieve the first language and character-set identifier
60
    UINT nQuerySize;
61
    DWORD* pTransTable;
62
    if (!::VerQueryValue(m_lpVersionData, _T( "\\VarFileInfo\\Translation"),
63
                         (void **)&pTransTable, &nQuerySize) )
64
    {
65
        Close();
66
        return FALSE;
67
    }
68
69
    // Swap the words to have lang-charset in the correct format
70
    m_dwLangCharset = MAKELONG(HIWORD(pTransTable[0]), LOWORD(pTransTable[0]));
71
72
    return TRUE;
73
}
74
75
CString CFileVersion::QueryValue(LPCTSTR lpszValueName, 
76
                                 DWORD dwLangCharset /* = 0*/)
77
{
78
    // Must call Open() first
79
    ASSERT(m_lpVersionData != NULL);
80
    if ( m_lpVersionData == NULL )
81
        return (CString)_T( "");
82
83
    // If no lang-charset specified use default
84
    if ( dwLangCharset == 0 )
85
        dwLangCharset = m_dwLangCharset;
86
87
    // Query version information value
88
    UINT nQuerySize;
89
    LPVOID lpData;
90
    CString strValue, strBlockName;
91
    strBlockName.Format(_T( "\\StringFileInfo\\%08lx\\%s"), 
92
	                     dwLangCharset, lpszValueName);
93
    if ( ::VerQueryValue((void **)m_lpVersionData, strBlockName.GetBuffer(0), 
94
	                     &lpData, &nQuerySize) )
95
        strValue = (LPCTSTR)lpData;
96
97
    strBlockName.ReleaseBuffer();
98
99
    return strValue;
100
}
101
102
BOOL CFileVersion::GetFixedInfo(VS_FIXEDFILEINFO& vsffi)
103
{
104
    // Must call Open() first
105
    ASSERT(m_lpVersionData != NULL);
106
    if ( m_lpVersionData == NULL )
107
        return FALSE;
108
109
    UINT nQuerySize;
110
	VS_FIXEDFILEINFO* pVsffi;
111
    if ( ::VerQueryValue((void **)m_lpVersionData, _T( "\\"),
112
                         (void**)&pVsffi, &nQuerySize) )
113
    {
114
        vsffi = *pVsffi;
115
        return TRUE;
116
    }
117
118
    return FALSE;
119
}
120
121
CString CFileVersion::GetFixedFileVersion()
122
{
123
    CString strVersion;
124
	VS_FIXEDFILEINFO vsffi;
125
126
    if ( GetFixedInfo(vsffi) )
127
    {
128
        strVersion.Format( _T( "%u.%u.%u.%u"),HIWORD(vsffi.dwFileVersionMS),
129
            LOWORD(vsffi.dwFileVersionMS),
130
            HIWORD(vsffi.dwFileVersionLS),
131
            LOWORD(vsffi.dwFileVersionLS));
132
    }
133
    return strVersion;
134
}
135
136
CString CFileVersion::GetFixedProductVersion()
137
{
138
    CString strVersion;
139
	VS_FIXEDFILEINFO vsffi;
140
141
    if ( GetFixedInfo(vsffi) )
142
    {
143
        strVersion.Format( _T( "%u.%u.%u.%u"), HIWORD(vsffi.dwProductVersionMS),
144
            LOWORD(vsffi.dwProductVersionMS),
145
            HIWORD(vsffi.dwProductVersionLS),
146
            LOWORD(vsffi.dwProductVersionLS));
147
    }
148
    return strVersion;
149
}
150
151
CString CFileVersion::GetProductLanguage()
152
{
153
	CString strLanguage;
154
	DWORD nSize = 256;
155
	TCHAR lpData[256];
156
	DWORD nResult;
157
158
	nResult = VerLanguageName(m_dwLangCharset, lpData, nSize);
159
	if ((nResult != 0) && (nResult < nSize - 1))
160
		strLanguage = (LPCTSTR)lpData;
161
162
	return strLanguage;
163
}