Skip to content

Commit

Permalink
Improve Visual Studio detection
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.
  • Loading branch information
akihikodaki committed Sep 26, 2018
1 parent 0198534 commit 553c9bb
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 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
42 changes: 28 additions & 14 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 @@ -83,24 +90,31 @@ bool tryLoadMsPdb(const char* version, const char* mspdb, const char* path = 0)

bool tryLoadMsPdbVS2017(const char* mspdb, const char* path = 0)
{
const char* key = "SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VS7";

HKEY hkey;
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, key, 0, KEY_OPEN_FLAGS, &hkey) != ERROR_SUCCESS)
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;

char installDir[260];
DWORD size = sizeof(installDir);
bool rc = RegQueryValueExA(hkey, "15.0", 0, 0, (LPBYTE)installDir, &size) == ERROR_SUCCESS;
RegCloseKey(hkey);
if(!rc)
return false;
while (!modMsPdb)
{
if (FAILED(instances->Next(1, &instance, &fetched)) || !fetched)
return false;

strncat(installDir, "Common7\\IDE\\", 260);
strncat(installDir, mspdb, 260);
if (FAILED(instance->GetInstallationPath(&installDir)))
continue;

tryLoadLibrary(installDir);
return modMsPdb != 0;
CStringA modpath = installDir;
modpath += "\\Common7\\IDE\\";
modpath += mspdb;
tryLoadLibrary(modpath);
}

return true;
}

#ifdef _M_X64
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 553c9bb

Please sign in to comment.