Advertisement

05.24.2006 at 08:36AM PDT, ID: 21862303
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.2

How do you pass an array to a COM object using interop?

Asked by wayside in C# Programming Language

Tags: , , ,

I need to extend an existing COM object to add a function that takes as input an array of bytes, which is readonly. I haven't done COM in a while and I don't remember the gory details so hopefully someone here can help me fill in the blanks.

The interface definition looks something like this:

[object, dual, uuid("12345678-1234-1234-1234-123456789ABC") ]
__interface IMyFoo {
  [id(1), helpstring("method foo1")] HRESULT foo1([in] BSTR str, [out,retval] BSTR* result);
  [id(2), helpstring("method foo2")] HRESULT foo2([in] BYTE buf[], [in] LONG numBytes, [out,retval] BSTR* result);  // <-- #1 below
};

[ coclass, uuid("87654321-4321-4321-4321-CBA987654321"), default(IMyFoo) ]
class CMyFoo : public IMyFoo
{
public:
  CMyFoo(void);
  ~CMyFoo void);
  STDMETHOD(foo1)(BSTR str, LONG* result);
  STDMETHOD(foo2)(BYTE []buf, LONG numBytes, BSTR* result);  // <--#1 below
};

The implementation of foo2 is conceptually something like:

STDMETHODIMP CMyFoo::foo2(BYTE []buf, LONG numBytes, BSTR* result)
{

  int sum = 0;
  for (int i=0; i<numBytes; i++) {
     // process buf here
     sum +=  buf[i]; // <--- #2 below
  }
 
  char outstr[50];
  sprintf(outstr, "answer is %ld", sum);

  *result = ::SysAllocString((OLECHAR *) outstr);

  return S_OK;
}

From the C# side, I want to do something like:

byte[] buf = new byte[1234];
// buf gets data in it, not important how

mycom.CMyFooClass myfoo = new mycomCMyFooClass();
string sum= myfoo.foo2(buf, buf.length);  // <--- #3 below

I've already got everything working to call the foo1() function in C#, but I need help with

#1) setting up the interface to be able to pass an array of bytes in foo2()
#2) accessing that array within the COM object function foo2() - don't need to modify it, just access it
#3) how do I call foo2() from the C# side to pass the array

Thanks!Start Free Trial
 
Keywords: How do you pass an array to a COM o…
 
Loading Advertisement...
 
[+][-]05.24.2006 at 11:19AM PDT, ID: 16754270

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C# Programming Language
Tags: interop, array, com, pass
Sign Up Now!
Solution Provided By: gbzhhu
Participating Experts: 1
Solution Grade: B
 
 
[+][-]05.24.2006 at 12:39PM PDT, ID: 16755208

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.25.2006 at 02:59AM PDT, ID: 16759310

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.20.2006 at 06:12PM PDT, ID: 16947771

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]06.21.2006 at 06:06AM PDT, ID: 16950921

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32