get file description via its window

here's my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string GetforegroundWindowdescription()
{
	HWND winhandle;
	LPSTR fileloc;
	HANDLE filohandle;
	DWORD sizE;
	DWORD reserved;
	LPVOID dAta;
	LPVOID subdAta;
	UINT dwLength;
	unsigned long int winid;
	winhandle = GetForegroundWindow();
	GetWindowThreadProcessId(winhandle, &winid);
	filohandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, winid);
	GetFinalPathNameByHandleA(filohandle, fileloc, NULL, NULL);
	sizE = GetFileVersionInfoSizeA(fileloc, &reserved);
	GetFileVersionInfoA(makanfile, NULL, sizE, dAta);
	VerQueryValue(dAta,TEXT("\\StringFileInfo\\040904E4\\FileDescription"), &subdAta, &dwLength);
	CloseHandle(filohandle);
	
}

Visual Studio 2015
I wanted to create a function to get the description field in task manager for the foreground window and return it as a string,but I just started learning VC++ and the problem is,that there are no sample codes for what I want to do.
so can you debug this please?
I was messing with the codes for 12 hours XD
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "stdafx.h"
#include <tchar.h>

#pragma comment (lib, "version.lib")	


CFileVersionInfo::CFileVersionInfo()
{
	Reset();
}


CFileVersionInfo::~CFileVersionInfo()
{}

BOOL CFileVersionInfo::GetTranslationId(LPVOID lpData, UINT unBlockSize, WORD wLangId, DWORD &dwId, BOOL bPrimaryEnough/*= FALSE*/)
{
	LPWORD lpwData;
	for (lpwData = (LPWORD)lpData; (LPBYTE)lpwData < ((LPBYTE)lpData) + unBlockSize; lpwData += 2)
	{
		if (*lpwData == wLangId)
		{
			dwId = *((DWORD*)lpwData);
			return TRUE;
		}
	}

	if (!bPrimaryEnough)
		return FALSE;

	for (lpwData = (LPWORD)lpData; (LPBYTE)lpwData < ((LPBYTE)lpData) + unBlockSize; lpwData += 2)
	{
		if (((*lpwData) & 0x00FF) == (wLangId & 0x00FF))
		{
			dwId = *((DWORD*)lpwData);
			return TRUE;
		}
	}

	return FALSE;
}

WORD CFileVersionInfo::GetFileVersion(int nIndex) const
{
	if (nIndex == 0)
		return (WORD)(m_FileInfo.dwFileVersionLS & 0x0000FFFF);
	else if (nIndex == 1)
		return (WORD)((m_FileInfo.dwFileVersionLS & 0xFFFF0000) >> 16);
	else if (nIndex == 2)
		return (WORD)(m_FileInfo.dwFileVersionMS & 0x0000FFFF);
	else if (nIndex == 3)
		return (WORD)((m_FileInfo.dwFileVersionMS & 0xFFFF0000) >> 16);
	else
		return 0;
}


WORD CFileVersionInfo::GetProductVersion(int nIndex) const
{
	if (nIndex == 0)
		return (WORD)(m_FileInfo.dwProductVersionLS & 0x0000FFFF);
	else if (nIndex == 1)
		return (WORD)((m_FileInfo.dwProductVersionLS & 0xFFFF0000) >> 16);
	else if (nIndex == 2)
		return (WORD)(m_FileInfo.dwProductVersionMS & 0x0000FFFF);
	else if (nIndex == 3)
		return (WORD)((m_FileInfo.dwProductVersionMS & 0xFFFF0000) >> 16);
	else
		return 0;
}


DWORD CFileVersionInfo::GetFileFlagsMask() const
{
	return m_FileInfo.dwFileFlagsMask;
}


DWORD CFileVersionInfo::GetFileFlags() const
{
	return m_FileInfo.dwFileFlags;
}


DWORD CFileVersionInfo::GetFileOs() const
{
	return m_FileInfo.dwFileOS;
}


DWORD CFileVersionInfo::GetFileType() const
{
	return m_FileInfo.dwFileType;
}


DWORD CFileVersionInfo::GetFileSubtype() const
{
	return m_FileInfo.dwFileSubtype;
}

stlString CFileVersionInfo::GetCompanyName() const
{
	return m_strCompanyName;
}


stlString CFileVersionInfo::GetFileDescription() const
{
	return m_strFileDescription;
}


stlString CFileVersionInfo::GetFileVersion() const
{
	return m_strFileVersion;
}


