Skip to content

Commit 8619e96

Browse files
authored
Merge pull request #54 from Microsoft/develop
Print query API version in logo
2 parents 4f70bea + 2db2a26 commit 8619e96

File tree

8 files changed

+151
-6
lines changed

8 files changed

+151
-6
lines changed

src/vswhere.lib/resource.h

90 Bytes
Binary file not shown.

src/vswhere.lib/vswhere.lib.rc

176 Bytes
Binary file not shown.

src/vswhere/Module.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// <copyright file="Module.cpp" company="Microsoft Corporation">
2+
// Copyright (C) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE.txt in the project root for license information.
4+
// </copyright>
5+
6+
#include "stdafx.h"
7+
8+
using namespace std;
9+
10+
void Module::FromIUnknown(_In_ const IUnknown* pUnk) noexcept
11+
{
12+
typedef struct IUnknownVtbl
13+
{
14+
HRESULT(STDMETHODCALLTYPE *QueryInterface)(ISetupConfiguration*, REFIID, LPVOID*);
15+
ULONG(STDMETHODCALLTYPE *AddRef)(ISetupConfiguration*);
16+
ULONG(STDMETHODCALLTYPE *Release)(ISetupConfiguration*);
17+
} IUnknownVtbl;
18+
19+
auto pVtbl = reinterpret_cast<const IUnknownVtbl*>(pUnk);
20+
::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<LPCWSTR>(pVtbl->QueryInterface), &m_hModule);
21+
}
22+
23+
const wstring& Module::get_Path() noexcept
24+
{
25+
if (m_hModule && m_path.empty())
26+
{
27+
m_path.resize(MAX_PATH);
28+
::GetModuleFileNameW(m_hModule, const_cast<LPWSTR>(m_path.data()), m_path.size());
29+
}
30+
31+
return m_path;
32+
}
33+
34+
const wstring& Module::get_FileVersion() noexcept
35+
{
36+
auto path = get_Path();
37+
if (path.empty())
38+
{
39+
return m_fileVersion;
40+
}
41+
42+
DWORD dwHandle = 0;
43+
DWORD cbVersionInfo = ::GetFileVersionInfoSizeW(path.c_str(), &dwHandle);
44+
if (!cbVersionInfo)
45+
{
46+
return m_fileVersion;
47+
}
48+
49+
vector<byte> buffer(cbVersionInfo);
50+
if (!::GetFileVersionInfoW(path.c_str(), dwHandle, buffer.size(), buffer.data()))
51+
{
52+
return m_fileVersion;
53+
}
54+
55+
VS_FIXEDFILEINFO* pFileInfo = NULL;
56+
UINT cbFileInfo = 0;
57+
if (!::VerQueryValueW(buffer.data(), L"\\", reinterpret_cast<LPVOID*>(&pFileInfo), &cbFileInfo))
58+
{
59+
return m_fileVersion;
60+
}
61+
62+
auto cch = _scwprintf(
63+
L"%d.%d.%d.%d",
64+
(pFileInfo->dwFileVersionMS >> 16) & 0xffff,
65+
pFileInfo->dwFileVersionMS & 0xffff,
66+
(pFileInfo->dwFileVersionLS >> 16) & 0xffff,
67+
pFileInfo->dwFileVersionLS & 0xffff);
68+
if (!cch)
69+
{
70+
return m_fileVersion;
71+
}
72+
73+
m_fileVersion.resize(++cch);
74+
swprintf_s(
75+
const_cast<LPWSTR>(m_fileVersion.c_str()),
76+
m_fileVersion.size(),
77+
L"%d.%d.%d.%d",
78+
(pFileInfo->dwFileVersionMS >> 16) & 0xffff,
79+
pFileInfo->dwFileVersionMS & 0xffff,
80+
(pFileInfo->dwFileVersionLS >> 16) & 0xffff,
81+
pFileInfo->dwFileVersionLS & 0xffff);
82+
83+
// Trim terminating null character.
84+
m_fileVersion.resize(--cch);
85+
86+
return m_fileVersion;
87+
}

src/vswhere/Module.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// <copyright file="Module.h" company="Microsoft Corporation">
2+
// Copyright (C) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE.txt in the project root for license information.
4+
// </copyright>
5+
6+
#pragma once
7+
8+
class Module
9+
{
10+
public:
11+
Module() :
12+
m_hModule(NULL)
13+
{
14+
}
15+
16+
Module(_In_ const Module& obj) :
17+
m_hModule(obj.m_hModule),
18+
m_path(obj.m_path),
19+
m_fileVersion(obj.m_fileVersion)
20+
{
21+
}
22+
23+
~Module()
24+
{
25+
if (m_hModule)
26+
{
27+
::FreeLibrary(m_hModule);
28+
}
29+
}
30+
31+
// Avoid throwing non-catastrphic exceptions since information is for diagnostics only.
32+
void FromIUnknown(_In_ const IUnknown* pUnk) noexcept;
33+
const std::wstring& get_Path() noexcept;
34+
const std::wstring& get_FileVersion() noexcept;
35+
36+
private:
37+
HMODULE m_hModule;
38+
std::wstring m_path;
39+
std::wstring m_fileVersion;
40+
};

src/vswhere/Program.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
using namespace std;
99

1010
void GetEnumerator(_In_ const CommandArgs& args, _In_ ISetupConfigurationPtr& query, _In_ IEnumSetupInstancesPtr& e);
11-
void WriteLogo(_In_ const CommandArgs& args, _In_ Console& console);
11+
void WriteLogo(_In_ const CommandArgs& args, _In_ Console& console, _In_ Module& module);
1212

