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 / .NET Framework / Interop / December 2004

Tip: Looking for answers? Try searching our database.

VC++ Class Library and Implenting COM Interfaces??

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brian R. - 30 Nov 2004 21:07 GMT
I have had a lot of practice creating .NET COM Interop objects in C# that can
be registered and called from existing COM clients.  I am trying to perform
the same process in VC++ (without writing an ATL object) via a class library
project -- just like I would with C#.

I create a .NET VC++ class library project and there is a default managed
class object called Class1.
I add a reference to a COM interface typelibrary that contains the interface
I would like to implement.
I then try to add an implementation of a particular interface to the VC++
managed class by typing the following on the end of the class definition line:

public __gc class Class1 : public MyTypLib::IMyInterface

Given my C# experience I would expect it to offer me the ability to generate
the implementation shell of each method on the interface, but it doesn't.  
Also if I compile this, there is no error stating that I have failed to
implement the methods of IMyInterface!!  In C# I am forced to add an
implementation of each method on the interface before it compiles.

Am I doing something wrong in the syntax here?  Can you create VC++ .NET
interop objects or do you have to use ATL?

Thanks in Advance!

Signature

Brian R.

"Peter Huang" [MSFT] - 01 Dec 2004 06:01 GMT
Hi

Based on my understanding, you wants to expose the .NET class to the COM
interface, if I have any misunderstanding, please feel free to let me know.
In MC++, we can do the job below to expose the IMyInterface to the COM
client, after built we need to use the regasm to generate the tlb file for
com client to use.

using namespace System;
using namespace System::Windows::Forms;
using namespace System::Runtime::InteropServices;
namespace MCPP
{
    [InterfaceTypeAttribute(ComInterfaceType::InterfaceIsDual)]
    public __gc __interface IMyInterface
    {
        void HelloWorld();
    };
    [ClassInterface(ClassInterfaceType::None)]
    public __gc class MyClass : public IMyInterface
    {
    public:
        void HelloWorld()
        {
            Form* fm = new Form();
            fm->Show();
        }
    };
}

Here are two links you may take a look.
http://groups.google.com/groups?hl=zh-CN&lr=&c2coff=1&threadm=mTfn46d2CHA.18
76%40cpmsftngxa06&rnum=2&prev=/groups%3Fhl%3Dzh-CN%26lr%3D%26c2coff%3D1%26q%
3Dimplement%2Binterface%2Bmc%252B%252B

http://groups.google.com/groups?hl=zh-CN&lr=&c2coff=1&threadm=a5ebf44c.02123
01430.42609045%40posting.google.com&rnum=5&prev=/groups%3Fhl%3Dzh-CN%26lr%3D
%26c2coff%3D1%26q%3Dimplement%2Binterface%2Bmc%252B%252B

Although the MC++ IDE did not did all the job as C# do, but we still can do
the similar job with C#.

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Brian R. - 01 Dec 2004 16:25 GMT
You have captured the basic idea I am trying to convey, but you didn't
address the actual issue I was trying to face.

First problem:

The interface I am trying to implement is not a managed .NET interface.  The
interface my object is trying to implement is a COM interface defined in IDL
that was compiled into a COM type library.  Your example used a .NET
interface.  

Second Issue:

When I added the implementation of the interface, it did not prompt me to
add teh interface stubs.  It's fine if it doesn't offer that capability!  But
what bothers me is that the class compiles without providing a definition of
the interface methods.  This should fail.  Why doesn't it fail?

That's probably my biggest issue.  Why does the component compile if I don't
provide an implementation of all of the methods of the derived interface?  

In your example, if you had not entered an implementation of HelloWorld,
would it have compiled?

> Hi
>
[quoted text clipped - 45 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT] - 02 Dec 2004 03:57 GMT
Hi

1. Actually if we need to implement the interface in managed code, we can
just import the interface into .net and then implement it as a managed
interface once it has been imported, because the managed code will run
based on .NET runtime.
e.g.
[
    object,
    uuid("59E6670E-8626-468e-88D4-2E4A86DB25EC"),
    dual,    helpstring("IMyInterface Interface"),
    pointer_default(unique)
]
__interface IMyInterface: IDispatch
{

    [id(1), helpstring("method TestString")] HRESULT TestString(LONG x,
[out,retval] LONG* y);
};

add a reference to the com tlb file and we can get the interop assembly
with the managed IMyInterface definition.

using namespace System;
using namespace System::Windows::Forms;
using namespace System::Runtime::InteropServices;
using namespace  Interop::ATLInterface;
namespace MCPP
{
//[InterfaceTypeAttribute(ComInterfaceType::InterfaceIsIUnknown)]
//public __gc __interface IMyInterface
//{
//    void HelloWorld();
//};
[ClassInterface(ClassInterfaceType::None)]
public __gc class MyClass : public IMyInterface
{
//public:
//    void HelloWorld()
//    {
//        Form* fm = new Form();
//        fm->Show();
//    }
public:
    int TestString(int x)
    {
        return x*x*x;
    }
};
}

2. I think this is C++ 's syntax, we can make a simple test.
class Shape  //which is interface in the C++ category
{
public:
  virtual void draw() = 0;
};

class Rectangle: public Shape
{
//public:
//   void draw();
};

The code below will not generate any compiler error both in VC6 and VC7.
C++ syntax allow the fact that the derived class did not implement virtual
method, we consider it as another vitual class. Because the MC++ will use
the cl.exe as the unmanaged C++, so they has the similar behavior.

namespace MCPP
{
    //[InterfaceTypeAttribute(ComInterfaceType::InterfaceIsIUnknown)]
    //public __gc __interface IMyInterface
    //{
    //    void HelloWorld();
    //};
    [ClassInterface(ClassInterfaceType::None)]
    public __gc class MyClass : public IMyInterface
    {
    //public:
    //    void HelloWorld()
    //    {
    //        Form* fm = new Form();
    //        fm->Show();
    //    }
    //public:
    //    int TestString(int x)
    //    {
    //        return x*x*x;
    //    }
    };
}
But if we plan to use the MyClass which has did not implement the
TestString, we will get compiler error.
    MCPP::MyClass* pCls = new MCPP::MyClass(); //the code line will generate
the compiler error as below.
error C2259: 'MCPP::MyClass' : cannot instantiate abstract class

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


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.