
Signature
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
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.