1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmCallVisualStudioMacro.h"
#include <sstream>
#include <cmext/string_view>
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#if defined(_MSC_VER)
# define HAVE_COMDEF_H
#endif
// Just for this file:
//
static bool LogErrorsAsMessages;
#if defined(HAVE_COMDEF_H)
# include <comdef.h>
// 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
# ifdef _DEBUG
# pragma comment(lib, "comsuppwd.lib")
# else
# pragma comment(lib, "comsuppw.lib")
# endif
# else
# ifdef _DEBUG
# pragma comment(lib, "comsuppd.lib")
# else
# pragma comment(lib, "comsupp.lib")
# endif
# endif
//! Use ReportHRESULT to make a cmSystemTools::Message after calling
//! a COM method that may have failed.
# 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 << '\n'; \
_hresult_oss.flags(std::ios::dec); \
_hresult_oss << __FILE__ << "(" << __LINE__ << ")"; \
cmSystemTools::Message(_hresult_oss.str()); \
} \
}
//! Using the given instance of Visual Studio, call the named macro
HRESULT InstanceCallMacro(IDispatch* vsIDE, const std::string& macro,
const std::string& args)
{
HRESULT hr = E_POINTER;
_bstr_t macroName(macro.c_str());
_bstr_t macroArgs(args.c_str());
if (0 != vsIDE) {
DISPID dispid = (DISPID)-1;
wchar_t execute_command[] = L"ExecuteCommand";
OLECHAR* name = execute_command;
hr =
vsIDE->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &dispid);
ReportHRESULT(hr, "GetIDsOfNames(ExecuteCommand)");
if (SUCCEEDED(hr)) {
VARIANTARG vargs[2];
DISPPARAMS params;
VARIANT result;
EXCEPINFO excep;
UINT arg = (UINT)-1;
// 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;
params.cArgs = sizeof(vargs) / sizeof(vargs[0]);
params.cNamedArgs = 0;
VariantInit(&result);
memset(&excep, 0, sizeof(excep));
hr = vsIDE->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD, ¶ms, &result, &excep, &arg);
std::ostringstream oss;
/* clang-format off */
oss << "\nInvoke(ExecuteCommand)\n"
" Macro: " << macro << "\n"
" Args: " << args << '\n';
/* clang-format on */
if (DISP_E_EXCEPTION == hr) {
/* clang-format off */
oss << "DISP_E_EXCEPTION EXCEPINFO:" << excep.wCode << "\n"
" wCode: " << excep.wCode << "\n"
" wReserved: " << excep.wReserved << '\n';
/* clang-format on */
if (excep.bstrSource) {
oss << " bstrSource: " << (const char*)(_bstr_t)excep.bstrSource
<< '\n';
}
if (excep.bstrDescription) {
oss << " bstrDescription: "
<< (const char*)(_bstr_t)excep.bstrDescription << '\n';
}
if (excep.bstrHelpFile) {
oss << " bstrHelpFile: " << (const char*)(_bstr_t)excep.bstrHelpFile
<< '\n';
}
/* clang-format off */
oss << " dwHelpContext: " << excep.dwHelpContext << "\n"
" pvReserved: " << excep.pvReserved << "\n"
" pfnDeferredFillIn: "
<< reinterpret_cast<void*>(excep.pfnDeferredFillIn) << "\n"
" scode: " << excep.scode << '\n';
/* clang-format on */
}
std::string exstr(oss.str());
ReportHRESULT(hr, exstr.c_str());
VariantClear(&result);
}
}
return hr;
}
//! Get the Solution object from the IDE object
HRESULT GetSolutionObject(IDispatch* vsIDE, IDispatchPtr& vsSolution)
{
HRESULT hr = E_POINTER;
if (0 != vsIDE) {
DISPID dispid = (DISPID)-1;
wchar_t solution[] = L"Solution";
OLECHAR* name = solution;
hr =
vsIDE->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &dispid);
ReportHRESULT(hr, "GetIDsOfNames(Solution)");
if (SUCCEEDED(hr)) {
DISPPARAMS params;
VARIANT result;
EXCEPINFO excep;
UINT arg = (UINT)-1;
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,
DISPATCH_PROPERTYGET, ¶ms, &result, &excep, &arg);
ReportHRESULT(hr, "Invoke(Solution)");
if (SUCCEEDED(hr)) {
vsSolution = V_DISPATCH(&result);
}
VariantClear(&result);
}
}
return hr;
}
//! Get the FullName property from the Solution object
HRESULT GetSolutionFullName(IDispatch* vsSolution, std::string& fullName)
{
HRESULT hr = E_POINTER;
if (0 != vsSolution) {
DISPID dispid = (DISPID)-1;
wchar_t full_name[] = L"FullName";
OLECHAR* name = full_name;
hr = vsSolution->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT,
&dispid);
ReportHRESULT(hr, "GetIDsOfNames(FullName)");
if (SUCCEEDED(hr)) {
DISPPARAMS params;
VARIANT result;
EXCEPINFO excep;
UINT arg = (UINT)-1;
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,
DISPATCH_PROPERTYGET, ¶ms, &result, &excep,
&arg);
ReportHRESULT(hr, "Invoke(FullName)");
if (SUCCEEDED(hr)) {
fullName = (std::string)(_bstr_t)V_BSTR(&result);
}
VariantClear(&result);
}
}
return hr;
}
//! Get the FullName property from the Solution object, given the IDE object
HRESULT GetIDESolutionFullName(IDispatch* vsIDE, std::string& fullName)
{
IDispatchPtr vsSolution;
HRESULT hr = GetSolutionObject(vsIDE, vsSolution);
ReportHRESULT(hr, "GetSolutionObject");
if (SUCCEEDED(hr)) {
GetSolutionFullName(vsSolution, fullName);
ReportHRESULT(hr, "GetSolutionFullName");
}
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");
if (SUCCEEDED(hr)) {
hr = runningObjectTable->EnumRunning(&monikerEnumerator);
ReportHRESULT(hr, "EnumRunning");
}
if (SUCCEEDED(hr)) {
hr = monikerEnumerator->Reset();
ReportHRESULT(hr, "Reset");
}
if (SUCCEEDED(hr)) {
while (S_OK == monikerEnumerator->Next(1, &moniker, &numFetched)) {
std::string runningObjectName;
IUnknownPtr runningObjectVal;
IBindCtxPtr ctx;
hr = CreateBindCtx(0, &ctx);
ReportHRESULT(hr, "CreateBindCtx");
if (SUCCEEDED(hr)) {
LPOLESTR displayName = 0;
hr = moniker->GetDisplayName(ctx, 0, &displayName);
ReportHRESULT(hr, "GetDisplayName");
if (displayName) {
runningObjectName = (std::string)(_bstr_t)displayName;
CoTaskMemFree(displayName);
}
hr = runningObjectTable->GetObject(moniker, &runningObjectVal);
ReportHRESULT(hr, "GetObject");
if (SUCCEEDED(hr)) {
mrot.insert(std::make_pair(runningObjectName, runningObjectVal));
}
}
numFetched = 0;
moniker = 0;
}
}
return hr;
}
//! Do the two file names refer to the same Visual Studio solution? Or are
//! we perhaps looking for any and all solutions?
bool FilesSameSolution(const std::string& slnFile, const std::string& slnName)
{
if (slnFile == "ALL"_s || slnName == "ALL"_s) {
return true;
}
// 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.
HRESULT FindVisualStudioInstances(const std::string& slnFile,
std::vector<IDispatchPtr>& instances)
{
std::map<std::string, IUnknownPtr> mrot;
HRESULT hr = GetRunningInstances(mrot);
ReportHRESULT(hr, "GetRunningInstances");
if (SUCCEEDED(hr)) {
std::map<std::string, IUnknownPtr>::iterator it;
for (it = mrot.begin(); it != mrot.end(); ++it) {
if (cmHasLiteralPrefix(it->first, "!VisualStudio.DTE.")) {
IDispatchPtr disp(it->second);
if (disp != (IDispatch*)0) {
std::string slnName;
hr = GetIDESolutionFullName(disp, slnName);
ReportHRESULT(hr, "GetIDESolutionFullName");
if (FilesSameSolution(slnFile, slnName)) {
instances.push_back(disp);
// std::cout << "Found Visual Studio instance." << std::endl;
// std::cout << " ROT entry name: " << it->first << std::endl;
// std::cout << " ROT entry object: "
// << (IUnknown*) it->second << std::endl;
// std::cout << " slnFile: " << slnFile << std::endl;
// std::cout << " slnName: " << slnName << std::endl;
}
}
}
}
}
return hr;
}
#endif // defined(HAVE_COMDEF_H)
int cmCallVisualStudioMacro::GetNumberOfRunningVisualStudioInstances(
const std::string& slnFile)
{
int count = 0;
LogErrorsAsMessages = false;
#if defined(HAVE_COMDEF_H)
HRESULT hr = CoInitialize(0);
ReportHRESULT(hr, "CoInitialize");
if (SUCCEEDED(hr)) {
std::vector<IDispatchPtr> instances;
hr = FindVisualStudioInstances(slnFile, instances);
ReportHRESULT(hr, "FindVisualStudioInstances");
if (SUCCEEDED(hr)) {
count = static_cast<int>(instances.size());
}
// Force release all COM pointers before CoUninitialize:
instances.clear();
CoUninitialize();
}
#else
(void)slnFile;
#endif
return count;
}
//! Get all running objects from the Windows running object table.
//! Save them in a map by their display names.
int cmCallVisualStudioMacro::CallMacro(const std::string& slnFile,
const std::string& macro,
const std::string& args,
const bool logErrorsAsMessages)
{
int err = 1; // no comdef.h
LogErrorsAsMessages = logErrorsAsMessages;
#if defined(HAVE_COMDEF_H)
err = 2; // error initializing
HRESULT hr = CoInitialize(0);
ReportHRESULT(hr, "CoInitialize");
if (SUCCEEDED(hr)) {
std::vector<IDispatchPtr> instances;
hr = FindVisualStudioInstances(slnFile, instances);
ReportHRESULT(hr, "FindVisualStudioInstances");
if (SUCCEEDED(hr)) {
err = 0; // no error
std::vector<IDispatchPtr>::iterator it;
for (it = instances.begin(); it != instances.end(); ++it) {
hr = InstanceCallMacro(*it, macro, args);
ReportHRESULT(hr, "InstanceCallMacro");
if (FAILED(hr)) {
err = 3; // error attempting to call the macro
}
}
if (instances.empty()) {
// no instances to call
// cmSystemTools::Message(
// "cmCallVisualStudioMacro::CallMacro no instances found to call",
// "Warning");
}
}
// Force release all COM pointers before CoUninitialize:
instances.clear();
CoUninitialize();
}
#else
(void)slnFile;
(void)macro;
(void)args;
if (LogErrorsAsMessages) {
cmSystemTools::Message("cmCallVisualStudioMacro::CallMacro is not "
"supported on this platform");
}
#endif
if (err && LogErrorsAsMessages) {
std::ostringstream oss;
oss << "cmCallVisualStudioMacro::CallMacro failed, err = " << err;
cmSystemTools::Message(oss.str());
}
return 0;
}
|