Reputation: 691
I'm trying to create a very basic COM dll for inclusion in a C# project, to then use to wrap up some C++ functionality into a COM module for inclusion in a C# project (the language used by the rest of the code base).
I included the COM dll, added a class to the COM dll and instantiated it in the C# code, but so far I haven't been able to add new functionality to the new class.
Here's what I did:
MyCOMDLLLib.MyCOMObjectClass myVariable = new MyCOMObjectClass();
I've tried various things so won't list any except the most obvious here - I tried adding a class method to MyCOMObject.h:
// CMyCOMObject
class ATL_NO_VTABLE CMyCOMObject :
public CComObjectRootEx,
public CComCoClass,
public IDispatchImpl
{
public:
CMyCOMObject()
{
}
DECLARE_PROTECT_FINAL_CONSTRUCT()
HRESULT FinalConstruct()
{
return S_OK;
}
void FinalRelease()
{
}
DECLARE_REGISTRY_RESOURCEID(IDR_MYCOMOBJECT)
DECLARE_NOT_AGGREGATABLE(CMyCOMObject)
BEGIN_COM_MAP(CMyCOMObject)
COM_INTERFACE_ENTRY(IMyCOMObject)
COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()
// IMyCOMObject
public:
// SB: My attempt at adding a new method
int ReturnTwo()
{
return 2;
}
};
The ReturnTwo method was visible in the class view of CMyCOMObject, but not visible in the class view of MyCOMObjectClass in the C# Windows Forms Application project.
Any help much appreciated.
Upvotes: 2
Views: 2743
Reputation: 942508
I tried adding a class method to MyCOMObject.h
That's not good enough. The super important file in your project is the .idl file (Interface Description Language). That file generates the type library that gets embedded in the DLL. The type library is what .NET uses to generate the managed import library. Your added function won't be in there because you didn't modify the .idl file.
Modifying interfaces is a painful if you do it by hand, there are three places that need to be edited. The .idl, the .h and the .cpp file. It is best done with the wizard. From the Class View window, right-click the interface (like IFoo), Add, Add Method (or Property). Then switch to the .cpp file to write the implementation of the method.
Upvotes: 2
Reputation: 16142
Did you copy your new build of the dll over into the C# project again?
Upvotes: 0