Call OCX from python?

Claudio Grondi claudio.grondi at freenet.de
Tue Nov 29 10:27:35 CET 2005


"JustSomeGuy" <nope at nottelling.com> schrieb im Newsbeitrag
news:VJMif.646622$tl2.454656 at pd7tw3no...
>
> "Claudio Grondi" <claudio.grondi at freenet.de> wrote in message
> news:3usl5aF12ja39U1 at individual.net...
> > "JustSomeGuy" <nope at nottelling.com> schrieb im Newsbeitrag
> > news:5n4if.627200$1i.183531 at pd7tw2no...
> > > Hi I have a commercial OCX that I want to use in
> > > my python application.  How do I call OCXs from
> > > python?
> > > TIA
> >
> > import win32com.client
> > axOCX =
> >
>
win32com.client.Dispatch("RegistryEntryForThisOCXin[VersionIndependentProgID
> > ]section")
> > retVal = axOCX.methodOfThisOCX(param1, param2)
> > value = axOCX.attributeOfThisOCX
> >
> > Claudio
> >
> >
>
> Thank you Claudio...
> Can you explain the RegistryEntryForThisOcxin[VersionIndependentProgID]
> I don't know what I should use here..

An ActiveX component (e.g. an OCX) requires to be registered before it can
be used. Usually this is done when the component is installed. If you know
the file name of the OCX start the registry editor running the command:
regedit ([Start] -> Execute -> regedit). Then search for the file name of
the OCX in the registry. One of the hits should lead you to a registry key
where you find on same hierarchy level the [VersionIndependentProgID] entry.
The name there is what you should use in the win32com.client.Dispatch() as
parameter.
To give an example let's assume you want to use the WSHOM.OCX which is
usually on any Windows system. Looking in the registry for WSHOM.OCX leads
you (probably the second hit) to the key [InProcServer32] in the
{72C24DD5-D70A-438B-8A42-98424B88AFB8} entry. Here you find also the key
[VersionIndependentProgID] with the standard value of WScript.Shell. You
should exactly know the methods and their parameter to be able to use the
OCX, see the code below :

# Raise a Yes/No/Abort dialog box provided by WSHOM.OCX:

import win32com.client
axWshShell = win32com.client.Dispatch("WScript.Shell") # WSHOM.OCX

axWshShell_Popup_Icon_Critical             =   16
axWshShell_Popup_Button_AbortRetryIgnore   =    2
axWshShell_Popup_NoAutoclose               =    0

intRetVal = axWshShell.Popup(
### Raise a message box:
   " The Popup() Text" + "\n" +
   "",
   axWshShell_Popup_NoAutoclose,
   " The Popup() Title:",
   axWshShell_Popup_Icon_Critical + axWshShell_Popup_Button_AbortRetryIgnore
)

axWshShell_Popup_Clicked_Abort             =    3  # [Abort]  button
axWshShell_Popup_Clicked_Retry             =    4  # [Retry]  button
axWshShell_Popup_Clicked_Ignore            =    5  # [Ignore] button

if(intRetVal ==  axWshShell_Popup_Clicked_Abort):
  print 'Abort clicked, return value = %i'%(intRetVal,)

if(intRetVal ==  axWshShell_Popup_Clicked_Retry):
  print 'Retry clicked, return value = %i'%(intRetVal,)

if(intRetVal ==  axWshShell_Popup_Clicked_Ignore):
  print 'Ignore clicked, return value = %i'%(intRetVal,)


To make this example complete, here an excerpt from the registry :

[HKEY_CLASSES_ROOT\CLSID\{72C24DD5-D70A-438B-8A42-98424B88AFB8}\InProcServer
32]
@="E:\\WINNT\\system32\\wshom.ocx"
"ThreadingModel"="Apartment"

[HKEY_CLASSES_ROOT\CLSID\{72C24DD5-D70A-438B-8A42-98424B88AFB8}\VersionIndep
endentProgID]
@="WScript.Shell"


Claudio
P.S. if you face problems with return values or parameter values, one of
reasons can be, that OCXses work always with Unicode strings and you haven't
taken it into consideration.





More information about the Python-list mailing list