0

I can see a number of google results for converting string array to BSTR, but none that does it the other way around. How do I do that?

Details

I'm using VSTO + C# for writing a Word add-in. One of the functions of Word Object Model named SynonymInfo.get_SynonymList() appears to return a strange data type that I cannot iterate over (foreach fails). There is no way of getting the upper bound or length of the array either. People use this function in VBA with UBound(), which unfortunately isn't there in C#. I searched further and found that the data type of this function is BSTR.

The function itself is defined as dynamic in VSTO, so I can't really see what members are there in it. Even the QuickWatch window doesn't show any members, even though it can successfully list down the array elements.

dotNET
  • 33,414
  • 24
  • 162
  • 251
  • It is not a BSTR, it returns an array. The usual problem is that it is an array whose first index is 1 instead of 0. Cast it to (Array). Check [this post](http://stackoverflow.com/a/4808751/17034). – Hans Passant Sep 07 '15 at 13:55
  • @HansPassant:- '(Array)SynoList' threw an exception of type 'System.InvalidCastException' "Unable to cast object of type 'System.String[*]' to type 'System.String[]'." – dotNET Sep 07 '15 at 13:58
  • @HansPassant: [A CodeProject article](http://www.codeproject.com/Articles/3388/Creating-a-Dictionary-using-Word-Automation-and-Te) says that `Note on COleVariant: The COleVariant is just a wrapper MFC class for the VARIANT type. Also please note that the GetSynonymList() method returns a VARIANT, that contains a safearray of type BSTR.` – dotNET Sep 07 '15 at 14:02
  • It is exactly like I predicted. You must cast to System.Array, not string[]. – Hans Passant Sep 07 '15 at 14:06
  • @HansPassant: Thanks for the input. Although it didn't work right away (`(Array)SynoList` fails as I mentioned in my last comment), doing it like `(Array)(Object)SynoList` did the trick. – dotNET Sep 07 '15 at 14:21

0 Answers0