using return values from a c# .net made component build as com+

449 views Asked by At

so far I made a component in C# .NET 4 and use System.EnterpriseServices to make it COM visible. I want to develop business methods in C#, but I still need to access them from classic ASP (vbscript).

So far so good, everything works fine (exept function overloading :)). Now I made a test class to get more expirience with return code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.EnterpriseServices;
using System.Management;

namespace iController
{
  /// <summary>
  /// The tools class provides additional functions for general use in out of context to other classes of the iController.
  /// </summary>
  public class tools :ServicedComponent
  {

    #region publich methods

    public bool TestBoolean()
    {
      return true;
    }

    public string TestString()
    {
      return "this is a string";
    }

    public int TestInteger()
    {
      return 32;
    }

    public double TestDouble()
    {
      return 32.32;
    }

    public float TestFloat()
    {
      float ret = 2 ^ 16;
      return ret;
    }

    public string[] TestArray()
    {
      string[] ret = {"0","1"};
      return ret;
    }

    public int[][] TestJaggedArray()
    {
      int[][] jaggedArray = new int[3][];
      jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
      jaggedArray[1] = new int[] { 0, 2, 4, 6 };
      jaggedArray[2] = new int[] { 11, 22 };
      return jaggedArray;
    }

    public Dictionary<string, string> TestDictionary()
    {
      Dictionary<string, string> ret = new Dictionary<string,string>();
      ret.Add("test1","val1");
      ret.Add("test2","val2");
      return ret;
    }

    #endregion

  }
}

Then I just made a simple vbscript file to run it with cscript.exe for testing porpuse.

Dim oTools : Set oTools = CreateObject("iController.tools")

WScript.StdOut.WriteLine TypeName(oTools.TestBoolean()) & " - " & oTools.TestBoolean()
WScript.StdOut.WriteLine TypeName(oTools.TestString()) & " - " & oTools.TestString()
WScript.StdOut.WriteLine TypeName(oTools.TestInteger()) & " - " & oTools.TestInteger()
WScript.StdOut.WriteLine TypeName(oTools.TestDouble()) & " - " & oTools.TestDouble()
WScript.StdOut.WriteLine TypeName(oTools.TestFloat()) & " - " & oTools.TestFloat()

test = oTools.TestArray()
WScript.StdOut.WriteLine TypeName(test)
WScript.StdOut.WriteLine UBound(test)

For i = 0 To UBound(test)
  WScript.StdOut.WriteLine test(i)
Next

For Each item IN test
  WScript.StdOut.WriteLine item
Next

test = oTools.TestJaggedArray()
WScript.StdOut.WriteLine TypeName(test)
For Each item IN test
  WScript.StdOut.WriteLine test & " - " & test.Item(item)
Next

test = oTools.TestDictionary()
WScript.StdOut.WriteLine TypeName(test)
For Each item IN test
  WScript.StdOut.WriteLine test & " - " & test.Item(item)
Next

What works fine:

string, int, foat, double

When it comes to array, jaggedarray or dictionaries I get a type mismatch. VarType is 13 object for the dictionary e.g. but this dict seems to be different then the Scripting.Dictionary.

I checked codeproject.com and stackoverflow all day and didn't find any hints exept some thread on stackoverflow where someone mentioned there is a need to created a IDispatch interface.

So anyone ever had the same issue and can help me or give me some hints I can go on with?

2

There are 2 answers

0
YvesR On BEST ANSWER
  1. Lesson: let .NET handle your MarshalAs :)

    public object[] Read()    {      var retVal = new object[] {1,2,3};      return retVal;    }
    

This is a Array which I can access from vbscript. The clue is that is has to be object[].

Now I go on work on a solution for jagged arrays and dictionaries...

2
honibis On

Passing Arrays to COM: All managed array types can be passed to unmanaged code from managed code. Depending on the managed type and the attributes applied to it, the array can be accessed as a safe array or a C-style array

Take a look at this article: http://msdn.microsoft.com/en-us/library/z6cfh6e6(v=vs.80).aspx