Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   accessing an OLE Automation (IDispatch) server from python whichrequires the use of "out params" (http://www.velocityreviews.com/forums/t955321-accessing-an-ole-automation-idispatch-server-from-python-whichrequires-the-use-of-out-params.html)

bitbucket 12-10-2012 07:13 PM

accessing an OLE Automation (IDispatch) server from python whichrequires the use of "out params"
 
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?

Terry Reedy 12-10-2012 08:58 PM

Re: accessing an OLE Automation (IDispatch) server from python whichrequires the use of "out params"
 
On 12/10/2012 2:13 PM, bitbucket wrote:
> 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 believe the easiest way to do that is to install the pywin extensions
http://sourceforge.net/projects/pywi...urce=directory

I assume it can handle out params.

--
Terry Jan Reedy


bitbucket 12-10-2012 09:39 PM

Re: accessing an OLE Automation (IDispatch) server from python whichrequires the use of "out params"
 
On Monday, December 10, 2012 3:58:33 PM UTC-5, Terry Reedy wrote:
> I believe the easiest way to do that is to install the pywin extensions
>
> http://sourceforge.net/projects/pywi...urce=directory
>
> I assume it can handle out params.


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.

bitbucket 12-10-2012 09:39 PM

Re: accessing an OLE Automation (IDispatch) server from python whichrequires the use of "out params"
 
On Monday, December 10, 2012 3:58:33 PM UTC-5, Terry Reedy wrote:
> I believe the easiest way to do that is to install the pywin extensions
>
> http://sourceforge.net/projects/pywi...urce=directory
>
> I assume it can handle out params.


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.

Dennis Lee Bieber 12-10-2012 11:39 PM

Re: accessing an OLE Automation (IDispatch) server from python whichrequires the use of "out params"
 
On Mon, 10 Dec 2012 13:39:00 -0800 (PST), bitbucket
<coder1024@gmail.com> declaimed the following in
gmane.comp.python.general:

> On Monday, December 10, 2012 3:58:33 PM UTC-5, Terry Reedy wrote:
> > I believe the easiest way to do that is to install the pywin extensions
> >
> > http://sourceforge.net/projects/pywi...urce=directory
> >
> > I assume it can handle out params.

>
> 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.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/


Mark Hammond 12-11-2012 01:16 AM

Re: accessing an OLE Automation (IDispatch) server from python whichrequires the use of "out params"
 
On 11/12/2012 8:39 AM, bitbucket wrote:
> On Monday, December 10, 2012 3:58:33 PM UTC-5, Terry Reedy wrote:
>> I believe the easiest way to do that is to install the pywin
>> extensions
>>
>> http://sourceforge.net/projects/pywi...urce=directory
>>
>> I assume it can handle out params.

>
> 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 python-win32@python.org mailing list where many people will be
able to help.

HTH,

Mark

Paul Kölle 12-11-2012 08:42 AM

Re: accessing an OLE Automation (IDispatch) server from python whichrequires the use of "out params"
 
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/arch...owershell.aspx


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

cheers
Paul


bitbucket 12-11-2012 03:37 PM

Re: accessing an OLE Automation (IDispatch) server from python whichrequires the use of "out params"
 
On Tuesday, December 11, 2012 3:42:35 AM UTC-5, Paul Kölle wrote:
> Before switching technologies I'd check if this solves your problem
>
> http://geekswithblogs.net/Lance/arch...owershell.aspx
>
> TL;DR IMHO "out" parameters are basically pointers (pass by reference)
>
> and need to be passed like GetSettingValue("name", [ref]$value)...
>


Thanks for the suggestion. I believe the [ref] syntax was one of the things I tried, but it didn't work in the context of OLE Automation (IDispatch) calls.

bitbucket 12-11-2012 03:37 PM

Re: accessing an OLE Automation (IDispatch) server from python whichrequires the use of "out params"
 
On Tuesday, December 11, 2012 3:42:35 AM UTC-5, Paul Kölle wrote:
> Before switching technologies I'd check if this solves your problem
>
> http://geekswithblogs.net/Lance/arch...owershell.aspx
>
> TL;DR IMHO "out" parameters are basically pointers (pass by reference)
>
> and need to be passed like GetSettingValue("name", [ref]$value)...
>


Thanks for the suggestion. I believe the [ref] syntax was one of the things I tried, but it didn't work in the context of OLE Automation (IDispatch) calls.

bitbucket 12-11-2012 03:48 PM

Re: accessing an OLE Automation (IDispatch) server from python whichrequires the use of "out params"
 
On Monday, December 10, 2012 8:16:43 PM UTC-5, Mark Hammond wrote:
> "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 python-win32@python.org 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/pythonwin...pter/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?


All times are GMT. The time now is 12:15 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.