2

Usually to create COM interface one should declare it in IDL file. In the project I work on I have one COM interface declared in *.h file in C++:

struct DECLSPEC_UUID("A67177F7-A4DD-4A80-8EE1-25CF12172068") ISomeService : public IUnknown
{
    virtual ~ISomeService() {}

    virtual HRESULT Initialize(const Settings& settings) = 0;

    // ...
};

Moreover the method Initialize takes a struct that contains std::string fields as its parameter. The corresponding COM class is implemented in C++ and it is used from another C++ module. This works fine until I run the code under AppVerifier. It causes access violation exceptions to occur.

So my questions are

  1. Is it right to sometimes declare COM-interface in *.h file?
  2. If yes is it right to specify C++ types as parameters for COM interface methods? Or should I always use COM compliant types in such cases (BSTR etc)?
4

1 回答 1

3
  1. 当然,您可以在不使用 IDL 的情况下描述 COM 接口。但是您将无法使用诸如类型库和编组代码生成之类的 IDL 功能。但是,如果您仅将 COM 组件用作进程内服务器 (DLL),并且您可以将 .h 文件分发给客户端 - 那么这种方法可以正常工作。

  2. 避免在接口中使用 C++ 类型,因为在处理跨 DLL 边界的内存时可能会导致访问冲突。更好地使用纯 C 类型或 COM 类型

于 2012-06-04T18:35:49.040 回答