Link to home
Start Free TrialLog in
Avatar of ChrisMDrew
ChrisMDrew

asked on

C# COM Server returning string[] to classic ASP

I have had to write a C# COM Server using .NET to service an existing classic ASP web application.  So far so good in that I have been able to call simply functions which return say a single string or integer.

I have a call however that needs to return an array of strings.  The function is shown below.  There appears to be an issue with the marshalling of string[] back to the ASP page as when I try to iterate through the entries using a for loop I get a type mis-match.

How can I return an array of strings from this function?
public string[] EnumAssets(int instanceID, int locationID)
{
   List<string> listValues = new List<string>();
 
   // First ensure that the database is open and the user logged in
   if (ValidateOpenAndLoggedIn() == 0)
   {
	AuditWizardDataAccess awDataAccess = new AuditWizardDataAccess();
	AssetList listAssets = new AssetList(awDataAccess.GetAssets(locationID, AssetGroup.GROUPTYPE.userlocation ,false), true);
 
	// Iterate through the returned list of Asset objects and format them for return as strings
	foreach (Asset asset in listAssets)
	{
		if (asset.Auditable)
			listValues.Add(Pack(asset.Name, ICON_PERIPH, true, asset.AssetID));
		else
			listValues.Add(Pack(asset.Name, ICON_ASSET, true, asset.AssetID));
	}
   }
 
   // Convert the list to an array of strings and return it
   string[] returnValues = listValues.ToArray();
   return returnValues;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of MogalManic
MogalManic
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ChrisMDrew
ChrisMDrew

ASKER

It appears according to that article that ALL type[] arguments are marshalled as SafeArray(type).  Given that object is marshalled as VARIANT I assume that it would be ,arshalled as SafeArray(VARIANT).  I'm not a VBScript expect so I'm not certain what VBScript would actually expect but I don't think it is a SafeArray.

Any ideas how to get this marshalled in such a way that my VBScript code which just declares a Dim can access the returned strings?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
After lots of experimenting I have arrived at a solution which I thought I would share.  It may not be the most tidy and is based on other code I found on the Internet but I spent so long searching that I thought it best to make this available here.

Even if you return object[] from a C# COM Server the actual elements of the array are still not determinable by VBScript.  What you seem to have to do is to return an object array containing objects.

The C# code to return an array (of strings but could be any type I expect) is shown below.  I have tested this using VBScript in classic ASP and the array can be accessed as expected and all array functions work.  While not an elegant solution it certainly works and no other changes I have made resulted in being able to acces the array.


public object[] EnumAssets(int instanceID, int locationID)
{
    List<string> listValues = new List<string>();
    listValues.Add("AAAA");
    listValues.Add("BBBB");
 
    // Return the list as an array of objects
    return ConvertToObjectArray(listValues);
}
 
 
 
/// <summary>
/// This is ued to convert a list of strings passed to an array of objects
/// </summary>
/// <param name="arrayIn"></param>
/// <returns></returns>
public object[] ConvertToObjectArray(List<string> listStrings)
{
	object[] arrayOut = new object[listStrings.Count];
	for (int i = 0; i < listStrings.Count; i++)
	{
		arrayOut[i] = listStrings[i];
	}
	return arrayOut;
} 

Open in new window

Thanks for your help with this - it certainly pointed me in the right direction.  I have in fact found an answer and have posted it in case others need it.