Problem Passing an Array to a COM object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sepidar
    New Member
    • Jan 2009
    • 7

    #1

    Problem Passing an Array to a COM object

    When I pass an array argument to a com object, I receive "Index out of Range" exception. I solved this in Delphi by change of start index of array from 0 to 1. But I don't know if this is the same issue in C#? And if is, how can I solve it?

    Code:
    Array msgs=new string[0];
    int n=0;
    
    //problem occurs here
    controller.Compute_CurrentPlan(ref n, ref msgs);
  • vekipeki
    Recognized Expert New Member
    • Nov 2007
    • 229

    #2
    What does your COM object expect to get from .NET? It looks strange that you are passing an empty array (length = 0)?

    Can you post your native method's signature, and explain exactly where your data should be allocated (in managed side, or in COM object)?

    You can try googling for "COM interop" for more information.

    Comment

    • Sepidar
      New Member
      • Jan 2009
      • 7

      #3
      Originally posted by vekipeki
      What does your COM object expect to get from .NET? It looks strange that you are passing an empty array (length = 0)?

      Can you post your native method's signature, and explain exactly where your data should be allocated (in managed side, or in COM object)?

      You can try googling for "COM interop" for more information.
      Length of array is not the point. Although I have to allocate my data in managed code, problem remains if I change size of array to 10 or 100 or even 1000. I correct the code to prevent misundrestandin gs.

      Method signatur is something like this:
      Code:
      Compute_CurrentPlan(ref int nMessages, ref System.Array messages)

      Comment

      • Sepidar
        New Member
        • Jan 2009
        • 7

        #4
        mmm,seems that editing old posts is not enabled in this forum. By the way, even this version wont work

        Code:
        Array msgs=new string[100]; 
        int n=100; 
          
        //problem occurs here 
        controller.Compute_CurrentPlan(ref n, ref msgs);

        Comment

        • vekipeki
          Recognized Expert New Member
          • Nov 2007
          • 229

          #5
          I solved this in Delphi by change of start index of array from 0 to 1.
          That's quite strange. Does that mean that this might work:
          Code:
          string[] msgs = new string[100]; 
          int n=99;
          ?

          Comment

          • Sepidar
            New Member
            • Jan 2009
            • 7

            #6
            Originally posted by vekipeki
            That's quite strange. Does that mean that this might work:
            Code:
            string[] msgs = new string[100]; 
            int n=99;
            ?
            Your code won't work for sure. But at last after a full working day I found the answer in MSDN. (Search: Nonzero-bound SAFEARRAY)
            Here I get solution summarily: Nonzero-bound arrays in safe code can be created using System.Array.Cr eateInstance method in cases needed. For example my problem was calling an OLE method that need a nonzero bound array and I created one like this:
            Code:
            RAS400.HECRASControllerClass controller = new RAS400.HECRASControllerClass();
            controller.Project_Open(textBox1.Text);
            Array msgs = Array.CreateInstance(typeof(string), new int[] { 100 }, new int[] { 1 });
            int n=100;
            controller.Compute_CurrentPlan(ref n, ref msgs);
            and code worked fine! ;)

            Comment

            • FariborzNouri
              New Member
              • Feb 2009
              • 3

              #7
              Hello
              I Am trying to call the same COM object in unmanaged C++. Have either of you two gens done that ?

              Comment

              Working...