2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2007-11-16 15:01:58 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2007-11-16 15:01:58 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2007-11-16 15:01:58 +03:00
|
|
|
#include "cmCallVisualStudioMacro.h"
|
2016-04-29 16:40:20 +03:00
|
|
|
|
2007-11-16 15:01:58 +03:00
|
|
|
#include "cmSystemTools.h"
|
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#define HAVE_COMDEF_H
|
|
|
|
#endif
|
|
|
|
|
2008-07-30 23:26:34 +04:00
|
|
|
// Just for this file:
|
|
|
|
//
|
|
|
|
static bool LogErrorsAsMessages;
|
|
|
|
|
2007-11-16 15:01:58 +03:00
|
|
|
#if defined(HAVE_COMDEF_H)
|
|
|
|
|
|
|
|
#include <comdef.h>
|
|
|
|
|
2008-10-08 22:19:01 +04:00
|
|
|
// Copied from a correct comdef.h to avoid problems with deficient versions
|
|
|
|
// of comdef.h that exist in the wild... Fixes issue #7533.
|
|
|
|
//
|
|
|
|
#ifdef _NATIVE_WCHAR_T_DEFINED
|
2016-05-16 17:34:04 +03:00
|
|
|
#ifdef _DEBUG
|
|
|
|
#pragma comment(lib, "comsuppwd.lib")
|
2008-10-08 22:19:01 +04:00
|
|
|
#else
|
2016-05-16 17:34:04 +03:00
|
|
|
#pragma comment(lib, "comsuppw.lib")
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#ifdef _DEBUG
|
|
|
|
#pragma comment(lib, "comsuppd.lib")
|
|
|
|
#else
|
|
|
|
#pragma comment(lib, "comsupp.lib")
|
|
|
|
#endif
|
2008-10-08 22:19:01 +04:00
|
|
|
#endif
|
|
|
|
|
2008-07-30 23:26:34 +04:00
|
|
|
///! Use ReportHRESULT to make a cmSystemTools::Message after calling
|
2007-11-16 15:01:58 +03:00
|
|
|
///! a COM method that may have failed.
|
2016-05-16 17:34:04 +03:00
|
|
|
#define ReportHRESULT(hr, context) \
|
|
|
|
if (FAILED(hr)) { \
|
|
|
|
if (LogErrorsAsMessages) { \
|
|
|
|
std::ostringstream _hresult_oss; \
|
|
|
|
_hresult_oss.flags(std::ios::hex); \
|
|
|
|
_hresult_oss << context << " failed HRESULT, hr = 0x" << hr \
|
|
|
|
<< std::endl; \
|
|
|
|
_hresult_oss.flags(std::ios::dec); \
|
|
|
|
_hresult_oss << __FILE__ << "(" << __LINE__ << ")"; \
|
|
|
|
cmSystemTools::Message(_hresult_oss.str().c_str()); \
|
|
|
|
} \
|
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
///! Using the given instance of Visual Studio, call the named macro
|
2016-05-16 17:34:04 +03:00
|
|
|
HRESULT InstanceCallMacro(IDispatch* vsIDE, const std::string& macro,
|
|
|
|
const std::string& args)
|
2007-11-16 15:01:58 +03:00
|
|
|
{
|
|
|
|
HRESULT hr = E_POINTER;
|
|
|
|
|
|
|
|
_bstr_t macroName(macro.c_str());
|
|
|
|
_bstr_t macroArgs(args.c_str());
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (0 != vsIDE) {
|
|
|
|
DISPID dispid = (DISPID)-1;
|
|
|
|
OLECHAR* name = L"ExecuteCommand";
|
2007-11-16 15:01:58 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
hr =
|
|
|
|
vsIDE->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &dispid);
|
2007-11-16 15:01:58 +03:00
|
|
|
ReportHRESULT(hr, "GetIDsOfNames(ExecuteCommand)");
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (SUCCEEDED(hr)) {
|
2007-11-16 15:01:58 +03:00
|
|
|
VARIANTARG vargs[2];
|
|
|
|
DISPPARAMS params;
|
|
|
|
VARIANT result;
|
|
|
|
EXCEPINFO excep;
|
2016-05-16 17:34:04 +03:00
|
|
|
UINT arg = (UINT)-1;
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
// No VariantInit or VariantClear calls are necessary for
|
|
|
|
// these two vargs. They are both local _bstr_t variables
|
|
|
|
// that remain in scope for the duration of the Invoke call.
|
|
|
|
//
|
|
|
|
V_VT(&vargs[1]) = VT_BSTR;
|
|
|
|
V_BSTR(&vargs[1]) = macroName;
|
|
|
|
V_VT(&vargs[0]) = VT_BSTR;
|
|
|
|
V_BSTR(&vargs[0]) = macroArgs;
|
|
|
|
|
|
|
|
params.rgvarg = &vargs[0];
|
|
|
|
params.rgdispidNamedArgs = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
params.cArgs = sizeof(vargs) / sizeof(vargs[0]);
|
2007-11-16 15:01:58 +03:00
|
|
|
params.cNamedArgs = 0;
|
|
|
|
|
|
|
|
VariantInit(&result);
|
|
|
|
|
|
|
|
memset(&excep, 0, sizeof(excep));
|
|
|
|
|
|
|
|
hr = vsIDE->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT,
|
2016-05-16 17:34:04 +03:00
|
|
|
DISPATCH_METHOD, ¶ms, &result, &excep, &arg);
|
2008-02-15 19:49:58 +03:00
|
|
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << std::endl;
|
|
|
|
oss << "Invoke(ExecuteCommand)" << std::endl;
|
|
|
|
oss << " Macro: " << macro.c_str() << std::endl;
|
|
|
|
oss << " Args: " << args.c_str() << std::endl;
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (DISP_E_EXCEPTION == hr) {
|
2008-02-15 19:49:58 +03:00
|
|
|
oss << "DISP_E_EXCEPTION EXCEPINFO:" << excep.wCode << std::endl;
|
|
|
|
oss << " wCode: " << excep.wCode << std::endl;
|
|
|
|
oss << " wReserved: " << excep.wReserved << std::endl;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (excep.bstrSource) {
|
|
|
|
oss << " bstrSource: " << (const char*)(_bstr_t)excep.bstrSource
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
if (excep.bstrDescription) {
|
|
|
|
oss << " bstrDescription: "
|
|
|
|
<< (const char*)(_bstr_t)excep.bstrDescription << std::endl;
|
|
|
|
}
|
|
|
|
if (excep.bstrHelpFile) {
|
|
|
|
oss << " bstrHelpFile: " << (const char*)(_bstr_t)excep.bstrHelpFile
|
|
|
|
<< std::endl;
|
|
|
|
}
|
2008-02-15 19:49:58 +03:00
|
|
|
oss << " dwHelpContext: " << excep.dwHelpContext << std::endl;
|
|
|
|
oss << " pvReserved: " << excep.pvReserved << std::endl;
|
|
|
|
oss << " pfnDeferredFillIn: " << excep.pfnDeferredFillIn << std::endl;
|
|
|
|
oss << " scode: " << excep.scode << std::endl;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-15 19:49:58 +03:00
|
|
|
|
|
|
|
std::string exstr(oss.str());
|
|
|
|
ReportHRESULT(hr, exstr.c_str());
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
VariantClear(&result);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
///! Get the Solution object from the IDE object
|
2016-05-16 17:34:04 +03:00
|
|
|
HRESULT GetSolutionObject(IDispatch* vsIDE, IDispatchPtr& vsSolution)
|
2007-11-16 15:01:58 +03:00
|
|
|
{
|
|
|
|
HRESULT hr = E_POINTER;
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (0 != vsIDE) {
|
|
|
|
DISPID dispid = (DISPID)-1;
|
|
|
|
OLECHAR* name = L"Solution";
|
2007-11-16 15:01:58 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
hr =
|
|
|
|
vsIDE->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &dispid);
|
2007-11-16 15:01:58 +03:00
|
|
|
ReportHRESULT(hr, "GetIDsOfNames(Solution)");
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (SUCCEEDED(hr)) {
|
2007-11-16 15:01:58 +03:00
|
|
|
DISPPARAMS params;
|
|
|
|
VARIANT result;
|
|
|
|
EXCEPINFO excep;
|
2016-05-16 17:34:04 +03:00
|
|
|
UINT arg = (UINT)-1;
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
params.rgvarg = 0;
|
|
|
|
params.rgdispidNamedArgs = 0;
|
|
|
|
params.cArgs = 0;
|
|
|
|
params.cNamedArgs = 0;
|
|
|
|
|
|
|
|
VariantInit(&result);
|
|
|
|
|
|
|
|
memset(&excep, 0, sizeof(excep));
|
|
|
|
|
|
|
|
hr = vsIDE->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT,
|
2016-05-16 17:34:04 +03:00
|
|
|
DISPATCH_PROPERTYGET, ¶ms, &result, &excep, &arg);
|
2007-11-16 15:01:58 +03:00
|
|
|
ReportHRESULT(hr, "Invoke(Solution)");
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (SUCCEEDED(hr)) {
|
2007-11-16 15:01:58 +03:00
|
|
|
vsSolution = V_DISPATCH(&result);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
VariantClear(&result);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
///! Get the FullName property from the Solution object
|
2016-05-16 17:34:04 +03:00
|
|
|
HRESULT GetSolutionFullName(IDispatch* vsSolution, std::string& fullName)
|
2007-11-16 15:01:58 +03:00
|
|
|
{
|
|
|
|
HRESULT hr = E_POINTER;
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (0 != vsSolution) {
|
|
|
|
DISPID dispid = (DISPID)-1;
|
|
|
|
OLECHAR* name = L"FullName";
|
2007-11-16 15:01:58 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
hr = vsSolution->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT,
|
|
|
|
&dispid);
|
2007-11-16 15:01:58 +03:00
|
|
|
ReportHRESULT(hr, "GetIDsOfNames(FullName)");
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (SUCCEEDED(hr)) {
|
2007-11-16 15:01:58 +03:00
|
|
|
DISPPARAMS params;
|
|
|
|
VARIANT result;
|
|
|
|
EXCEPINFO excep;
|
2016-05-16 17:34:04 +03:00
|
|
|
UINT arg = (UINT)-1;
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
params.rgvarg = 0;
|
|
|
|
params.rgdispidNamedArgs = 0;
|
|
|
|
params.cArgs = 0;
|
|
|
|
params.cNamedArgs = 0;
|
|
|
|
|
|
|
|
VariantInit(&result);
|
|
|
|
|
|
|
|
memset(&excep, 0, sizeof(excep));
|
|
|
|
|
|
|
|
hr = vsSolution->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT,
|
2016-05-16 17:34:04 +03:00
|
|
|
DISPATCH_PROPERTYGET, ¶ms, &result, &excep,
|
|
|
|
&arg);
|
2007-11-16 15:01:58 +03:00
|
|
|
ReportHRESULT(hr, "Invoke(FullName)");
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
fullName = (std::string)(_bstr_t)V_BSTR(&result);
|
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
VariantClear(&result);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
///! Get the FullName property from the Solution object, given the IDE object
|
2016-05-16 17:34:04 +03:00
|
|
|
HRESULT GetIDESolutionFullName(IDispatch* vsIDE, std::string& fullName)
|
2007-11-16 15:01:58 +03:00
|
|
|
{
|
|
|
|
IDispatchPtr vsSolution;
|
|
|
|
HRESULT hr = GetSolutionObject(vsIDE, vsSolution);
|
|
|
|
ReportHRESULT(hr, "GetSolutionObject");
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (SUCCEEDED(hr)) {
|
2007-11-16 15:01:58 +03:00
|
|
|
GetSolutionFullName(vsSolution, fullName);
|
|
|
|
ReportHRESULT(hr, "GetSolutionFullName");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
///! Get all running objects from the Windows running object table.
|
|
|
|
///! Save them in a map by their display names.
|
|
|
|
HRESULT GetRunningInstances(std::map<std::string, IUnknownPtr>& mrot)
|
|
|
|
{
|
|
|
|
// mrot == Map of the Running Object Table
|
|
|
|
|
|
|
|
IRunningObjectTablePtr runningObjectTable;
|
|
|
|
IEnumMonikerPtr monikerEnumerator;
|
|
|
|
IMonikerPtr moniker;
|
|
|
|
ULONG numFetched = 0;
|
|
|
|
|
|
|
|
HRESULT hr = GetRunningObjectTable(0, &runningObjectTable);
|
|
|
|
ReportHRESULT(hr, "GetRunningObjectTable");
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (SUCCEEDED(hr)) {
|
2007-11-16 15:01:58 +03:00
|
|
|
hr = runningObjectTable->EnumRunning(&monikerEnumerator);
|
|
|
|
ReportHRESULT(hr, "EnumRunning");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (SUCCEEDED(hr)) {
|
2007-11-16 15:01:58 +03:00
|
|
|
hr = monikerEnumerator->Reset();
|
|
|
|
ReportHRESULT(hr, "Reset");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
while (S_OK == monikerEnumerator->Next(1, &moniker, &numFetched)) {
|
2007-11-16 15:01:58 +03:00
|
|
|
std::string runningObjectName;
|
|
|
|
IUnknownPtr runningObjectVal;
|
|
|
|
IBindCtxPtr ctx;
|
|
|
|
|
|
|
|
hr = CreateBindCtx(0, &ctx);
|
|
|
|
ReportHRESULT(hr, "CreateBindCtx");
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (SUCCEEDED(hr)) {
|
2007-11-16 15:01:58 +03:00
|
|
|
LPOLESTR displayName = 0;
|
|
|
|
hr = moniker->GetDisplayName(ctx, 0, &displayName);
|
|
|
|
ReportHRESULT(hr, "GetDisplayName");
|
2016-05-16 17:34:04 +03:00
|
|
|
if (displayName) {
|
|
|
|
runningObjectName = (std::string)(_bstr_t)displayName;
|
2007-11-16 15:01:58 +03:00
|
|
|
CoTaskMemFree(displayName);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
hr = runningObjectTable->GetObject(moniker, &runningObjectVal);
|
|
|
|
ReportHRESULT(hr, "GetObject");
|
2016-05-16 17:34:04 +03:00
|
|
|
if (SUCCEEDED(hr)) {
|
2007-11-16 15:01:58 +03:00
|
|
|
mrot.insert(std::make_pair(runningObjectName, runningObjectVal));
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
numFetched = 0;
|
|
|
|
moniker = 0;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
///! Do the two file names refer to the same Visual Studio solution? Or are
|
|
|
|
///! we perhaps looking for any and all solutions?
|
2016-05-16 17:34:04 +03:00
|
|
|
bool FilesSameSolution(const std::string& slnFile, const std::string& slnName)
|
2007-11-16 15:01:58 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (slnFile == "ALL" || slnName == "ALL") {
|
2007-11-16 15:01:58 +03:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
// Otherwise, make lowercase local copies, convert to Unix slashes, and
|
|
|
|
// see if the resulting strings are the same:
|
|
|
|
std::string s1 = cmSystemTools::LowerCase(slnFile);
|
|
|
|
std::string s2 = cmSystemTools::LowerCase(slnName);
|
|
|
|
cmSystemTools::ConvertToUnixSlashes(s1);
|
|
|
|
cmSystemTools::ConvertToUnixSlashes(s2);
|
|
|
|
|
|
|
|
return s1 == s2;
|
|
|
|
}
|
|
|
|
|
|
|
|
///! Find instances of Visual Studio with the given solution file
|
|
|
|
///! open. Pass "ALL" for slnFile to gather all running instances
|
|
|
|
///! of Visual Studio.
|
2016-05-16 17:34:04 +03:00
|
|
|
HRESULT FindVisualStudioInstances(const std::string& slnFile,
|
|
|
|
std::vector<IDispatchPtr>& instances)
|
2007-11-16 15:01:58 +03:00
|
|
|
{
|
|
|
|
std::map<std::string, IUnknownPtr> mrot;
|
|
|
|
|
|
|
|
HRESULT hr = GetRunningInstances(mrot);
|
|
|
|
ReportHRESULT(hr, "GetRunningInstances");
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (SUCCEEDED(hr)) {
|
2007-11-16 15:01:58 +03:00
|
|
|
std::map<std::string, IUnknownPtr>::iterator it;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (it = mrot.begin(); it != mrot.end(); ++it) {
|
2007-11-16 15:01:58 +03:00
|
|
|
if (cmSystemTools::StringStartsWith(it->first.c_str(),
|
2016-05-16 17:34:04 +03:00
|
|
|
"!VisualStudio.DTE.")) {
|
2007-11-16 15:01:58 +03:00
|
|
|
IDispatchPtr disp(it->second);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (disp != (IDispatch*)0) {
|
2007-11-16 15:01:58 +03:00
|
|
|
std::string slnName;
|
|
|
|
hr = GetIDESolutionFullName(disp, slnName);
|
|
|
|
ReportHRESULT(hr, "GetIDESolutionFullName");
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (FilesSameSolution(slnFile, slnName)) {
|
2007-11-16 15:01:58 +03:00
|
|
|
instances.push_back(disp);
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
// std::cout << "Found Visual Studio instance." << std::endl;
|
|
|
|
// std::cout << " ROT entry name: " << it->first << std::endl;
|
|
|
|
// std::cout << " ROT entry object: "
|
2007-11-20 19:10:11 +03:00
|
|
|
// << (IUnknown*) it->second << std::endl;
|
2016-05-16 17:34:04 +03:00
|
|
|
// std::cout << " slnFile: " << slnFile << std::endl;
|
|
|
|
// std::cout << " slnName: " << slnName << std::endl;
|
2007-11-16 15:01:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
#endif // defined(HAVE_COMDEF_H)
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
int cmCallVisualStudioMacro::GetNumberOfRunningVisualStudioInstances(
|
|
|
|
const std::string& slnFile)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
|
2008-07-30 23:26:34 +04:00
|
|
|
LogErrorsAsMessages = false;
|
|
|
|
|
2007-11-16 15:01:58 +03:00
|
|
|
#if defined(HAVE_COMDEF_H)
|
|
|
|
HRESULT hr = CoInitialize(0);
|
|
|
|
ReportHRESULT(hr, "CoInitialize");
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (SUCCEEDED(hr)) {
|
2007-11-16 15:01:58 +03:00
|
|
|
std::vector<IDispatchPtr> instances;
|
|
|
|
hr = FindVisualStudioInstances(slnFile, instances);
|
|
|
|
ReportHRESULT(hr, "FindVisualStudioInstances");
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (SUCCEEDED(hr)) {
|
2007-11-20 19:10:11 +03:00
|
|
|
count = static_cast<int>(instances.size());
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
// Force release all COM pointers before CoUninitialize:
|
|
|
|
instances.clear();
|
|
|
|
|
|
|
|
CoUninitialize();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-20 19:10:11 +03:00
|
|
|
#else
|
|
|
|
(void)slnFile;
|
2007-11-16 15:01:58 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
///! Get all running objects from the Windows running object table.
|
|
|
|
///! Save them in a map by their display names.
|
2016-05-16 17:34:04 +03:00
|
|
|
int cmCallVisualStudioMacro::CallMacro(const std::string& slnFile,
|
|
|
|
const std::string& macro,
|
|
|
|
const std::string& args,
|
|
|
|
const bool logErrorsAsMessages)
|
2007-11-16 15:01:58 +03:00
|
|
|
{
|
|
|
|
int err = 1; // no comdef.h
|
|
|
|
|
2008-07-30 23:26:34 +04:00
|
|
|
LogErrorsAsMessages = logErrorsAsMessages;
|
|
|
|
|
2007-11-16 15:01:58 +03:00
|
|
|
#if defined(HAVE_COMDEF_H)
|
|
|
|
err = 2; // error initializing
|
|
|
|
|
|
|
|
HRESULT hr = CoInitialize(0);
|
|
|
|
ReportHRESULT(hr, "CoInitialize");
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (SUCCEEDED(hr)) {
|
2007-11-16 15:01:58 +03:00
|
|
|
std::vector<IDispatchPtr> instances;
|
|
|
|
hr = FindVisualStudioInstances(slnFile, instances);
|
|
|
|
ReportHRESULT(hr, "FindVisualStudioInstances");
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (SUCCEEDED(hr)) {
|
2007-11-16 15:01:58 +03:00
|
|
|
err = 0; // no error
|
|
|
|
|
|
|
|
std::vector<IDispatchPtr>::iterator it;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (it = instances.begin(); it != instances.end(); ++it) {
|
2007-11-16 15:01:58 +03:00
|
|
|
hr = InstanceCallMacro(*it, macro, args);
|
|
|
|
ReportHRESULT(hr, "InstanceCallMacro");
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (FAILED(hr)) {
|
2007-11-16 15:01:58 +03:00
|
|
|
err = 3; // error attempting to call the macro
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (instances.empty()) {
|
2007-11-16 15:01:58 +03:00
|
|
|
// no instances to call
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
// cmSystemTools::Message(
|
2007-11-16 15:01:58 +03:00
|
|
|
// "cmCallVisualStudioMacro::CallMacro no instances found to call",
|
|
|
|
// "Warning");
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
// Force release all COM pointers before CoUninitialize:
|
|
|
|
instances.clear();
|
|
|
|
|
|
|
|
CoUninitialize();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
#else
|
2007-11-20 19:10:11 +03:00
|
|
|
(void)slnFile;
|
|
|
|
(void)macro;
|
|
|
|
(void)args;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (LogErrorsAsMessages) {
|
2008-07-30 23:26:34 +04:00
|
|
|
cmSystemTools::Message("cmCallVisualStudioMacro::CallMacro is not "
|
2016-05-16 17:34:04 +03:00
|
|
|
"supported on this platform");
|
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
#endif
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (err && LogErrorsAsMessages) {
|
2007-11-16 15:01:58 +03:00
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "cmCallVisualStudioMacro::CallMacro failed, err = " << err;
|
2008-07-30 23:26:34 +04:00
|
|
|
cmSystemTools::Message(oss.str().c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
2008-07-30 23:26:34 +04:00
|
|
|
return 0;
|
2007-11-16 15:01:58 +03:00
|
|
|
}
|