开发者

How to add new functions to a COM .dll created by a Visual Studio C++ ATL Project

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:

  1. New Visual C# Windows Forms Application
  2. Right click solution->Add->New Project...
  3. Add New Visual C++ ATL Project (called MyCOMDLL)
  4. Default options chosen in ATL Project Wizard (I've also tried ticking 'Support COM+ 1.0')
  5. Right click MyCOMDLL project->Add->Class
  6. Add New Visual C++ ATL COM+ 1.0 Component
  7. Typed 'MyCOMObject' into Short name, which automatically populated the other fields in the 'Names' tab
  8. Left the other settings as default
  9. Built the MyCOMDLL project
  10. Add a reference to the new MyCOMDLL.dll to the C# Windows Forms Application
  11. Right Click C# Windows Forms Application Project->Properties
  12. On the Build tab set Platform target to x86
  13. Add "using MyCOMDLLLib;" to Form1.cs file
  14. Add a button to the Form and in the button click function add

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.


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.


Did you copy your new build of the dll over into the C# project again?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