473,388 Members | 1,198 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Passing COM object from c# to COM causes exception

Hopefully I have the correct newsgroup for this question.

I've developed a very simple COM server in C++ as im trying to learn
COM. The COM server has an array of IUnknown interfaces (MyArray)
where im allowing the user to store COM objects (derived from
IUnknown) via get/put properties.

I have a created a COM object which is derived from IUnknown but when
i come to add the object to my array from C# i get a com exception :
"Unable to cast COM object of type 'MyListClass' to interface type
'IMyList'. This operation failed because the QueryInterface call on
the COM component for the interface with IID "

The functions to add to the array are (it only returns the first
element in the array for now!):

STDMETHODIMP CMyList::get_ArrayItem(LPUNKNOWN * pVal)
{
if (this->m_aUnkArray.size() == 0)
return E_FAIL;

pVal = this->m_aUnkArray.at(0);
return S_OK;
}

STDMETHODIMP CMyList::put_ArrayItem(LPUNKNOWN newVal)
{
this->m_aUnkArray.push_back(&newVal);
return S_OK;
}

The array storing the IUnknowns is a vector:

std::vector <IUnknown**m_aUnkArray;

....and the C# code im using to call the COM object is:
MyCOMServer comServer = new MyCOMServer ;

MyItemClass item1 = new MyItemClass();

item1.Text3 = "andy";

MyListClass list = new DCOMAPPLib.MyListClass();
list.DispArrayItem = item1; <--------
exception occurs here
Is it possible to put/get C# objects to a property which takes
IUnknown as parameter? Any advice on what im doing wrong will be
appreciated.

Thanks,

Andy

Apr 5 '07 #1
1 3479
First off, in your put_ArrayItem implementation, I don't see a call to
AddRef. You need to make this call before you set it in the array, because
you need to let the implementation know that you have a reference to it. If
you don't do this, then when all other references are released (through
calls to, coincidentally, Release) the pointer that you are holding will be
invalid.

Also, I can't tell if the get_ArrayItem is an in-out parameter or an out
parameter. If it is an in-out parameter, then you need to call Release on
the IUnknown interface passed in, and then you need to call AddRef on the
value you set pVar to.

For more information, read the section of the MSDN documentation titled
"Rules for Managing Reference Counts", located at:

http://msdn2.microsoft.com/en-us/library/ms692481.aspx

All that being said, what is the DispArrayItem property? The ArrayItem
property you define is showing that it takes IUnknown, which means it should
take ANY COM object. Is MyItemClass a COM object, or is it a native .NET
object?

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ae******@gmail.comwrote in message
news:11**********************@w1g2000hsg.googlegro ups.com...
Hopefully I have the correct newsgroup for this question.

I've developed a very simple COM server in C++ as im trying to learn
COM. The COM server has an array of IUnknown interfaces (MyArray)
where im allowing the user to store COM objects (derived from
IUnknown) via get/put properties.

I have a created a COM object which is derived from IUnknown but when
i come to add the object to my array from C# i get a com exception :
"Unable to cast COM object of type 'MyListClass' to interface type
'IMyList'. This operation failed because the QueryInterface call on
the COM component for the interface with IID "

The functions to add to the array are (it only returns the first
element in the array for now!):

STDMETHODIMP CMyList::get_ArrayItem(LPUNKNOWN * pVal)
{
if (this->m_aUnkArray.size() == 0)
return E_FAIL;

pVal = this->m_aUnkArray.at(0);
return S_OK;
}

STDMETHODIMP CMyList::put_ArrayItem(LPUNKNOWN newVal)
{
this->m_aUnkArray.push_back(&newVal);
return S_OK;
}

The array storing the IUnknowns is a vector:

std::vector <IUnknown**m_aUnkArray;

...and the C# code im using to call the COM object is:
MyCOMServer comServer = new MyCOMServer ;

MyItemClass item1 = new MyItemClass();

item1.Text3 = "andy";

MyListClass list = new DCOMAPPLib.MyListClass();
list.DispArrayItem = item1; <--------
exception occurs here
Is it possible to put/get C# objects to a property which takes
IUnknown as parameter? Any advice on what im doing wrong will be
appreciated.

Thanks,

Andy

Apr 5 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Fiona McBride | last post by:
Hi all, I have a really odd problem with some Visual Basic .NET 2003 code; I have a program that creates a number of windows which contain RichTextBox, Timers (disabled) and menus. The code...
0
by: Dino Hadzic | last post by:
Hallo csharp'ers I am trying to call a function on a COM (dll) object from managed c# code, which will send data to another node using an already existing bluetooth connection. However, I get a...
7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
8
by: Chris | last post by:
Hi, I have two froms (form1 and form2). I want to be able to pass values from form 1 to form2 and be able to use those values leter in form2. This is my code for form1 Private Sub...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
2
by: Steve | last post by:
(I posted this in the ADO.NET NG, but didn't get any responses) I've started getting this exception since installing vs2005 "COM object that has been separated from its underlying RCW cannot be...
61
by: academic | last post by:
When I declare a reference variable I initialize it to Nothing. Now I'm wondering if that best for String variables - is "" better? With Nothing I assume no memory is set aside nor GC'ed But...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.