By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,067 Members | 1,818 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,067 IT Pros & Developers. It's quick & easy.

Problem Passing an Array to a COM object

Sepidar
P: 7
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?

Expand|Select|Wrap|Line Numbers
  1. Array msgs=new string[0];
  2. int n=0;
  3.  
  4. //problem occurs here
  5. controller.Compute_CurrentPlan(ref n, ref msgs); 
Jan 27 '09 #1
Share this Question
Share on Google+
6 Replies


vekipeki
Expert 100+
P: 229
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.
Jan 27 '09 #2

Sepidar
P: 7
@vekipeki
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 misundrestandings.

Method signatur is something like this:
Expand|Select|Wrap|Line Numbers
  1. Compute_CurrentPlan(ref int nMessages, ref System.Array messages)
Jan 27 '09 #3

Sepidar
P: 7
mmm,seems that editing old posts is not enabled in this forum. By the way, even this version wont work

Expand|Select|Wrap|Line Numbers
  1. Array msgs=new string[100]; 
  2. int n=100; 
  3.  
  4. //problem occurs here 
  5. controller.Compute_CurrentPlan(ref n, ref msgs);  
Jan 27 '09 #4

vekipeki
Expert 100+
P: 229
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:
Expand|Select|Wrap|Line Numbers
  1. string[] msgs = new string[100]; 
  2. int n=99;
?
Jan 27 '09 #5

Sepidar
P: 7
@vekipeki
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.CreateInstance 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:
Expand|Select|Wrap|Line Numbers
  1. RAS400.HECRASControllerClass controller = new RAS400.HECRASControllerClass();
  2. controller.Project_Open(textBox1.Text);
  3. Array msgs = Array.CreateInstance(typeof(string), new int[] { 100 }, new int[] { 1 });
  4. int n=100;
  5. controller.Compute_CurrentPlan(ref n, ref msgs);
and code worked fine! ;)
Jan 27 '09 #6

P: 3
Hello
I Am trying to call the same COM object in unmanaged C++. Have either of you two gens done that ?
Feb 12 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.