Learn how to a build a cloud-first strategyRegister Now


C# COM Server returning string[] to classic ASP

Posted on 2009-04-21
Medium Priority
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
  • 3
  • 2
LVL 21

Accepted Solution

MogalManic earned 1500 total points
ID: 24203378
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

ID: 24204550
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

MogalManic earned 1500 total points
ID: 24205859

Author Comment

ID: 24216209
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

ID: 31572704
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.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

810 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