stlString CFileVersionInfo::GetInternalName() const
{
	return m_strInternalName;
}


stlString CFileVersionInfo::GetLegalCopyright() const
{
	return m_strLegalCopyright;
}


stlString CFileVersionInfo::GetOriginalFileName() const
{
	return m_strOriginalFileName;
}


stlString CFileVersionInfo::GetProductName() const
{
	return m_strProductName;
}


stlString CFileVersionInfo::GetProductVersion() const
{
	return m_strProductVersion;
}


stlString CFileVersionInfo::GetComments() const
{
	return m_strComments;
}


stlString CFileVersionInfo::GetLegalTrademarks() const
{
	return m_strLegalTrademarks;
}


stlString CFileVersionInfo::GetPrivateBuild() const
{
	return m_strPrivateBuild;
}


stlString CFileVersionInfo::GetSpecialBuild() const
{
	return m_strSpecialBuild;
}


void CFileVersionInfo::Reset()
{
	ZeroMemory(&m_FileInfo, sizeof(m_FileInfo));
	m_strCompanyName.clear();
	m_strFileDescription.clear();
	m_strFileVersion.clear();
	m_strInternalName.clear();
	m_strLegalCopyright.clear();
	m_strOriginalFileName.clear();
	m_strProductName.clear();
	m_strProductVersion.clear();
	m_strComments.clear();
	m_strLegalTrademarks.clear();
	m_strPrivateBuild.clear();
	m_strSpecialBuild.clear();
}

BOOL CFileVersionInfo::Create(LPCTSTR lpszFileName)
{
	Reset();

	DWORD	dwHandle;
	DWORD	dwFileVersionInfoSize = GetFileVersionInfoSize((LPTSTR)lpszFileName, &dwHandle);
	if (!dwFileVersionInfoSize)
		return FALSE;

	LPVOID	lpData = (LPVOID)new BYTE[dwFileVersionInfoSize];
	if (!lpData)
		return FALSE;

	try
	{
		if (!GetFileVersionInfo((LPTSTR)lpszFileName, dwHandle, dwFileVersionInfoSize, lpData))
			throw FALSE;

		// catch default information
		LPVOID	lpInfo;
		UINT		unInfoLen;
		if (VerQueryValue(lpData, _T("\\"), &lpInfo, &unInfoLen))
		{
			//ASSERT(unInfoLen == sizeof(m_FileInfo));
			if (unInfoLen == sizeof(m_FileInfo))
				memcpy(&m_FileInfo, lpInfo, unInfoLen);
		}

		// find best matching language and codepage
		VerQueryValue(lpData, _T("\\VarFileInfo\\Translation"), &lpInfo, &unInfoLen);

		DWORD	dwLangCode = 0;
		if (!GetTranslationId(lpInfo, unInfoLen, GetUserDefaultLangID(), dwLangCode, FALSE))
		{
			if (!GetTranslationId(lpInfo, unInfoLen, GetUserDefaultLangID(), dwLangCode, TRUE))
			{
				if (!GetTranslationId(lpInfo, unInfoLen, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), dwLangCode, TRUE))
				{
					if (!GetTranslationId(lpInfo, unInfoLen, MAKELANGID(LANG_ENGLISH, SUBLANG_NEUTRAL), dwLangCode, TRUE))
						// use the first one we can get
						dwLangCode = *((DWORD*)lpInfo);
				}
			}
		}


		stlString	strSubBlock;
		TCHAR buf[1024];
		_stprintf(buf, _T("\\StringFileInfo\\%04X%04X\\"), dwLangCode & 0x0000FFFF, (dwLangCode & 0xFFFF0000) >> 16);
		strSubBlock = buf;


		// catch string table
		stlString sBuf;

		sBuf = strSubBlock;
		sBuf += _T("CompanyName");
		if (VerQueryValue(lpData, sBuf.c_str (), &lpInfo, &unInfoLen))
			m_strCompanyName = stlString((LPCTSTR)lpInfo);

		sBuf.clear();
		sBuf = strSubBlock;
		sBuf += _T("FileDescription");
		if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
			m_strFileDescription = stlString((LPCTSTR)lpInfo);


		sBuf.clear();
		sBuf = strSubBlock;
		sBuf += _T("FileVersion");
		if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
			m_strFileVersion = stlString((LPCTSTR)lpInfo);


		sBuf.clear();
		sBuf = strSubBlock;
		sBuf += _T("InternalName");
		if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
			m_strInternalName = stlString((LPCTSTR)lpInfo);


		sBuf.clear();
		sBuf = strSubBlock;
		sBuf += _T("LegalCopyright");
		if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
			m_strLegalCopyright = stlString((LPCTSTR)lpInfo);


		sBuf.clear();
		sBuf = strSubBlock;
		sBuf += _T("OriginalFileName");
		if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
			m_strOriginalFileName = stlString((LPCTSTR)lpInfo);


		sBuf.clear();
		sBuf = strSubBlock;
		sBuf += _T("ProductName");
		if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
			m_strProductName = stlString((LPCTSTR)lpInfo);


		sBuf.clear();
		sBuf = strSubBlock;
		sBuf += _T("ProductVersion");
		if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
			m_strProductVersion = stlString((LPCTSTR)lpInfo);


		sBuf.clear();
		sBuf = strSubBlock;
		sBuf += _T("Comments");
		if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
			m_strComments = stlString((LPCTSTR)lpInfo);


		sBuf.clear();
		sBuf = strSubBlock;
		sBuf += _T("LegalTrademarks");
		if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
			m_strLegalTrademarks = stlString((LPCTSTR)lpInfo);


		sBuf.clear();
		sBuf = strSubBlock;
		sBuf += _T("PrivateBuild");
		if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
			m_strPrivateBuild = stlString((LPCTSTR)lpInfo);


		sBuf.clear();
		sBuf = strSubBlock;
		sBuf += _T("SpecialBuild");
		if (VerQueryValue(lpData, sBuf.c_str(), &lpInfo, &unInfoLen))
			m_strSpecialBuild = stlString((LPCTSTR)lpInfo);

		delete[] lpData;
	}
	catch (BOOL)
	{
		delete[] lpData;
		return FALSE;
	}

	return TRUE;
}
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
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <windows.h>
#include <string>

