Inexperienced with COM arrays
-
I need pass an array of data in a variant to an API call. I'm not sure if I'm passing the data correctly as I'm new to COM. I appreciate if someone could verify the below snippet has a obvious problem when using COM arrays because the API call does not produce the intended results with the array data I passed. // generate 3 element array of floats SAFEARRAY *psa = SafeArrayCreateVector( VT_R4, 0, 3 ); float HUGEP *dp = 0; SafeArrayAccessData( psa, (void HUGEP**)&dp ); dp[0] = 6; dp[1] = 7; dp[2] = 8; // set array to 6,7,8 SafeArrayUnaccessData( psa ); VARIANT va; va.vt = VT_ARRAY | VT_R4; va.parray = psa; // pass va to API that takes a variant Blah( va ); SafeArrayDestroy( psa );
-
I need pass an array of data in a variant to an API call. I'm not sure if I'm passing the data correctly as I'm new to COM. I appreciate if someone could verify the below snippet has a obvious problem when using COM arrays because the API call does not produce the intended results with the array data I passed. // generate 3 element array of floats SAFEARRAY *psa = SafeArrayCreateVector( VT_R4, 0, 3 ); float HUGEP *dp = 0; SafeArrayAccessData( psa, (void HUGEP**)&dp ); dp[0] = 6; dp[1] = 7; dp[2] = 8; // set array to 6,7,8 SafeArrayUnaccessData( psa ); VARIANT va; va.vt = VT_ARRAY | VT_R4; va.parray = psa; // pass va to API that takes a variant Blah( va ); SafeArrayDestroy( psa );
Did you try to pass it as a reference instead?
va.vt = VT_ARRAY | VT_BYREF | VT_R4;
va.pparray= &psa;Your code seems to work fine other than that! What API are you using? the way it expects to receive your data is also important. One good thing about getting older, you don't lose the ages you've been!
-
Did you try to pass it as a reference instead?
va.vt = VT_ARRAY | VT_BYREF | VT_R4;
va.pparray= &psa;Your code seems to work fine other than that! What API are you using? the way it expects to receive your data is also important. One good thing about getting older, you don't lose the ages you've been!
Ernest Laurentin wrote: Did you try to pass it as a reference instead? Yup, didn't work too. The API is a Avid SoftImage XSI v2 SDK. It has virtually ZERO documentation so I'm guessing how to call it by looking at headers and vbscript examples. The vbscript version works like this
Blah( array(5,6,7) )
The C++ version is defined asvirtual HRESULT STDMETHODCALLTYPE Blah( /* [in] */ VARIANT in_pNewEffPos );
The worst part is the API runs successfully (HR succeeded) but the results are wrong. I'm not well experienced enough with COM or I would have condemn it as a bug with their API straight away. -
Ernest Laurentin wrote: Did you try to pass it as a reference instead? Yup, didn't work too. The API is a Avid SoftImage XSI v2 SDK. It has virtually ZERO documentation so I'm guessing how to call it by looking at headers and vbscript examples. The vbscript version works like this
Blah( array(5,6,7) )
The C++ version is defined asvirtual HRESULT STDMETHODCALLTYPE Blah( /* [in] */ VARIANT in_pNewEffPos );
The worst part is the API runs successfully (HR succeeded) but the results are wrong. I'm not well experienced enough with COM or I would have condemn it as a bug with their API straight away.Sorry it took me long to write you! Did you try to send "long" or "short" array? You example here both integer value not float. Maybe the API accepts integer value not float! One good thing about getting older, you don't lose the ages you've been!
-
Sorry it took me long to write you! Did you try to send "long" or "short" array? You example here both integer value not float. Maybe the API accepts integer value not float! One good thing about getting older, you don't lose the ages you've been!
Ernest Laurentin wrote: Maybe the API accepts integer value not float! Thanks for the reply. Quite impossible. It should be taking either float or double because in the UI, one can enter decimal places for the values. They can't be using fixed point maths. But I will try again just to make doubly sure.
-
Ernest Laurentin wrote: Maybe the API accepts integer value not float! Thanks for the reply. Quite impossible. It should be taking either float or double because in the UI, one can enter decimal places for the values. They can't be using fixed point maths. But I will try again just to make doubly sure.
I see... but I still think if you can make it work with VBScript, you can implement the same in C++. Did you try to reproduce simple code in VB? For example:
dim vbArray(3)
dim nloop
for nloop = 0 to 3
vbArray(nloop) = nloop+4 ' or other value
next
Blah( vbArray )The reason I will do it like this instead to do just call:
Blah( array(5,6,7) )
is that I am not really sure how VB interprets that line. But you should be able to reproduce the same type of array (dim vbArray as "type"). Good luck! One good thing about getting older, you don't lose the ages you've been! -
I see... but I still think if you can make it work with VBScript, you can implement the same in C++. Did you try to reproduce simple code in VB? For example:
dim vbArray(3)
dim nloop
for nloop = 0 to 3
vbArray(nloop) = nloop+4 ' or other value
next
Blah( vbArray )The reason I will do it like this instead to do just call:
Blah( array(5,6,7) )
is that I am not really sure how VB interprets that line. But you should be able to reproduce the same type of array (dim vbArray as "type"). Good luck! One good thing about getting older, you don't lose the ages you've been! -
That's a very good idea you have there. Unfortunately (fortunately?), it's weekend party and I have to wait till next week to try it out. Thanks for your time. Very appreciated.
Fortunately for you, I guess! Keep in touch! Have great time! One good thing about getting older, you don't lose the ages you've been!
-
I see... but I still think if you can make it work with VBScript, you can implement the same in C++. Did you try to reproduce simple code in VB? For example:
dim vbArray(3)
dim nloop
for nloop = 0 to 3
vbArray(nloop) = nloop+4 ' or other value
next
Blah( vbArray )The reason I will do it like this instead to do just call:
Blah( array(5,6,7) )
is that I am not really sure how VB interprets that line. But you should be able to reproduce the same type of array (dim vbArray as "type"). Good luck! One good thing about getting older, you don't lose the ages you've been!Ah, I spotted an mistake It should be
Dim vbArray(2)
instead ofDim vbArray(3)
because for reasons I cannot fathom, vbArray(2) means a 3 element array. Anyway, the vbscript in the form above works. I am unable to explicitly declare the dim type though.Dim vbArray(2) As Double
generates an "Expected end of statement" error. This seems to be more of the scripting environment problem rather than mine. X| -
Ah, I spotted an mistake It should be
Dim vbArray(2)
instead ofDim vbArray(3)
because for reasons I cannot fathom, vbArray(2) means a 3 element array. Anyway, the vbscript in the form above works. I am unable to explicitly declare the dim type though.Dim vbArray(2) As Double
generates an "Expected end of statement" error. This seems to be more of the scripting environment problem rather than mine. X|Yes, the reason is VBScript is only a subset of visual basic. You can't really use that form
Dim vbArray(2) as Double
. In fact, I was suggesting you to try it in Visual Basic or VBA (I use Excel sometimes to do that! :-) As far as the syntax,Dim vbArray(3)
is the one that I used with another API and it works fine. BTW, VBScript will pass your data as an array of variant. One good thing about getting older, you don't lose the ages you've been!