[python-win32] help passing a buffer to an ActiveX object via win32com

Tim Roberts timr at probo.com
Sat Aug 8 00:21:52 CEST 2009


Orest Kozyar wrote:
> I'm trying to use an ActiveX object to read data from hardware. 
> Certain methods do not appear to work, specifically ones that expect a
> pointer to an array of 32-bit floats as one of the variables.  One
> method is /ReadTag/.
>
> The C prototype is
> /long ReadTag(LPCTSTR name, float* pBuf, long nOS, long nWords);/

That's not really the COM-approved way to declare that.  Note the signature:

> Using MakePy, the signature is
> /def ReadTag(self, Name=defaultNamedNotOptArg,
> pBuf=defaultNamedNotOptArg, nOS=defaultNamedNotOptArg,
> nWords=defaultNamedNotOptArg):
>      return self._oleobj_.InvokeTypes(9, LCID, 1, (3, 0), ((8, 0),
> (16388,0), (3, 0), (3, 0)),Name, pBuf, nOS, nWords)/

16388 means it a single float (VT_R4), passed by reference.  That
matches your first experience.


> Alternatively:
> /x = numpy.zeros(100)
> obj.ReadTag('microphone', x.ctypes.data, 0, 100)
> /returns a tuple and no error is raised:
> /(1, 4.28, 0, 100)/
> However, x is not updated with the new data from the hardware buffer.

No, because x is not an array of floats (4-byte).  It's an array of
doubles (8-byte).  You might try:
    x = numpy.zeros( 100, numpy.float32 )
but I'm not hopeful.


> The following does not work either:
> /x = numpy.zeros(100)/
> /ptr = x.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
> obj.ReadTag('microphone', ptr, 0, 100)/

No, again, because you're passing a chunk of doubles to an API that
expects an array of singles.


> Although no error is raised for the above two examples where I am
> attempting to pass a pointer to the array, x remains an array of zeros
> (the microphone is reading a 1 kHz sinusoid so x should be a sine
> wave).  I'm a bit confused and not sure what the best way to proceed
> at this point is.  How do a pass a pointer to my array into the
> ActiveX method?

Whatever you do is going to be a hack, because it's not declared in a
COM-safe manner, but I suspect you're on the right track with the ctypes
scheme.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list