Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 546655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:53:24+00:00 2026-05-13T10:53:24+00:00

I need to dynamically create com objects from an activex dll and each of

  • 0

I need to dynamically create com objects from an activex dll and each of the objects can raise events which should be handled with event handlers.

I can do this easily with win32com.client.Dispatch and win32com.client.WithEvents and associate a “separate” class of event handlers with each of the objects. Like so:

class evt_1:
    def OnEvent(self):
        print "got event from object 1"

class evt_2:
    def OnEvent(self):
        print "got event from object 2"

obj_1 = win32com.client.Dispatch("mycom")
ev_1  = win32com.client.WithEvents(obj_1, evt_1)

obj_2 = win32com.client.Dispatch("mycom")
ev_1  = win32com.client.WithEvents(obj_2, evt_2)

But if I dynamically create the objects, lets say in a list:

listOfObjects = []
for i in range(10):
    obj = win32com.client.Dispatch("mycom")
    listOfObjects.append(obj)
    ev = win32com.client.WithEvents(obj, MyEventHandlerClass)

I want to code the event handlers only once, since I don’t know how many objects I would be creating until run time. And I don’t know how to get the object that raised the event from inside the event handler.

In VB 6, I’ve used the activex control using control arrays, and the event handlers simply get an “index” value of the control that raised the event.

Do you think something similar can be done in python ?
I am not sure what python decorators work for, but can they be used to “decorate” the MyEventHandlerClass for each index of the com object?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:53:25+00:00Added an answer on May 13, 2026 at 10:53 am

    It’s a major flaw of win32com’s event handling that you have to pass in a class object rather than a class instance.

    You can attach state to your classes using win32com, however, by creating classes dynamically using new.classobj:

    from win32com.client import Dispatch, WithEvents
    from new import classobj
    
    class MyEventHandler(object):
        def OnVisible(self, visible):
            print "got event from object %d" % self.obj_index
    
    listOfObjects = []
    for i in range(3):
        handler = classobj('Handler_%s' % i,(MyEventHandler,),{})
        handler.obj_index = i
        ie = Dispatch("InternetExplorer.Application")
        listOfObjects.append(ie)
        WithEvents(ie, handler)
    
    listOfObjects[0].Visible = 1
    listOfObjects[2].Visible = 1
    

    Output:

    got event from object 0
    got event from object 2
    

    You might want to look into the comtypes module (see event handling) if you want to do this in a saner way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 316k
  • Answers 316k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is no such functionality built in spec or in… May 13, 2026 at 11:28 pm
  • Editorial Team
    Editorial Team added an answer If you are on about speed, then technically I would… May 13, 2026 at 11:28 pm
  • Editorial Team
    Editorial Team added an answer Your code almost works -- but remember that R creates… May 13, 2026 at 11:28 pm

Related Questions

I'm making a flash quiz which will have a series of questions. Each question
I'm using rhino via the bean scripting framework to create and configure objects in
I'm following Railscast 88 to create a dynamic dependent dropdown menu. http://railscasts.com/episodes/88-dynamic-select-menus I'm rendering
I have a web page with a GridView. The GridView contains a list of
I know I can do something like the following code to dynamically create a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.