1313
int wmain(_In_ int argc, _In_ LPCWSTR argv[])
1414
{
1515
CommandArgs args;
1616
Console console(args);
17+
Module queryModule;
1718

1819
console.Initialize();
1920
try
@@ -23,7 +24,7 @@ int wmain(_In_ int argc, _In_ LPCWSTR argv[])
2324
args.Parse(argc, argv);
2425
if (args.get_Help())
2526
{
26-
WriteLogo(args, console);
27+
WriteLogo(args, console, queryModule);
2728
args.Usage(console);
2829

2930
return ERROR_SUCCESS;
@@ -34,6 +35,9 @@ int wmain(_In_ int argc, _In_ LPCWSTR argv[])
3435

3536
GetEnumerator(args, query, e);
3637

38+
// Try to get information about the query module for later.
39+
queryModule.FromIUnknown(static_cast<IUnknown*>(query));
40+
3741
// Attempt to get the ISetupHelper.
3842
ISetupHelperPtr helper;
3943
if (query)
@@ -48,7 +52,7 @@ int wmain(_In_ int argc, _In_ LPCWSTR argv[])
4852
auto formatter = Formatter::Create(args.get_Format());
4953
if (formatter->ShowLogo())
5054
{
51-
WriteLogo(args, console);
55+
WriteLogo(args, console, queryModule);
5256
}
5357

5458
formatter->Write(args, console, instances);
@@ -59,7 +63,7 @@ int wmain(_In_ int argc, _In_ LPCWSTR argv[])
5963
const auto code = ex.code().value();
6064
if (ERROR_INVALID_PARAMETER == code)
6165
{
62-
WriteLogo(args, console);
66+
WriteLogo(args, console, queryModule);
6367
}
6468

6569
console.Write(L"%ls 0x%x: ", ResourceManager::GetString(IDS_ERROR).c_str(), code);
@@ -127,11 +131,14 @@ void GetEnumerator(_In_ const CommandArgs& args, _In_ ISetupConfigurationPtr& qu
127131
}
128132
}
129133

130-
void WriteLogo(_In_ const CommandArgs& args, _In_ Console& console)
134+
void WriteLogo(_In_ const CommandArgs& args, _In_ Console& console, _In_ Module& module)
131135
{
132136
if (args.get_Logo())
133137
{
134-
console.WriteLine(ResourceManager::FormatString(IDS_PROGRAMINFO, FILE_VERSION_STRINGW));
138+
const auto version = module.get_FileVersion();
139+
const auto nID = version.empty() ? IDS_PROGRAMINFO : IDS_PROGRAMINFOEX;
140+
141+
console.WriteLine(ResourceManager::FormatString(nID, FILE_VERSION_STRINGW, version.c_str()));
135142
console.WriteLine(ResourceManager::GetString(IDS_COPYRIGHT));
136143
console.WriteLine();
137144
}

src/vswhere/stdafx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// Project headers
1919
#include <stdafx.h>
2020
#include <VersionInfo.h>
21+
#include "Module.h"
2122

2223
_COM_SMARTPTR_TYPEDEF(ISetupConfiguration, __uuidof(ISetupConfiguration));
2324
_COM_SMARTPTR_TYPEDEF(ISetupConfiguration2, __uuidof(ISetupConfiguration2));

src/vswhere/vswhere.vcxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<Link>
6464
<SubSystem>Console</SubSystem>
6565
<GenerateDebugInformation>true</GenerateDebugInformation>
66+
<AdditionalDependencies>version.lib;%(AdditionalDependencies)</AdditionalDependencies>
6667
</Link>
6768
</ItemDefinitionGroup>
6869
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -81,14 +82,17 @@
8182
<EnableCOMDATFolding>true</EnableCOMDATFolding>
8283
<OptimizeReferences>true</OptimizeReferences>
8384
<GenerateDebugInformation>true</GenerateDebugInformation>
85+
<AdditionalDependencies>version.lib;%(AdditionalDependencies)</AdditionalDependencies>
8486
</Link>
8587
</ItemDefinitionGroup>
8688
<ItemGroup>
89+
<ClInclude Include="Module.h" />
8790
<ClInclude Include="resource.h" />
8891
<ClInclude Include="stdafx.h" />
8992
<ClInclude Include="targetver.h" />
9093
</ItemGroup>
9194
<ItemGroup>
95+
<ClCompile Include="Module.cpp" />
9296
<ClCompile Include="Program.cpp" />
9397
<ClCompile Include="stdafx.cpp">
9498
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>

src/vswhere/vswhere.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<ClInclude Include="resource.h">
2525
<Filter>Header Files</Filter>
2626
</ClInclude>
27+
<ClInclude Include="Module.h">
28+
<Filter>Header Files</Filter>
29+
</ClInclude>
2730
</ItemGroup>
2831
<ItemGroup>
2932
<ClCompile Include="stdafx.cpp">
@@ -32,6 +35,9 @@
3235
<ClCompile Include="Program.cpp">
3336
<Filter>Source Files</Filter>
3437
</ClCompile>
38+
<ClCompile Include="Module.cpp">
39+
<Filter>Source Files</Filter>
40+
</ClCompile>
3541
</ItemGroup>
3642
<ItemGroup>
3743
<None Include="packages.config" />

0 commit comments

Comments
 (0)