开发者

Pywin - Initialize pvarResult by calling the VariantInit()

开发者 https://www.devze.com 2023-02-20 09:52 出处:网络
I\'m using Pywin32 to communicate with Bloomberg through its COM-library.This works rather good!However, I have stumbeled upona a problem which I consider pretty complex.If I set the property QueueEve

I'm using Pywin32 to communicate with Bloomberg through its COM-library. This works rather good! However, I have stumbeled upona a problem which I consider pretty complex. If I set the property QueueEvents of the Com object to True I the program fails. In the documentation they have a section regarding this,

If your QueueEvents property is set to True and you are performing low-level instantiation of the data control using C++, then in your data event handler (invoke) you will be required to initialize pvarResult by calling the VariantInit() function. This will prevent your application from receiving duplicate ticks.

session = win32com.client.DispatchWithEvents(comobj, EventHandler)
session.QueueEvents = True   <--  this trigger some strange "bugs" in execution
                                  if "pvarResult" is not initialized

I think I understand the theoretical aspects here, you need to initialize a datastructure before the comobject can write to it. However, how do you do this from Pywin32? That I have no clue abou开发者_JS百科t, and would appreciate any ideas or pointers(!) to how this can be done.

None of the tips below helped. My program doesn't throw an exception, it just returns the same message from the COM object again and again and again...

From the documentation:

If your QueueEvents property is set to True and you are performing low-level instantiation of the data control using C++, then in your data event handler (invoke) you will be required to initialize pvarResult by calling the VariantInit() function. This will prevent your application from receiving duplicate ticks. If this variable is not set then the data control assumes that you have not received data yet, and it will then attempt to resend it. In major containers, such as MFC and Visual Basic, this flag will automatically be initialized by the container. Keep in mind that this only pertains to applications, which set the QueueEvents property to True.


I'm not sure if this will help for your issue, but to have working COM events in Python you shouldn't forget about:

  • setting COM apartment to free threaded at the beginning of script file. This could be done using following lines

    import sys
    sys.coinit_flags = 0
    
  • generating wrapper for com library before calling first DispatchWithEvents

    from win32com.client.makepy import GenerateFromTypeLibSpec
    GenerateFromTypeLibSpec("ComLibName 1.0 Type Library")
    

If you could post how the program fails (COM object fails or maybe python trows some exceptions) maybe I could advice more.

0

精彩评论

暂无评论...
验证码 换一张
取 消