none
Passing two dimentional array to/from ATL Com object function to/from C# RRS feed

  • Question

  • I have a code in com object like this

    interface IMath{

    id(1) HRESULT Sum([in, out] SAFEARAY* arr1,  [out, retval] LONG* sum)

    }

    and in c#:

    IMath math = new ...

    int [,] arr2 = {{..},....}

    int sum = math.Sum(arr2)

    but that code does not compile and gets a error about VOID or VOID* for arr1 and ...

    Please help.

    Thursday, September 6, 2012 1:51 PM

Answers

  • Hello Ali.Drn,

    1. In order that a SAFEARRAY parameter (for a COM method) be successfully interpreted in managed code, its type needs to be declared, e.g. :

    [id(1), helpstring("method Sum")] HRESULT Sum([in,out] SAFEARRAY(LONG)* arr1, [out,retval] LONG* sum);

    2. Only then can the C# type library importer properly interpret the "arr1" parameter as :

    int Sum(ref System.Array arr1);

    3. However, in this case, you need to pass a System.Array type object as parameter :

    int [,] arr2 = {{1, 2}, {3, 4}};
    Array myarr = (Array)arr2;
    int sum = math.Sum(ref myarr);

    4. There are ways to make the signature of "Sum()" appear as :

    int Sum(ref int[,] arr1);

    which would be more intuitive for you (based on the sample code you provided).

    However, you will need to manually create the import assembly (using TLBIMP.EXE), disassemble the import assembly file into an intermediate language file (a text file) using ILDASM.EXE, modify the intermediate language file and then re-assemble it using ILASM.EXE. It is quite a complicated process.

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Proposed as answer by Mike FengModerator Wednesday, September 12, 2012 6:01 AM
    • Marked as answer by Ali.Drn Thursday, September 13, 2012 12:02 PM
    Tuesday, September 11, 2012 11:10 AM

All replies

  • Hi Ali,

    Welcome to the MSDN Forum.

    How do you declare this API in C#?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, September 7, 2012 9:35 AM
    Moderator
  • Hello Ali.Drn,

    1. In order that a SAFEARRAY parameter (for a COM method) be successfully interpreted in managed code, its type needs to be declared, e.g. :

    [id(1), helpstring("method Sum")] HRESULT Sum([in,out] SAFEARRAY(LONG)* arr1, [out,retval] LONG* sum);

    2. Only then can the C# type library importer properly interpret the "arr1" parameter as :

    int Sum(ref System.Array arr1);

    3. However, in this case, you need to pass a System.Array type object as parameter :

    int [,] arr2 = {{1, 2}, {3, 4}};
    Array myarr = (Array)arr2;
    int sum = math.Sum(ref myarr);

    4. There are ways to make the signature of "Sum()" appear as :

    int Sum(ref int[,] arr1);

    which would be more intuitive for you (based on the sample code you provided).

    However, you will need to manually create the import assembly (using TLBIMP.EXE), disassemble the import assembly file into an intermediate language file (a text file) using ILDASM.EXE, modify the intermediate language file and then re-assemble it using ILASM.EXE. It is quite a complicated process.

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Proposed as answer by Mike FengModerator Wednesday, September 12, 2012 6:01 AM
    • Marked as answer by Ali.Drn Thursday, September 13, 2012 12:02 PM
    Tuesday, September 11, 2012 11:10 AM
  • Thanks. My code becomes like this:

    in *.idl file:

    [
        object,
        uuid(...),
        dual,
        nonextensible,
        pointer_default(unique),
    ]
    interface IMath : IDispatch{
        [id(1)]  HRESULT Sum([in,out] SAFEARRAY(LONG) *arr1, [out,retval] LONG* sum);
    };

    in *.cpp file:

    STDMETHODIMP CCOMDemo::Sum(SAFEARRAY ** arr1, LONG* sum)
    {
        // TODO: Add your implementation code here
       ...

       .

       .   
        return S_OK;
    }

    Thursday, September 13, 2012 12:06 PM