PythonCOM - How to get (LPDISPATCH) of myself?

Markus Wankus markus_wankusGETRIDOFALLCAPS at hotmail.com
Sun Mar 14 04:33:24 CET 2004


Well, I'm sure my unique application of a Python COM server as a 
Solidworks addin is not of much interest to most - but my lesson in the 
obvious may be. ;o)  I finally got the plumbing working so I can now 
move onto actual, meaningful coding!!

When using a garbage collected language like Python - it is important to 
  keep references to your controls around in your class.  You can't 
execute a callback if the button object you are trying to click has been 
garbage collected!!  MAN!  I swear sometimes I can be pretty thick....

Markus.

Markus Wankus wrote:
> Getting clooooseeerr......
> 
> OK, apparently what I want is:
> 
> win32com.server.util.wrap(self)
> 
> This seems to work fine.  All I have left to do is to get my callbacks 
> to work.  They are intermittent, but I think it is not Python's fault...
> 
> Markus.
> 
> Markus Wankus wrote:
> 
>> In the spirit of trial and error.. ;o) ..I tried the following to get 
>> a Dispatch object to myself from within my Python COM server:
>>
>> pythoncom.UnwrapObject(pythoncom.GetActiveObject(self._reg_progid_))
>>
>> But got the following exception:
>>
>> Traceback (most recent call last):
>>   File "C:\PYTHON23\lib\site-packages\win32com\universal.py", line 
>> 173, in dispatch
>>     retVal = ob._InvokeEx_(meth.dispid, 0, meth.invkind, args, None, 
>> None)
>>   File "C:\PYTHON23\lib\site-packages\win32com\server\policy.py", line 
>> 322, in _InvokeEx_
>>     return self._invokeex_(dispid, lcid, wFlags, args, kwargs, 
>> serviceProvider)
>>   File "C:\PYTHON23\lib\site-packages\win32com\server\policy.py", line 
>> 541, in _invokeex_
>>     return func(*args)
>>   File "E:\_DEV\python\solidworks\PropertyPageUtils.py", line 53, in 
>> ConnectToSW
>>     obj = 
>> pythoncom.UnwrapObject(pythoncom.GetActiveObject(self._reg_progid_))
>> pywintypes.com_error: (-2147221021, 'Operation unavailable', None, None)
>>
>> Can anyone tell me what 'Operation unavailable' might be hinting at?
>>
>> Markus.
>>
>> Markus Wankus wrote:
>>
>>> I have a Python COM server which supports callbacks for a large CAD 
>>> program.  It adds menu items and then listens to events - however, I 
>>> have a problem (obviously)...  I'm a COM idiot - so please expect 
>>> dumb questions.
>>>
>>> There are a bunch of functions that require a pointer to the event 
>>> handler, or a pointer to my COM server.  Prolem is - it is looking 
>>> for a pointer to a Dispatch object - but I have already been 
>>> dispatched. Maybe an example will clear things up.  One of the 
>>> functions is as follows:
>>>
>>> Status = SldWorks.SetAddinCallbackInfo ( ModuleHandle, 
>>> AddinCallbacks, Cookie )
>>>
>>> Input:
>>>  (long) ModuleHandle
>>>  Instance handle of the add-in
>>>
>>> Input:
>>>  (LPDISPATCH) AddinCallbacks
>>>  Pointer to the object that includes the add-in callback methods
>>>
>>> Input:
>>>  (long) Cookie
>>>  Add-in ID; this is the same Cookie you specified in 
>>> SwAddin::ConnectToSW
>>>
>>> Output:
>>>  (VARIANT_BOOL) Status
>>>  TRUE if successful, FALSE if unsuccessful
>>>
>>> What I *want* to do is essentially:
>>>
>>> SetAddinCallbackInfo(id(self), self, self.ICookie)
>>>
>>> ...however - this fails because "self" (my COM server) is not a 
>>> Pointer to a Dispatch object.
>>>
>>> I can get it to work if I do the following:
>>>
>>> SetAddinCallbackInfo(id(self), Dispatch(self._reg_progid_), 
>>> self.ICookie)
>>>
>>> ...however this dipatches a new instance of my COM server, losing any 
>>> attributes that may have been set since instantiation - thus is useless.
>>>
>>> Is there a way to get a pointer to a Dispatch object of yourself?
>>>
>>> Thanks,
>>> Mark.



More information about the Python-list mailing list