Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Implementing Unmanaged Interface in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Saad - 13 Mar 2008 07:03 GMT
Hi,

I have an unmanged interface which i have exposed through COM to C#. I
have been able to implement the interface in one of the C# classes
(say Class1) as well.

But now what i want to do is that i need to pass the reference of the
object of Class1 to this COM wrapper...and then from inside that
wrapper i want to call one of the func of that class1.

What i did roughly is something like this:

/
*********************************************************************************************************/
//this is the interface that was exposed
// IExportedClassWrapper
[
    object,
    uuid("7DD5445A-A8AE-471C-A065-629C3A004362"),
    dual,    helpstring("IExportedClassWrapper Interface"),
    pointer_default(unique)
]
__interface IExportedClassWrapper : IDispatch
{
[id(4), helpstring("method CheckInterfaceOne")] HRESULT
CheckInterfaceOne(IDispatch* a, [out,retval] LONG* RetParam);
};
/
*********************************************************************************************************/

/
*********************************************************************************************************/
STDMETHODIMP CExportedClassWrapper::CheckInterfaceOne(IDispatch* a,
LONG* RetParam)
{
    // TODO: Add your implementation code here
    IExportedClassWrapper* objI = static_cast<IExportedClassWrapper*>(a);

              //THIS IS THE LINE ON WHICH I'M GETTING EXCEPTION
              //SAYING THAT THE CALLING CONVENTION IS NOT RIGHT....
    objI->MethodSampleReturn(RetParam);

    return S_OK;
}
/
*********************************************************************************************************/

/
*********************************************************************************************************/
//then the C# class implemented it like
public class Class1 : SampleCISCOATLCOMWrapper.IExportedClassWrapper
    {
        public Class1()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region IExportedClassWrapper Members

        public int CheckInterfaceOne(object a)
        {
            // TODO:  Add Class1.CheckInterfaceOne implementation
                                               //THIS IS THE METHOD
THAT I WANT TO CALL FROM COM
            return 20;
        }

        #endregion
    }
/
*********************************************************************************************************/
//and then i used this interface like this in C#

SampleCISCOATLCOMWrapper.CExportedClassWrapperClass objCl = new
SampleCISCOATLCOMWrapper.CExportedClassWrapperClass();

Class1 objClass = new Class1();

int nRet = objCl.CheckInterfaceOne(objClass);
/
*********************************************************************************************************/

this whole thing is sort of circular....:)

I think you got an idea of what i want to do...
Is it possible? :)
And how?

Note: I dont have much experience in COM ...

Thanks,
Saad.
Martin Bonner - 13 Mar 2008 11:31 GMT
> Hi,
>
[quoted text clipped - 11 lines]
> {
>         // TODO: Add your implementation code here
(Aside.  you have added your implementation - remove the TODO comment.
>         IExportedClassWrapper* objI = static_cast<IExportedClassWrapper*>(a);

I suspect your problem is here.  You are breaking the rules of COM,
and paying the price.  If you have one interface pointer ("a" in this
case), and you want to get another one from it ("objI" in this case),
the *only* legitimate way to do so is to call a->QueryInterface.  The
problem is almost certainly that the Interop code is passing in a
pointer to an IDispatch interface on some generic proxy.  If you
QueryInterface for IExportedClassWrapper it will create a more
suitable proxy, and return you that pointer - but a simple static_cast
won't cut it.

>                //THIS IS THE LINE ON WHICH I'M GETTING EXCEPTION
>                //SAYING THAT THE CALLING CONVENTION IS NOT RIGHT....
[quoted text clipped - 3 lines]
> }
> /****************************************************************/

> this whole thing is sort of circular....:)
It often is!

> I think you got an idea of what i want to do...
> Is it possible? :)
> And how?
>
> Note: I dont have much experience in COM ...
Aah!  Welcome to a world of pain :-)

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.