Pregunta

I have a dll I wrote in VS2010 C#, and ensured the project is marked COM visible, and registered for COM Interop. I can use in a VS2010 C++ project, by going through the C++ Property Pages->Common Properties->Framework and References, referencing the C# project, and then writing the code to instantiate my object with gcnew.

However, this method won't work in Borland C++. Since the C# dll was registered, I can add a reference to it, and *_OCX.h and *_TLB.h files were generated and I can see what look like usable objects in those files. But, I don't know how to create my class. I don't even know which class is the right one because of the renaming that's been done.

In C#, the class can be as simple as:

public namespace MyTest
{
   public class MyClass
   {
      public MyClass() { }
      public int SomeInt { get; set; }
   }
}

In C++, a lot is generated. Some of which is:

MyTest_TLB.cpp

#include <vcl.h>
#pragma hdrstop
#include "MyTest_TLB.h"

namespace Mytest_tlb
{
const GUID LIBID_MyTest = {0xE082EF89, 0x8FE8, 0x45FA,{ 0x8D, 0x13, 0x20,0x3B, 0x58, 0xCF,0x7E, 0x82} };
const GUID CLSID_MyClass = {0x22E68247, 0x3CDB, 0x3CCA,{ 0xB8, 0x8B, 0xB9,0x52, 0xF3, 0x4C,0x11, 0x70} };
const GUID IID__MyClass = {0x6A6EC8CB, 0x1AAB, 0x3AB1,{ 0xBD, 0x57, 0x0F,0xE2, 0x17, 0x78,0xD4, 0xE2} };
};     // namespace Mytest_tlb

MyTest_TLB.h

namespace Stdvcl {class IStrings; class IStringsDisp;}
using namespace Stdvcl;
typedef TComInterface<IStrings> IStringsPtr;
typedef TComInterface<IStringsDisp> IStringsDispPtr;

namespace Mytest_tlb
{
extern __declspec (package) const GUID LIBID_MyTest;
extern __declspec (package) const GUID IID__MyClass;
extern __declspec (package) const GUID CLSID_MyClass;

interface DECLSPEC_UUID("{6A6EC8CB-1AAB-3AB1-BD57-0FE21778D4E2}") _MyClass;
typedef TComInterface<_MyClass, &IID__MyClass> _MyClassPtr;
typedef _MyClass MyClass;
typedef _MyClassPtr MyClassPtr;

interface _MyClass  : public IDispatch
{
public:
#if !defined(__TLB_NO_INTERFACE_WRAPPERS)
#endif //   __TLB_NO_INTERFACE_WRAPPERS
};

#if !defined(__TLB_NO_INTERFACE_WRAPPERS)
template <class T /* _MyClass */ >
class TCOM_MyClassT : public TComInterface<_MyClass>, public TComInterfaceBase<IUnknown>
{
public:
  TCOM_MyClassT() {}
  TCOM_MyClassT(_MyClass *intf, bool addRef = false) : TComInterface<_MyClass>(intf, addRef) {}
  TCOM_MyClassT(const TCOM_MyClassT& src) : TComInterface<_MyClass>(src) {}
  TCOM_MyClassT& operator=(const TCOM_MyClassT& src) { Bind(src, true); return *this;}
};
typedef TCOM_MyClassT<_MyClass> TCOM_MyClass;

template<class T>
class _MyClassDispT : public TAutoDriver<_MyClass>
{
public:
  _MyClassDispT(){}

  _MyClassDispT(_MyClass *pintf)
  {
    TAutoDriver<_MyClass>::Bind(pintf, false);
  }

  _MyClassDispT(_MyClassPtr pintf)
  {
    TAutoDriver<_MyClass>::Bind(pintf, true);
  }

  _MyClassDispT& operator=(_MyClass *pintf)
  {
    TAutoDriver<_MyClass>::Bind(pintf, false);
    return *this;
  }

  _MyClassDispT& operator=(_MyClassPtr pintf)
  {
    TAutoDriver<_MyClass>::Bind(pintf, true);
    return *this;
  }

  HRESULT BindDefault()
  {
    return OLECHECK(Bind(CLSID_MyClass));
  }

  HRESULT BindRunning()
  {
    return BindToActive(CLSID_MyClass);
  }
};
typedef _MyClassDispT<_MyClass> _MyClassDisp;

typedef TCoClassCreatorT<TCOM_MyClass, _MyClass, &CLSID_MyClass, &IID__MyClass> CoMyClass;
#endif  //   __TLB_NO_INTERFACE_WRAPPERS
};     // namespace Mytest_tlb

There's also a TCOM_MyClassT.h file. I don't know what any of this means. The tutorials or examples I've seen are either for VC++, out-of-date, not specific to my problem, or just don't work.

¿Fue útil?

Solución

Got it.

CoInitialize(NULL);
_MyClassPtr obj;
HRESULT ptr = obj.CreateInstance(__uuidof(MyClass));

Frustrating that some of the simplest things can be so difficult.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top