Skip to content

Commit

Permalink
Print empty enumerable even if not registered
Browse files Browse the repository at this point in the history
Fixes issue #1
  • Loading branch information
heaths committed Feb 24, 2017
1 parent c2cffab commit 2ad43fa
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 52 deletions.
33 changes: 17 additions & 16 deletions src/vswhere.lib/InstanceSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,30 @@ bool InstanceSelector::Less(const ISetupInstancePtr& a, const ISetupInstancePtr&
}
}

vector<ISetupInstancePtr> InstanceSelector::Select(_In_ IEnumSetupInstances* pEnum) const
vector<ISetupInstancePtr> InstanceSelector::Select(_In_opt_ IEnumSetupInstances* pEnum) const
{
_ASSERT(pEnum);

HRESULT hr = S_OK;
unsigned long celtFetched = 0;
ISetupInstance* pInstances[1] = {};

vector<ISetupInstancePtr> instances;
do
if (pEnum)
{
celtFetched = 0;
HRESULT hr = S_OK;
unsigned long celtFetched = 0;
ISetupInstance* pInstances[1] = {};

hr = pEnum->Next(1, pInstances, &celtFetched);
if (SUCCEEDED(hr) && celtFetched)
do
{
ISetupInstancePtr instance(pInstances[0], false);
if (IsMatch(instance))
celtFetched = 0;

hr = pEnum->Next(1, pInstances, &celtFetched);
if (SUCCEEDED(hr) && celtFetched)
{
instances.push_back(instance);
ISetupInstancePtr instance(pInstances[0], false);
if (IsMatch(instance))
{
instances.push_back(instance);
}
}
}
} while (SUCCEEDED(hr) && celtFetched);
} while (SUCCEEDED(hr) && celtFetched);
}

if (m_args.get_Latest() && 1 < instances.size())
{
Expand Down
2 changes: 1 addition & 1 deletion src/vswhere.lib/InstanceSelector.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class InstanceSelector
}

bool Less(const ISetupInstancePtr& a, const ISetupInstancePtr& b) const;
std::vector<ISetupInstancePtr> Select(_In_ IEnumSetupInstances* pEnum) const;
std::vector<ISetupInstancePtr> Select(_In_opt_ IEnumSetupInstances* pEnum) const;

private:
static std::wstring GetId(_In_ ISetupPackageReference* pPackageReference);
Expand Down
80 changes: 45 additions & 35 deletions src/vswhere/Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using namespace std;

void GetEnumerator(_In_ const CommandArgs& args, _In_ ISetupConfigurationPtr& query, _In_ IEnumSetupInstancesPtr& e);
void WriteLogo(_In_ const CommandArgs& args, _In_ wostream& out);

int wmain(_In_ int argc, _In_ LPCWSTR argv[])
Expand All @@ -30,44 +31,14 @@ int wmain(_In_ int argc, _In_ LPCWSTR argv[])
ISetupConfigurationPtr query;
IEnumSetupInstancesPtr e;

auto hr = query.CreateInstance(__uuidof(SetupConfiguration));
if (FAILED(hr))
{
if (REGDB_E_CLASSNOTREG == hr)
{
WriteLogo(args, out);
return ERROR_SUCCESS;
}
}

// If all instances are requested, try to get the proper enumerator; otherwise, fall back to original enumerator.
if (args.get_All())
{
ISetupConfiguration2Ptr query2;

hr = query->QueryInterface(&query2);
if (SUCCEEDED(hr))
{
hr = query2->EnumAllInstances(&e);
if (FAILED(hr))
{
throw win32_error(hr);
}
}
}

if (!e)
{
hr = query->EnumInstances(&e);
if (FAILED(hr))
{
throw win32_error(hr);
}
}
GetEnumerator(args, query, e);

// Attempt to get the ISetupHelper.
ISetupHelperPtr helper;
query->QueryInterface(&helper);
if (query)
{
query->QueryInterface(&helper);
}

InstanceSelector selector(args, helper);
auto instances = selector.Select(e);
Expand Down Expand Up @@ -99,6 +70,45 @@ int wmain(_In_ int argc, _In_ LPCWSTR argv[])
return E_FAIL;
}

void GetEnumerator(_In_ const CommandArgs& args, _In_ ISetupConfigurationPtr& query, _In_ IEnumSetupInstancesPtr& e)
{
auto hr = query.CreateInstance(__uuidof(SetupConfiguration));
if (FAILED(hr))
{
if (REGDB_E_CLASSNOTREG == hr)
{
return;
}

throw win32_error(hr);
}

// If all instances are requested, try to get the proper enumerator; otherwise, fall back to original enumerator.
if (args.get_All())
{
ISetupConfiguration2Ptr query2;

hr = query->QueryInterface(&query2);
if (SUCCEEDED(hr))
{
hr = query2->EnumAllInstances(&e);
if (FAILED(hr))
{
throw win32_error(hr);
}
}
}

if (!e)
{
hr = query->EnumInstances(&e);
if (FAILED(hr))
{
throw win32_error(hr);
}
}
}

void WriteLogo(_In_ const CommandArgs& args, _In_ wostream& out)
{
if (args.get_Logo())
Expand Down
11 changes: 11 additions & 0 deletions test/vswhere.test/InstanceSelectorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework;
TEST_CLASS(InstanceSelectorTests)
{
public:
TEST_METHOD(Select_Null)
{
CommandArgs args;
args.Parse(L"vswhere.exe");

InstanceSelector sut(args);
auto selected = sut.Select(NULL);

Assert::AreEqual<size_t>(0, selected.size());
}

TEST_METHOD(Select_No_Product)
{
TestInstance instance =
Expand Down

0 comments on commit 2ad43fa

Please sign in to comment.