Forum Discussion

Eppo's avatar
Eppo
New Contributor
10 years ago
Solved

Call a c# function from testcomplete with a int[] parameter

I'm trying to call a c# library function from within my testcomplete script (using delphiscript). The c# function is structured as follows: (built target .net 4.0)

public object CSharpFunction(int[] idList, Enum enumParameter)
{
    //Do something with the array
}

Im trying to call this function like this:

function CallManagedFunction(
   CSharpLibrary: OleVariant;   singleId: integer;   enumParameter: integer);
var functionResult: OleVariant;   arrayOfIntegers : Array[0..1] of Integer;
begin 
   arrayOfIntegers[0] := singleId;   functionResult := CSharpLibrary.CSharpFunction(arrayOfIntegers, enumParameter);  
   //Parse result object
end; 

This results in a Exception with the message "The parameter is incorrect". My suspicion is that the delphi array is not marshalled correctly by the .net environment. Am i missing something in either the function declaration or in the delph call? any help is appreciated

  • I see you already got an answer on Stack Overflow, but I'll recap it here just in case - you need to create .NET arrays and enums explicitly by using the dotNET object:

     

    function CallManagedFunction(
       CSharpLibrary: OleVariant;
       singleId: integer; 
       enumParameter: integer);
    var functionResult, arrType, arrayOfIntegers, enumType, enumValue;
    begin
      // Create a .NET array with 1 element
      arrType := dotNET.System.Type.GetType('System.Int32');
      arrayOfIntegers := dotNET.System.Array.CreateInstance(arrType, 1);
      arrayOfIntegers.SetValue(singleId, 0);
    
      // Convert a number to an enumeration member
    // TODO: replace 'System.DayOfWeek' with your enum type
    enumType := dotNET.System.Type.GetType('System.DayOfWeek'); enumValue := dotNET.System.Enum.ToObject_3(enumType, enumParameter); functionResult := CSharpLibrary.CSharpFunction(arrayOfIntegers, enumValue); // Parse result object end;

     

3 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    I see you already got an answer on Stack Overflow, but I'll recap it here just in case - you need to create .NET arrays and enums explicitly by using the dotNET object:

     

    function CallManagedFunction(
       CSharpLibrary: OleVariant;
       singleId: integer; 
       enumParameter: integer);
    var functionResult, arrType, arrayOfIntegers, enumType, enumValue;
    begin
      // Create a .NET array with 1 element
      arrType := dotNET.System.Type.GetType('System.Int32');
      arrayOfIntegers := dotNET.System.Array.CreateInstance(arrType, 1);
      arrayOfIntegers.SetValue(singleId, 0);
    
      // Convert a number to an enumeration member
    // TODO: replace 'System.DayOfWeek' with your enum type
    enumType := dotNET.System.Type.GetType('System.DayOfWeek'); enumValue := dotNET.System.Enum.ToObject_3(enumType, enumParameter); functionResult := CSharpLibrary.CSharpFunction(arrayOfIntegers, enumValue); // Parse result object end;

     

    • Eppo's avatar
      Eppo
      New Contributor

      I marked this as solved to prevent dual effort since you saw this on stack overflow. Unfortunately I dont seem to be able to get your example to work. I updated my question on stack overflow to show the problem a little more accurately (i edited my code to use your suggestion) any chance you could take another look?