Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

interop : passing C# object pointer to a C++ client.

874 views
Skip to first unread message

Ajay K Verma

unread,
Feb 4, 2003, 5:54:17 AM2/4/03
to
Hi All,

I have got an very interesting problem.
What i am doing is described below step by step:

1. I made a C# COM, which has a class(say CSCLASS) and few
methods defined in it.

2. Then i make a C++ COM, Which has a class (say CPPCLASS)
and a method(say cppFunc) which takes the pointer to the
object of class CSCLASS.

3. Then i make an C# application.

4. In C# application i instantiate class CSCLASS(oCSCLASS)
and CPPCLASS(oCPPCLASS).

5. Then on oCPPCLASS i call the method cppFunc and pass
the pointer of oCSCLASS as argument to it.

ALL THIS IS OK.

PROBLEM STARTS NOW.

*******************************************************
When i use this object pointer(oCSCLASS) in C++ code and
try to call member methods of class CSCLASS i do not see
any method over there.

My intellisense does not show me the methods of class
CSCLASS whost object pointer i am having here.
*******************************************************

Is it possible to pass the object pointers and work on
that.

Please send your valuble suggestions to me.

Regards.

David Stucki [MS]

unread,
Feb 4, 2003, 3:57:57 PM2/4/03
to
Declare a public interface in your C# library (ICSClass perhaps) with all
the methods you're interested in exposing to COM and have CSClass implement
it. Then the COM client can access your methods using an ICSClass*.

David Stucki
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Ajay K Verma

unread,
Feb 4, 2003, 10:49:50 PM2/4/03
to
Hi There,
I am doing the same thing.

Problem is i want to work on my object,
which i am creating in C# application and
passing to C++ client.

I dont want to create another instance
using CoCreateInstance in C++ client.

>.
>

Ajay Verma

unread,
Feb 4, 2003, 10:52:37 PM2/4/03
to

Hi There,
I am doing the same thing.

Problem is i want to work on my object,
which i am creating in C# application and
passing to C++ client.

I dont want to create another instance
using CoCreateInstance in C++ client.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

David Stucki [MS]

unread,
Feb 5, 2003, 4:31:18 PM2/5/03
to
As far as I understand your issue I believe I've gotten the same scenario
working in my tests. Here's what I have.

I created a C# library with an interface and a class that implements that
interface:

namespace CSLib
{
public interface ICSClass
{
void TestMethod(string s);
}
public class CSClass : ICSClass
{
public CSClass()
{
Console.WriteLine("CSClass Constructor");
}

public void TestMethod(string s)
{
Console.WriteLine(s);
}
}
}


I made sure that this library is registered for COM interop. I then
created a C++ COM DLL (using ATL) with an ATL Simple object named CppClass
in it. I added a method named SetObject on the custom interface that took
an IDispatch*:

[ idl attributes omitted here... ]
interface ICppClass : IDispatch{
[id(1), helpstring("method SetObject")] HRESULT SetObject(IDispatch*
pCSClass);
};

I added an #import directive to the class header file:

#import "C:\public\projects\newsgroup\cslib\bin\debug\cslib.tlb"
no_namespace

In the C++ ATL object I implemented the SetObject method like this:

STDMETHODIMP CCppClass::SetObject(IDispatch* pCSClassDisp)
{
ICSClass *pICSClass = NULL;
HRESULT hr = pCSClassDisp->QueryInterface(__uuidof(ICSClass),
(void**)&pICSClass);
if(SUCCEEDED(hr))
{
pICSClass->TestMethod(_bstr_t("Calling TestMethod"));
pICSClass->Release();
}

return hr;
}


Then I created a C# console application. I added references to the CSLib
and the C++ COM object in the console application. Here's the code in the
main function that creates the C# class from the library, creates a C++ COM
Object, passes the C# class through a method on the C++ COM Object, and the
C++ object calls a method on the C# object.


[STAThread]
static void Main(string[] args)
{
//Create a C# component
CSLib.ICSClass csclass = new CSLib.CSClass();
//Create the C++ COM Object
CPPObjLib.ICppClass cppclass = new CPPObjLib.CppClassClass();
//Call a method on the COM object that takes the C# object as a parameter.
cppclass.SetObject(csclass);
}

I also worked out a solution where the ATL COM object's interface
explicitly takes an ICSClass* instead of an IDispatch(and then calling
QueryInterface). This method required a little more modification of the
IDL file so I choose to use the IDispatch solution here.

If you have follow up questions please be as specific as possible giving
any code, or errors you're having trouble with.

Hope this helps,

thosar....@gmail.com

unread,
Mar 3, 2015, 11:13:02 PM3/3/15
to
Hey, david could you please send me the project file of above code
i'm badly stuck in doing same thing. Your support is appreciated
thank you
0 new messages