accessing an OLE Automation (IDispatch) server from python whichrequires the use of "out params"

B

bitbucket

I have an existing Windows application which provides an OLE Automation (IDispatch) interface. I'm not able to change that interface. I'd like to call it from a scripting language. I figure this would provide a nice quick way to invoke on the app.

I initially tried this with Windows Powershell but ran into the following problem. I was able to create the object and invoke simple methods on it. However the interface for this app has methods which take out params. i.e.you pass in a reference to a variable and the server fills in the value. I couldn't get that to work. I finally gave up and decided it was just a limitation of Powershell, not being able to work with those out params.

My next thought was to do it in python. I've been reading up on python andI've found a decent amount of into out there on doing OLE and I'm optimistic. But, I thought that I'd ask the question before digging too much farther into it...

When calling an OLE Automation (IDispatch) server from python can I make use of "out params" defined by the interface?

To get more specific, here's an example from the server's IDL for one of its methods.

[id(15), helpstring("method GetSettingValue")] VARIANT_BOOL GetSettingValue(BSTR settingName, BSTR* settingValue);

As you can see, you have to pass in an out param for settingValue. The server fills this in for you. And this is what I couldn't get to work in Powershell.

Anyone know whether or not OLE from python will allow passing in out params? Do you think this will work?
 
D

Dennis Lee Bieber

That definitely looks like a good starting point. Just hoping someone knows whether or not it'll support the out params before I spend too much time digging into it.

Well, Python itself doesn't handle "out" parameters... The win32
extensions mostly access the defined Win32 API... May not be that useful
for ad-hoc interfaces.

The ctypes library may be a more direct means -- it interfaces
between Python objects (with specific attributes/properties) used as
data types and the C-language calling conventions... "ctypes exports the
byref() function which is used to pass parameters by reference"

PowerShell likely uses the same method you'd need in Python --
passing a mutable object (an array in PS), but the receiver would need
to be expecting such an object.
 
M

Mark Hammond

That definitely looks like a good starting point. Just hoping
someone knows whether or not it'll support the out params before I
spend too much time digging into it.

"out" params are best supported if the object supplied a typelib - then
Python knows the params are out and does the right thing automagically.
If out params are detected, the result of the function will be a tuple
of (real_result, out_param1, ...)

Even if no typelib is supported, you can access them with a little pain
via the win32com.client.Dispatch() object. You might like to follow up
to the (e-mail address removed) mailing list where many people will be
able to help.

HTH,

Mark
 
P

Paul Kölle

Hi,

Am 10.12.2012 20:13, schrieb bitbucket:
I have an existing Windows application which provides an OLE
Automation (IDispatch) interface. I'm not able to change that
interface. I'd like to call it from a scripting language. I figure
this would provide a nice quick way to invoke on the app.

I initially tried this with Windows Powershell but ran into the
following problem. I was able to create the object and invoke simple
methods on it. However the interface for this app has methods which
take out params. i.e. you pass in a reference to a variable and the
server fills in the value. I couldn't get that to work. I finally
gave up and decided it was just a limitation of Powershell, not being
able to work with those out params.

[snipp]
Before switching technologies I'd check if this solves your problem
http://geekswithblogs.net/Lance/archive/2009/01/14/pass-by-reference-parameters-in-powershell.aspx


TL;DR IMHO "out" parameters are basically pointers (pass by reference)
and need to be passed like GetSettingValue("name", [ref]$value)...

cheers
Paul
 
B

bitbucket

B

bitbucket

B

bitbucket

"out" params are best supported if the object supplied a typelib - then
Python knows the params are out and does the right thing automagically.
If out params are detected, the result of the function will be a tuple
of (real_result, out_param1, ...)

Even if no typelib is supported, you can access them with a little pain
via the win32com.client.Dispatch() object. You might like to follow up
to the (e-mail address removed) mailing list where many people will be
able to help.

HTH,

Mark

Mark, thanks for the reply. In this case, I have a type library and attempted to use MakePy but it doesn't seem to be working as expected.

I was reading through CH12 of your Python Programming on Win32 book (http://oreilly.com/catalog/pythonwin32/chapter/ch12.html). I was hopeful given your description of MakePy that I could get this to work. It appears that you're saying MakePy will convert "byref" args in a function over to return values.

For example, the IDL in the server includes the following 3 functions.

[id(1)] void ShowMessage(BSTR msg);
[id(2)] void GetSettingValue(BSTR settingName, BSTR* settingValue);
[id(3)] void SetSettingValue(BSTR settingName, BSTR settingValue);

The thorny one is the GetSettingValue since it takes the out param. When Irun MakePy, it generates the below.

def ShowMessage(self, msg=defaultNamedNotOptArg):
return self._oleobj_.InvokeTypes(1, LCID, 1, (24, 0), ((8, 0),),msg
)

def GetSettingValue(self, settingName=defaultNamedNotOptArg, settingValue=defaultNamedNotOptArg):
return self._oleobj_.InvokeTypes(2, LCID, 1, (24, 0), ((8, 0), (16392, 0)),settingName
, settingValue)

def SetSettingValue(self, settingName=defaultNamedNotOptArg, settingValue=defaultNamedNotOptArg):
return self._oleobj_.InvokeTypes(3, LCID, 1, (24, 0), ((8, 0), (8, 0)),settingName
, settingValue)

I noticed that the argument type is different for the out param (16392 instead of 8). However, it doesn't appear to me that its generating return values instead of args (though I'm not very experienced in python).

