注意:问题最初是关于 Python 和win32com
的,但事实证明问题存在于 COM 服务器中,而不是客户端。因此,问题和答案中的信息适用于所有可用作 COM 客户端的语言。
也许有人可以帮忙。我面临的是以下情况。我可以用我的代码实例化VisualStudio.VCProjectEngine.8.0
就好了。相关代码:
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
...给出了这个错误:
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)
如您所见,一个工作正常,另一个神秘地失败了。是什么赋予了?
这是makepy.py -i
给出的:
>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)
注意:我刚刚注意到由匹配给出的 CLSIDmakepy.py -i
匹配VisualStudio.VCProjectEngine.8.0
,但不匹配VisualStudio.VCProjectEngine.10.0
(注册表显示的位置{4547a58d-fc1c-4502-84fa-0163ee766635}
):
但是,即使将元组调整为VisualStudio.VCProjectEngine.10.0
to('{4547a58d-fc1c-4502-84fa-0163ee766635}', 0, 10, 0)
也无济于事。
该错误似乎与 Visual Studio 2010 及更高版本有关。我还尝试使用 给定VisualStudio.VCProjectEngine.11.0
的VisualStudio.VCProjectEngine.12.0
值作为 ProgID makepy.py -i
,但错误相似:
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)