COM: Passing arrays, SAFEARRAYs, and BSTRs in connection points

Robert soundhack at hotmail.com
Wed Mar 22 10:14:47 EST 2000


The short form of the question:
pythoncom seems to convert BSTR/unicode strings to normal python strings
automatically. Is there any way of recovering the original raw data?
I am sending raw chars that are converted to BSTRs through COM to
a python program. For example I am sending 'test' (4 bytes) in a
SAFEARRAY/vector,
convert it to a bstr using BstrFromVector, then send it off to python, which
somehow converts it to a string '??' (2 bytes). Is there anyway of
retrieving the
original 4 bytes?

Now the long form:

I'm trying different ways of passing signed short (2 byte) data from a ATL
COM automation server to a python program through the connection point
interface (ie. the server
sends events back to python). After fussing (unsuccessfully) with straight
fixed arrays (MIDL statement: [id(1)] HRESULT Event(short array[10]);)
and SAFEARRAYs ( [id(2)] HRESULT Event2(SAFEARRAY(short) array); )
I found the BstrFromVector() function. Unfortunately, the data gets munged
somewhere between the BstrFromVector() function and the pythoncom event
handler
function.
Here are the two parts:
ATL side: (all error checking taken out)
 psa=SafeArrayCreateVector(VT_UI1,0,4);
 unsigned char *ptr;
 SafeArrayAccessData(psa,(void **)&ptr);
 ptr[0]='t';ptr[1]='e'; ptr[2]='s';ptr[3]='t';
 SafeArrayUnaccessData(psa);
 BstrFromVector(psa,&bstr)
 pAuto->Fire_Array(bstr); // pAuto is a atlcom object with connection points

python side: (only relevant stuff shown)
CEHBase=win32com.client.getevents("Test.Auto")
class EventHandler (CEHBase):
def OnArray(self,array):
  print "Got Array: ",len(array),array

which produces
Got Array: 2 ??

Any help much appreciated!

btw, using standard bstr functions (eg CComBSTR) to create bstr string works
fine, but
I need to be able to stuff arbitrary 8bit data.





More information about the Python-list mailing list