MFC ColeSafeArray Usage
My apologies if this isn't the best place to ask.
I'm writing some VC++ code to control Microsoft MapPoint and =
programmatically import some data. The main "magic function" that makes=
=
the MapPoint world go around is DisplayDataMap, conveniently documented =
at =
the following URL.
http://msdn2.microsoft.com/en-us/aa562363.aspx
I'm trying to call this function with the geoDataMapTypeMultipleSymbol m=
ap =
type, and that involves passing some additional arrays in the =
"ArrayOfCustomValues" and "ArrayOfPushpinSymbols" values. I have tried =
=
dozens of variations on the call, but it keeps throwing an OLE error "Th=
e =
parameter is incorrect."
I googled for several hours and came across an article talking about how=
=
to call the code with C#. The examples given show the parameters being =
=
passed as "object[]" arrays, not "string[]" arrays. After some testing =
in =
C#, I found that the parameters do indeed need to be passed as "object[]=
" =
arrays, or the C# code will throw the same incorrect parameter error.
My code that converts the vector<wstring> arrays to variant arrays is as=
=
follows:
VARIANT wsvec_to_varray(std::vector<std::wstring> vec) {
COleSafeArray varArray;
DWORD elements[] = { vec.size() };
varArray.Create(VT_BSTR, 1, elements);
for (long i = 0; i < vec.size(); i++) {
CString cs(vec[i].c_str());
VARIANT v;
VariantInit(&v);
V_VT(&v) = VT_BSTR;
v.bstrVal = cs.AllocSysString();
varArray.PutElement(&i, v.bstrVal);
SysFreeString(v.bstrVal);
VariantClear(&v);
}
return varArray.Detach();
}
This code does indeed produce a variant array that the code inspector is=
=
perfectly happy with, but I believe the VT_BSTR array item type means th=
is =
array is like a "string[]" array, not an "object[]" array. Does anyone =
=
know the right way to create an "object[]" compatible variant array =
containing a list of strings?
Eric