0

I try to call a function from a com server with the signature (.idl file):

typedef [v1_enum] enum Mode
{
   Mode1,
   Mode2
}
Mode;

[id(33)] HRESULT GetPosition
(
    [out] double* x,
    [out] double* y,
    [in] Mode positionMode
);

The python code looks like:

import win32com.client

obj = win32com.client.gencache.EnsureDispatch('Server.Object.1')

from win32com.client import constants as c
(x,y) = obj.GetPosition(Mode=c.Mode1)
# I tried also x,y=... x=...

And I get the error message

pywintypes.com_error: (-2147352562, 'Invalid number of parameters.', None, None)

It works well when passing parameters to functions or returning single output arguments, like:

obj.setValue(42)
x = obj.getValue()

How can I deal with multiple output parameters?

martineau
  • 119,623
  • 25
  • 170
  • 301
MiB_Coder
  • 865
  • 1
  • 7
  • 20
  • It looks like `GetPosition` also returns an `HRESULT`. could you try `(h,x,y) = obj.GetPosition(Mode=c.Mode1)`? – AbbeGijly Apr 28 '20 at 17:48
  • It gives the same result: Invalid number of parameters – MiB_Coder Apr 29 '20 at 06:53
  • 1
    The named parameter for obj.GetPosition() is positionMode, not Mode. It is slightly odd to put the [out] parameters first, ahead of the [in], and usually one of them is [out,retval] for container code that can't handle tuples. The gencache code will have generated something like IServerObject1.py in the ..\AppData\Local\Temp\gen_py\3.7\SomeLongCLSID directory: where you will see what parameters the call is expecting. Then `x,y = obj.GetPosition(positionMode = c.Mode1)` might work. – DS_London Jun 01 '21 at 20:05
  • Yes, that was helpful. The right call is `(x,y)=obj.GetPosition(0,0,Mode=c.Mode1)` – MiB_Coder Nov 05 '21 at 12:20

1 Answers1

0

The problem was indeed the signature of the function, which was translated to:

def GetPosition(self, x=pythoncom.Missing, y=pythoncom.Missing, Mode=defaultNamedNotOptArg):

That means that the function needs to be called like:

(x,y)=obj.GetPosition(0,0,Mode=c.Mode1)

Thanks a lot to DS_London to giving the right hint.

MiB_Coder
  • 865
  • 1
  • 7
  • 20