#include <winver.h>
typedef std::basic_string <TCHAR> stlString;
class CFileVersionInfo
{
	
	// construction/destruction
public:
	CFileVersionInfo();
	virtual ~CFileVersionInfo();

	// operations
public:
	BOOL Create(HMODULE hModule = NULL);
	BOOL Create(LPCTSTR lpszFileName);

	// attribute operations
public:
	WORD GetFileVersion(int nIndex) const;
	WORD GetProductVersion(int nIndex) const;
	DWORD GetFileFlagsMask() const;
	DWORD GetFileFlags() const;
	DWORD GetFileOs() const;
	DWORD GetFileType() const;
	DWORD GetFileSubtype() const;

	stlString GetCompanyName() const;
	stlString GetFileDescription() const;
	stlString GetFileVersion() const;
	stlString GetInternalName() const;
	stlString GetLegalCopyright() const;
	stlString GetOriginalFileName() const;
	stlString GetProductName() const;
	stlString GetProductVersion() const;
	stlString GetComments() const;
	stlString GetLegalTrademarks() const;
	stlString GetPrivateBuild() const;
	stlString GetSpecialBuild() const;

	// implementation helpers
protected:
	virtual void Reset();
	BOOL GetTranslationId(LPVOID lpData, UINT unBlockSize, WORD wLangId, DWORD &dwId, BOOL bPrimaryEnough = FALSE);

	// attributes
private:
	VS_FIXEDFILEINFO m_FileInfo;

	stlString m_strCompanyName;
	stlString m_strFileDescription;
	stlString m_strFileVersion;
	stlString m_strInternalName;
	stlString m_strLegalCopyright;
	stlString m_strOriginalFileName;
	stlString m_strProductName;
	stlString m_strProductVersion;
	stlString m_strComments;
	stlString m_strLegalTrademarks;
	stlString m_strPrivateBuild;
	stlString m_strSpecialBuild;
};


Example usage:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int CALLBACK WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	HWND winhandle = GetForegroundWindow ();
	DWORD winid = 0;
	GetWindowThreadProcessId ( winhandle, &winid );

	HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION, FALSE, winid );
	
	TCHAR szExe[MAX_PATH + 1];
	DWORD lpdwSize = MAX_PATH;

	BOOL bFile = QueryFullProcessImageName ( hProcess, 0, szExe, &lpdwSize );
	CloseHandle(hProcess);

	CFileVersionInfo data;
	data.Create(szExe);
	MessageBox(NULL, data.GetFileDescription ().c_str (), szExe, MB_OK);
	return 0;
}
Last edited on
Topic archived. No new replies allowed.