Copy 1D safeArray to 2D safearray
Author |
Message |
Quick Functi #1 / 6
|
Copy 1D safeArray to 2D safearray
I have a 1D COleSafeArray, and a 2D COleSafeArray. They could be any type. I want to copy the 1D array to a column of the 2D. What's the best way to do? here is my code: for (long i = ... VARIANT var; my1DArray.GetElement(&i, &var); my2DArray.PutElement((some index), &var); Quote: }
Is works for interger and double, but does not work for BSTR; Why? I have another question: If I have a BSTR type COleSafeArray, which one should I use: BSTR bstr; myArray.putElement(&i, &bstr) or BSTR bstr; myArray.putElement(&index, bstr)? Thanks, qq
|
Mon, 10 Oct 2005 04:05:19 GMT |
|
|
Igor Tandetni #2 / 6
|
Copy 1D safeArray to 2D safearray
Quote: > I have a 1D COleSafeArray, and a 2D COleSafeArray. They could be any type. > I want to copy the 1D array to a column of the 2D. What's the best way to do? > here is my code: > for (long i = ... > VARIANT var; > my1DArray.GetElement(&i, &var); > my2DArray.PutElement((some index), &var); > } > Is works for interger and double, but does not work for BSTR; Why?
Does not work in what way? Note that you are leaking resources here. SafeArrayGetElement places a copy of the actual element into your VARIANT. SafeArrayPutElement makes another copy and places it into destination array. You are left with a VARIANT containing a valid copy of the data, which you promptly leak. Call VariantClear(&var) after PutElement Quote: > I have another question: > If I have a BSTR type COleSafeArray, which one should I use: > BSTR bstr; > myArray.putElement(&i, &bstr) or > BSTR bstr; > myArray.putElement(&index, bstr)?
The second. BSTR, IUnkown* and IDispatch* are pointers already and don't require an extra level of indirection in SafeArrayPutElement call. -- With best wishes, Igor Tandetnik "For every complex problem, there is a solution that is simple, neat, and wrong." H.L. Mencken
|
Mon, 10 Oct 2005 06:11:29 GMT |
|
|
Quick Functi #3 / 6
|
Copy 1D safeArray to 2D safearray
Thank you for your post. What if I don't know the type inside the COleSafeArray? My basic question is how to copy a selected part of a COleSafeArray to another COleSafeArray? Does this work: for (long i = ... void *ptr; my1DArray.PtrOfIndex(&i, &ptr); my2DArray.PutElement((some index), ptr); Quote: }
Thank you very much. qq Quote:
> > I have a 1D COleSafeArray, and a 2D COleSafeArray. They could be any > type. > > I want to copy the 1D array to a column of the 2D. What's the best way > to do? > > here is my code: > > for (long i = ... > > VARIANT var; > > my1DArray.GetElement(&i, &var); > > my2DArray.PutElement((some index), &var); > > } > > Is works for interger and double, but does not work for BSTR; Why? > Does not work in what way? > Note that you are leaking resources here. SafeArrayGetElement places a > copy of the actual element into your VARIANT. SafeArrayPutElement makes > another copy and places it into destination array. You are left with a > VARIANT containing a valid copy of the data, which you promptly leak. > Call VariantClear(&var) after PutElement > > I have another question: > > If I have a BSTR type COleSafeArray, which one should I use: > > BSTR bstr; > > myArray.putElement(&i, &bstr) or > > BSTR bstr; > > myArray.putElement(&index, bstr)? > The second. BSTR, IUnkown* and IDispatch* are pointers already and don't > require an extra level of indirection in SafeArrayPutElement call.
|
Mon, 10 Oct 2005 14:09:35 GMT |
|
|
Igor Tandetni #4 / 6
|
Copy 1D safeArray to 2D safearray
No, that would break on BSTR, IUnknown* and IDispatch*. You will have to special-case them. Something like this should work: VARTYPE vt = V_VT(my1DArray); bool bDereference = (vt == VT_BSTR || vt == VT_UNKNOWN || vt == VT_DISPATCH); for (long i = ...) { void *ptr; my1DArray.PtrOfIndex(&i, &ptr); if (bDereference) { ptr = *(void**)ptr; } my2DArray.PutElement((some index), ptr); Quote: }
-- With best wishes, Igor Tandetnik "For every complex problem, there is a solution that is simple, neat, and wrong." H.L. Mencken
Quote: > What if I don't know the type inside the COleSafeArray? My basic > question is how to copy a selected part of a COleSafeArray to another > COleSafeArray? Does this work: > for (long i = ... > void *ptr; > my1DArray.PtrOfIndex(&i, &ptr); > my2DArray.PutElement((some index), ptr); > }
|
Mon, 10 Oct 2005 21:45:21 GMT |
|
|
Quick Functi #5 / 6
|
Copy 1D safeArray to 2D safearray
It works! Thank you very much! Are BSTR, IUnknown*, and IDispatch* the only cases we should pay attention here? Thanks again, qq Quote:
> No, that would break on BSTR, IUnknown* and IDispatch*. You will have to > special-case them. Something like this should work: > VARTYPE vt = V_VT(my1DArray); > bool bDereference = (vt == VT_BSTR || vt == VT_UNKNOWN || vt == > VT_DISPATCH); > for (long i = ...) > { > void *ptr; > my1DArray.PtrOfIndex(&i, &ptr); > if (bDereference) > { > ptr = *(void**)ptr; > } > my2DArray.PutElement((some index), ptr); > } > -- > With best wishes, > Igor Tandetnik > "For every complex problem, there is a solution that is simple, neat, > and wrong." H.L. Mencken
> > What if I don't know the type inside the COleSafeArray? My basic > > question is how to copy a selected part of a COleSafeArray to another > > COleSafeArray? Does this work: > > for (long i = ... > > void *ptr; > > my1DArray.PtrOfIndex(&i, &ptr); > > my2DArray.PutElement((some index), ptr); > > }
|
Tue, 11 Oct 2005 00:56:39 GMT |
|
|
Igor Tandetni #6 / 6
|
Copy 1D safeArray to 2D safearray
Quote: > Are BSTR, IUnknown*, and IDispatch* the only cases we should pay
attention here? According to SafeArrayPutElement documentation, yes, they are. -- With best wishes, Igor Tandetnik "For every complex problem, there is a solution that is simple, neat, and wrong." H.L. Mencken
|
Tue, 11 Oct 2005 01:34:03 GMT |
|
|
|