I have a strange issue. I have a method:
public String[] MethodA()
{
List
result.Add(ObjectA.Instance.Name);
result.Sort();
return result.ToArray();
}
this is in a .NET 2.0 c# library which is com enabled and I am consuming in a C++ MFC application.
When I call the method in the C++ app safearray I am using to hold the results is corrupted. The Name property is a string.
If I modify the method like so:
public String[] MethodA()
{
List
result.Add("MyString");
result.Sort();
return result.ToArray();
}
then when I call it it returns the correct information and I can use the safearray with no problems.
I have tried several options for modifying the method including calling ToSting() on the name, creating a string variable first setting this to be the name, then adding the string to the list, creating a stringBuilder, adding the Name to that then adding StringBuilder.ToString() to the list, but all fail in the same way.
Does anyone have any idea why this might be? Can anyone offer any potential solution?
Thanks
Sam
sam holderPosted Apr 22, 2008, 6:47 AM
Your suggestion, although it didn't work, has made a little progress. It doesn't crash anymore. The safearray now gets recognised as a an array of IUknown*, although it should be BSTR*. I can't access the components of the array, indeed it reports it as having min of 0 and max of 0 and when I try and access index 0 I just get an empty CString back.
any further ideas?
AlanPosted Apr 18, 2008, 6:04 PM
That is very strange indeed :-/
I have no idea what could cause this but the only difference I can think of between string literals and strings held in variables is that the former are interned and the latter (usually) are not.
I'd therefore suggest that you try interning the string before passing it to the method:
http://msdn2.microsoft.com/en-us/library/system.string.intern.aspx