C# COM Server returning string[] to classic ASP

Posted on 2009-04-21
Last Modified: 2012-06-27
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));


			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

Question by:ChrisMDrew
    LVL 21

    Accepted Solution

    when you return a string array  .net COM interop is converting it to a SafeArray(string)

    You could try returning a object array (object[]) the COM interop will return (i think) a VARIANT array.


    Author Comment

    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?
    LVL 21

    Assisted Solution


    Author Comment

    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>();
        // 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


    Author Closing Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now