Note: the question was originally about Python and win32com
, but it turns out that the issue exists within the COM server, not the client-side. Therefore the information in the question and the answer are applicable to all languages that are usable as COM clients.
Maybe someone can help out. What I am facing is the following situation. I can instantiate VisualStudio.VCProjectEngine.8.0
just fine with my code. The relevant code:
import win32com
vcver = {
'VisualStudio.VCProjectEngine.8.0' : ('{FBBF3C60-2428-11D7-8BF6-00B0D03DAA06}', 0, 8, 0),
'VisualStudio.VCProjectEngine.10.0' : ('{0CD36BB6-D828-4DB9-91BF-AD493EE76B79}', 0, 10, 0),
}
for ProgID, vc in vcver.iteritems():
# Error also happens without the following line
win32com.client.gencache.EnsureModule(vc[0], vc[1], vc[2], vc[3])
o = win32com.client.gencache.GetClassForProgID(ProgID)()
if o:
try:
# The following line causes the exception ...
o.CreateProject('test')
print "Success for %s" % ProgID
except Exception as e:
print str(e)
raise
... gives this error:
Success for VisualStudio.VCProjectEngine.8.0
(-2147352567, 'Exception occurred.', (0, u'mscorlib', u"Could not find a part of the path 'C:\\Python27\\ProjectComponents'.", None, 0, -2147024893), None)
Traceback (most recent call last):
File "D:\scriptname.py", line 123, in <module>
o.CreateProject('test')
File "C:\Users\Username\AppData\Local\Temp\gen_py\2.7\0CD36BB6-D828-4DB9-91BF-AD493EE76B79x0x10x0.py", line 2756, in CreateProject
ret = self._oleobj_.InvokeTypes(213, LCID, 1, (9, 0), ((8, 1),),projectName
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'mscorlib', u"Could not find a part of the path 'C:\\Python27\\ProjectComponents'.", None, 0, -2147024893), None)
So as you can see, one works fine and the other fails mysteriously. What gives?
Here's what makepy.py -i
gives:
>makepy.py -i VisualStudio.VCProjectEngine.8.0
VCProjectEngineLibrary
{FBBF3C60-2428-11D7-8BF6-00B0D03DAA06}, lcid=0, major=8, minor=0
>>> # Use these commands in Python code to auto generate .py support
>>> from win32com.client import gencache
>>> gencache.EnsureModule('{FBBF3C60-2428-11D7-8BF6-00B0D03DAA06}', 0, 8, 0)
>makepy.py -i VisualStudio.VCProjectEngine.10.0
VCProjectEngineLibrary
{0CD36BB6-D828-4DB9-91BF-AD493EE76B79}, lcid=0, major=10, minor=0
>>> # Use these commands in Python code to auto generate .py support
>>> from win32com.client import gencache
>>> gencache.EnsureModule('{0CD36BB6-D828-4DB9-91BF-AD493EE76B79}', 0, 10, 0)
Note: I just noticed that the CLSID given by makepy.py -i
matches for VisualStudio.VCProjectEngine.8.0
, but not for VisualStudio.VCProjectEngine.10.0
(where the registry shows {4547a58d-fc1c-4502-84fa-0163ee766635}
):
However, even adjusting the tuple for VisualStudio.VCProjectEngine.10.0
to be ('{4547a58d-fc1c-4502-84fa-0163ee766635}', 0, 10, 0)
doesn't help with the exception.
The error appears to be with Visual Studio 2010 and later. I also tried VisualStudio.VCProjectEngine.11.0
and VisualStudio.VCProjectEngine.12.0
as ProgID with the values given by makepy.py -i
, but the errors were similar:
Error for VisualStudio.VCProjectEngine.11.0: (-2147352567, 'Exception occurred.', (0, u'mscorlib', u"Could not find a part of the path 'C:\\Python27\\ProjectComponents'.", None, 0, -2147024893), None)
Error for VisualStudio.VCProjectEngine.10.0: (-2147352567, 'Exception occurred.', (0, u'mscorlib', u"Could not find a part of the path 'C:\\Python27\\ProjectComponents'.", None, 0, -2147024893), None)
Error for VisualStudio.VCProjectEngine.12.0: (-2147352567, 'Exception occurred.', (0, u'mscorlib', u'One or more errors occurred.', None, 0, -2146233088), None)
TL;WR (To long won't read)
The gist is that Microsoft has quietly changed the semantics of an automation interface,
VisualStudio.VCProjectEngine
, starting with Visual Studio 2010 and makes almost no mention of that fact in the official documentation. Instead developers seem to be expected to collect tidbits MSFT employees leave all over the MS Connect and forum pages and connect them in a meaningful way.Wow, so Microsoft decided to change the semantics of how
VCProjectEngine
. Compare:The remark reads:
Unfortunately Microsoft seems to have decided that it's not legit to create a new project using an old interface, i.e. change the semantics of the interface.
This post is an attempt to collect and connect the information available - and that's available not in the official documentation.
And to add insult to injury, the documentation of this ends up in a meager remark at the bottom of the documentation for said interface.
If you want a better explanation ... or rather tidbits ... do a web search. Perhaps you can come up with anything better than I found. What I came up with was this statement here (emphasis mine):
It's "funny" how this vital information has not made it into the official documentation. So following the above link, all we get is an explanation of basics that existed before VS 2010, with the implication that the API is not supported only mentioned in the quoted MS Connect case.
This MS Connect case mentions setting an environment variable (
VisualStudioVersion=11.0
), but unsurprisingly this doesn't help either. Also using the Developer Command Prompt for VS201x for the respective Visual Studio version yields no results either. Another dead end.Another MS forum thread seems relevant as well: Opening .vcxproj files in the VS2010 along with this one: VS 2010 C++ VCProjectEngine failure?.
Quoting the relevant part of the latter:
A long journey ends at last? Not so ... Trying to locate this mysterious folder on a Visual Studio installation yields ... well, nothing.
... gives (stripped redundant information):
So the folders that are required for the alleged workaround don't exist in (all?) Visual Studio installations.
Side-note: the mysterious CLSID
{4547a58d-fc1c-4502-84fa-0163ee766635}
I found resolves to the nameMicrosoft.VisualStudio.Project.VisualC.VCProjectEngine.VCProjectEngineShim
under the keyHKEY_CLASSES_ROOT\Wow6432Node\CLSID\{4547a58d-fc1c-4502-84fa-0163ee766635}
. So I guess the actual thing was replaced by a shim that doesn't do its job very well.