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