Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34554 > unrolled thread

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

Started bybitbucket <coder1024@gmail.com>
First post2012-12-10 11:13 -0800
Last post2012-12-11 07:37 -0800
Articles 14 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" bitbucket <coder1024@gmail.com> - 2012-12-10 11:13 -0800
    Re: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" Terry Reedy <tjreedy@udel.edu> - 2012-12-10 15:58 -0500
      Re: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" bitbucket <coder1024@gmail.com> - 2012-12-10 13:39 -0800
        Re: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-12-10 18:39 -0500
        Re: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" Mark Hammond <skippy.hammond@gmail.com> - 2012-12-11 12:16 +1100
          Re: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" bitbucket <coder1024@gmail.com> - 2012-12-11 07:48 -0800
            Re: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" bitbucket <coder1024@gmail.com> - 2012-12-11 07:54 -0800
            Re: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" bitbucket <coder1024@gmail.com> - 2012-12-11 07:54 -0800
            Re: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" Mark Hammond <mhammond@skippinet.com.au> - 2012-12-12 08:55 +1100
          Re: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" bitbucket <coder1024@gmail.com> - 2012-12-11 07:48 -0800
      Re: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" bitbucket <coder1024@gmail.com> - 2012-12-10 13:39 -0800
    Re: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" Paul Kölle <paul@subsignal.org> - 2012-12-11 09:42 +0100
      Re: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" bitbucket <coder1024@gmail.com> - 2012-12-11 07:37 -0800
      Re: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" bitbucket <coder1024@gmail.com> - 2012-12-11 07:37 -0800

#34554 — accessing an OLE Automation (IDispatch) server from python which requires the use of "out params"

Frombitbucket <coder1024@gmail.com>
Date2012-12-10 11:13 -0800
Subjectaccessing an OLE Automation (IDispatch) server from python which requires the use of "out params"
Message-ID<38a14001-82b6-4a91-b3ad-2cb5c8ee580d@googlegroups.com>
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 and I'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?

[toc] | [next] | [standalone]


#34563

FromTerry Reedy <tjreedy@udel.edu>
Date2012-12-10 15:58 -0500
Message-ID<mailman.682.1355173145.29569.python-list@python.org>
In reply to#34554
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/pywin32/?source=directory

I assume it can handle out params.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#34571

Frombitbucket <coder1024@gmail.com>
Date2012-12-10 13:39 -0800
Message-ID<ed7dd76c-c1f2-4d33-9e14-41e8be65c29a@googlegroups.com>
In reply to#34563
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/pywin32/?source=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.

[toc] | [prev] | [next] | [standalone]


#34582

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-12-10 18:39 -0500
Message-ID<mailman.694.1355182737.29569.python-list@python.org>
In reply to#34571
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/pywin32/?source=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/

[toc] | [prev] | [next] | [standalone]


#34592

FromMark Hammond <skippy.hammond@gmail.com>
Date2012-12-11 12:16 +1100
Message-ID<mailman.704.1355188608.29569.python-list@python.org>
In reply to#34571
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/pywin32/?source=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

[toc] | [prev] | [next] | [standalone]


#34628

Frombitbucket <coder1024@gmail.com>
Date2012-12-11 07:48 -0800
Message-ID<5b228557-2788-4243-b840-5c8aa03d5423@googlegroups.com>
In reply to#34592
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/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?

[toc] | [prev] | [next] | [standalone]


#34630

Frombitbucket <coder1024@gmail.com>
Date2012-12-11 07:54 -0800
Message-ID<b4e28b89-a5ba-43fb-a26e-0c8ccfeda1ea@googlegroups.com>
In reply to#34628
On Tuesday, December 11, 2012 10:48:53 AM UTC-5, bitbucket wrote:
>
> 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?

[toc] | [prev] | [next] | [standalone]


#34631

Frombitbucket <coder1024@gmail.com>
Date2012-12-11 07:54 -0800
Message-ID<mailman.734.1355241302.29569.python-list@python.org>
In reply to#34628
On Tuesday, December 11, 2012 10:48:53 AM UTC-5, bitbucket wrote:
>
> 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?

[toc] | [prev] | [next] | [standalone]


#34651

FromMark Hammond <mhammond@skippinet.com.au>
Date2012-12-12 08:55 +1100
Message-ID<mailman.747.1355262914.29569.python-list@python.org>
In reply to#34628
On 12/12/2012 2:48 AM, bitbucket wrote:
> 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/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
>

[toc] | [prev] | [next] | [standalone]


#34629

Frombitbucket <coder1024@gmail.com>
Date2012-12-11 07:48 -0800
Message-ID<mailman.733.1355240942.29569.python-list@python.org>
In reply to#34592
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/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?

[toc] | [prev] | [next] | [standalone]


#34572

Frombitbucket <coder1024@gmail.com>
Date2012-12-10 13:39 -0800
Message-ID<mailman.686.1355175549.29569.python-list@python.org>
In reply to#34563
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/pywin32/?source=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.

[toc] | [prev] | [next] | [standalone]


#34607

FromPaul Kölle <paul@subsignal.org>
Date2012-12-11 09:42 +0100
Message-ID<mailman.716.1355215375.29569.python-list@python.org>
In reply to#34554
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

[toc] | [prev] | [next] | [standalone]


#34627

Frombitbucket <coder1024@gmail.com>
Date2012-12-11 07:37 -0800
Message-ID<eb3c2f80-e8d6-46d9-82b2-1decca7de24a@googlegroups.com>
In reply to#34607
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/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)...
> 

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.

[toc] | [prev] | [next] | [standalone]


#34632

Frombitbucket <coder1024@gmail.com>
Date2012-12-11 07:37 -0800
Message-ID<mailman.735.1355242863.29569.python-list@python.org>
In reply to#34607
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/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)...
> 

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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web