I tried invoking these in python. The ShowMessage and SetSettingValue workgreat. I can't get the GetSettingValue to work though. Perhaps there's adifferent syntax I need when using the MakePy generated code?
 
B

bitbucket

"out" params are best supported if the object supplied a typelib - then
Python knows the params are out and does the right thing automagically.
If out params are detected, the result of the function will be a tuple
of (real_result, out_param1, ...)

Even if no typelib is supported, you can access them with a little pain
via the win32com.client.Dispatch() object. You might like to follow up
to the (e-mail address removed) mailing list where many people will be
able to help.

HTH,

Mark

Mark, thanks for the reply. In this case, I have a type library and attempted to use MakePy but it doesn't seem to be working as expected.

I was reading through CH12 of your Python Programming on Win32 book (http://oreilly.com/catalog/pythonwin32/chapter/ch12.html). I was hopeful given your description of MakePy that I could get this to work. It appears that you're saying MakePy will convert "byref" args in a function over to return values.

For example, the IDL in the server includes the following 3 functions.

[id(1)] void ShowMessage(BSTR msg);
[id(2)] void GetSettingValue(BSTR settingName, BSTR* settingValue);
[id(3)] void SetSettingValue(BSTR settingName, BSTR settingValue);

The thorny one is the GetSettingValue since it takes the out param. When Irun MakePy, it generates the below.

def ShowMessage(self, msg=defaultNamedNotOptArg):
return self._oleobj_.InvokeTypes(1, LCID, 1, (24, 0), ((8, 0),),msg
)

def GetSettingValue(self, settingName=defaultNamedNotOptArg, settingValue=defaultNamedNotOptArg):
return self._oleobj_.InvokeTypes(2, LCID, 1, (24, 0), ((8, 0), (16392, 0)),settingName
, settingValue)

def SetSettingValue(self, settingName=defaultNamedNotOptArg, settingValue=defaultNamedNotOptArg):
return self._oleobj_.InvokeTypes(3, LCID, 1, (24, 0), ((8, 0), (8, 0)),settingName
, settingValue)

I noticed that the argument type is different for the out param (16392 instead of 8). However, it doesn't appear to me that its generating return values instead of args (though I'm not very experienced in python).

I tried invoking these in python. The ShowMessage and SetSettingValue workgreat. I can't get the GetSettingValue to work though. Perhaps there's adifferent syntax I need when using the MakePy generated code?
 
B

bitbucket

I noticed that the argument type is different for the out param (16392 instead of 8). However, it doesn't appear to me that its generating return values instead of args (though I'm not very experienced in python).

I see that the value 16392 is really VT_BYREF | VT_BSTR and 8 is just VT_BSTR. So in that case it appears MakePy is taking noticed at least of the VT_BYREF and including that in the generated code (since it uses 16392).

So maybe there's a special way I need to call the generated wrapper?
 
B

bitbucket

I noticed that the argument type is different for the out param (16392 instead of 8). However, it doesn't appear to me that its generating return values instead of args (though I'm not very experienced in python).

I see that the value 16392 is really VT_BYREF | VT_BSTR and 8 is just VT_BSTR. So in that case it appears MakePy is taking noticed at least of the VT_BYREF and including that in the generated code (since it uses 16392).

So maybe there's a special way I need to call the generated wrapper?
 
M

Mark Hammond

"out" params are best supported if the object supplied a typelib -
then Python knows the params are out and does the right thing
automagically. If out params are detected, the result of the
function will be a tuple of (real_result, out_param1, ...)

Even if no typelib is supported, you can access them with a little
pain via the win32com.client.Dispatch() object. You might like to
follow up to the (e-mail address removed) mailing list where many
people will be able to help.

HTH,

Mark

Mark, thanks for the reply. In this case, I have a type library and
attempted to use MakePy but it doesn't seem to be working as
expected.

I was reading through CH12 of your Python Programming on Win32 book
(http://oreilly.com/catalog/pythonwin32/chapter/ch12.html). I was
hopeful given your description of MakePy that I could get this to
work. It appears that you're saying MakePy will convert "byref" args
in a function over to return values.

For example, the IDL in the server includes the following 3
functions.

[id(1)] void ShowMessage(BSTR msg); [id(2)] void GetSettingValue(BSTR
settingName, BSTR* settingValue); [id(3)] void SetSettingValue(BSTR
settingName, BSTR settingValue);

The thorny one is the GetSettingValue since it takes the out param.
When I run MakePy, it generates the below.

def ShowMessage(self, msg=defaultNamedNotOptArg): return
self._oleobj_.InvokeTypes(1, LCID, 1, (24, 0), ((8, 0),),msg )

def GetSettingValue(self, settingName=defaultNamedNotOptArg,
settingValue=defaultNamedNotOptArg): return
self._oleobj_.InvokeTypes(2, LCID, 1, (24, 0), ((8, 0), (16392,
0)),settingName , settingValue)

def SetSettingValue(self, settingName=defaultNamedNotOptArg,
settingValue=defaultNamedNotOptArg): return
self._oleobj_.InvokeTypes(3, LCID, 1, (24, 0), ((8, 0), (8,
0)),settingName , settingValue)

I noticed that the argument type is different for the out param
(16392 instead of 8). However, it doesn't appear to me that its
generating return values instead of args (though I'm not very
experienced in python).

I tried invoking these in python. The ShowMessage and
SetSettingValue work great. I can't get the GetSettingValue to work
though. Perhaps there's a different syntax I need when using the
MakePy generated code?

Seeing the "real" return value is void, it should just be a matter of:

settingValue = ob.GetSettingValue("settingName")

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top