GetObject fails to retrieve an instance of an object when
a moniker is used.
For example, when working with WMI the following
will fail :-
>>> import win32com.client
>>> o = win32com.client.GetObject("winmgmts:")
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python23\lib\site-
packages\win32com\client\__init__.py", line 73, in
GetObject
return Moniker(Pathname, clsctx)
File "C:\Python23\lib\site-
packages\win32com\client\__init__.py", line 88, in
Moniker
moniker, i, bindCtx =
pythoncom.MkParseDisplayName(Pathname)
com_error: (-2147221020, 'Invalid syntax', None, None)
I believe there is a bug in the implementation of
MkParseDisplayName - possible corruption of passed
moniker string.
Logged In: YES
user_id=773155
In pywin32\com\win32com\src\PythonCOM.cpp, in the
function pythoncom_MkParseDisplayName, the variable
displayName appears to get set to a narrow string (char *)
even though it's declared as a wide string (WCHAR *). When
displayName is passed into the function MkParseDisplayName
(as the second parameter), its type is correct (a wide
character pointer), but the data it contains is wrong.
Here's a quick and really dirty fix:
1. add the following somewhere near the top of
PythonCOM.cpp:
#include <comdef.h>
2. change:
hr = MkParseDisplayName(pBC, displayName,
&chEaten, &pmk);
to:
hr = MkParseDisplayName(pBC, (wchar_t *)(_bstr_t)
(char *)displayName, &chEaten, &pmk);
Logged In: YES
user_id=14198
Fixed in 154 and 155.