Skip to content

Commit

Permalink
Add Visual Studio detection by the COM API
Browse files Browse the repository at this point in the history
The new COM API is expected to work on Visual Studio 2017 and newer
installations. It is also compatible with non-x86 runtime.
  • Loading branch information
akihikodaki committed Sep 27, 2018
1 parent 0198534 commit 5e1632e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*.user
/src/.vs
/src/*.opensdf
/src/packages
10 changes: 10 additions & 0 deletions src/cv2pdb.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,18 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
</MASM>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.targets" />
<Import Project="packages\Microsoft.VisualStudio.Setup.Configuration.Native.1.16.30\build\native\Microsoft.VisualStudio.Setup.Configuration.Native.targets" Condition="Exists('packages\Microsoft.VisualStudio.Setup.Configuration.Native.1.16.30\build\native\Microsoft.VisualStudio.Setup.Configuration.Native.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('packages\Microsoft.VisualStudio.Setup.Configuration.Native.1.16.30\build\native\Microsoft.VisualStudio.Setup.Configuration.Native.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Microsoft.VisualStudio.Setup.Configuration.Native.1.16.30\build\native\Microsoft.VisualStudio.Setup.Configuration.Native.targets'))" />
</Target>
</Project>
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ int T_main(int argc, TCHAR* argv[])
const TCHAR* pdbref = 0;
bool debug = false;

CoInitialize(nullptr);

while (argc > 1 && argv[1][0] == '-')
{
argv++;
Expand Down
38 changes: 38 additions & 0 deletions src/mspdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@

#include "mspdb.h"

#include <atlstr.h>
#include <comdef.h>
#include <windows.h>
#include <Setup.Configuration.h>

_COM_SMARTPTR_TYPEDEF(ISetupConfiguration, __uuidof(ISetupConfiguration));
_COM_SMARTPTR_TYPEDEF(ISetupInstance, __uuidof(ISetupInstance));
_COM_SMARTPTR_TYPEDEF(IEnumSetupInstances, __uuidof(IEnumSetupInstances));

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

Expand Down Expand Up @@ -81,6 +88,35 @@ bool tryLoadMsPdb(const char* version, const char* mspdb, const char* path = 0)
return modMsPdb != 0;
}

bool tryLoadMsPdbCom(const char* mspdb, const char* path = 0)
{
ISetupConfigurationPtr query;
ISetupInstancePtr instance;
IEnumSetupInstancesPtr instances;
BSTR installDir;
unsigned long fetched;

auto result = query.CreateInstance(__uuidof(SetupConfiguration));
if ((FAILED(result) && result != REGDB_E_CLASSNOTREG) || FAILED(query->EnumInstances(&instances)))
return false;

while (!modMsPdb)
{
if (FAILED(instances->Next(1, &instance, &fetched)) || !fetched)
return false;

if (FAILED(instance->GetInstallationPath(&installDir)))
continue;

CStringA modpath = installDir;
modpath += "\\Common7\\IDE\\";
modpath += mspdb;
tryLoadLibrary(modpath);
}

return true;
}

bool tryLoadMsPdbVS2017(const char* mspdb, const char* path = 0)
{
const char* key = "SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VS7";
Expand Down Expand Up @@ -177,6 +213,8 @@ void tryLoadMsPdb140(bool throughPath)
{
if(throughPath)
modMsPdb = LoadLibraryA(mspdb140_dll);
if(!modMsPdb && !throughPath)
tryLoadMsPdbCom(mspdb140_dll);
if(!modMsPdb && !throughPath)
tryLoadMsPdbVS2017(mspdb140_dll);
if (!modMsPdb && !throughPath)
Expand Down
4 changes: 4 additions & 0 deletions src/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.VisualStudio.Setup.Configuration.Native" version="1.16.30" targetFramework="native" developmentDependency="true" />
</packages>

0 comments on commit 5e1632e

Please sign in to comment.