c++ - 应返回 ComObject 的 ComServer

标签 c++ com c++builder

我想做的是将在服务器端创建的对象传输到客户端。当我在服务器端和客户端使用 C++ 时,我已经让它工作得很好,但是我没有让我的服务器与其他语言(如 .Net)一起工作,它可能不喜欢这些指针!

这个服务器端代码看起来正确吗?

服务器表单: .h

class TForm2 : public TForm
{
__published:      // IDE-managed Components
      TMemo *Memo1;
private:      // User declarations
      DummyComObj* formDummy;
public:            // User declarations
      __fastcall TForm2(TComponent* Owner);
      IDummyComObj* Getformdummy();
};

.cpp

__fastcall TForm2::TForm2(TComponent* Owner)
      : TForm(Owner)
{
      CoCreateInstance( CLSID_DummyComObj,NULL,CLSCTX_ALL,IID_IDummyComObj,(void**)&formDummy);
}

DummyComObj* TForm2::Getformdummy()
{
      return formDummy;
}

服务器 TestComServerImpl: .cpp

STDMETHODIMP STDMETHODCALLTYPE TServerDidleComTestImpl::GetMyObject(IDummyComObj** outObj)
{
      DummyComObj *myDum = Form2->Getformdummy();
      *outObj = &myDum;
      return S_OK;
}

最佳答案

除了 TServerDidleComTestImpl::GetMyObject() 由于您的原因没有编译 试图将 DummyComObj ** 分配给 DummyComObj*,但你也是 完全忽略 COM 引用计数规则。

试试这个:

服务器形式:.h

#include <utilcls.h>

class TForm2 : public TForm
{
__published: // IDE-managed Components
    TMemo *Memo1;
private: // User declarations
    TComInterface<IDummyComObj> formDummy;
public: // User declarations
    __fastcall TForm2(TComponent* Owner);
    void Getformdummy(IDummyComObj** outObj);
};

服务器形式:.cpp

__fastcall TForm2::TForm2(TComponent* Owner)
    : TForm(Owner)
{
    CoCreateInstance( 
CLSID_DummyComObj,NULL,CLSCTX_ALL,IID_IDummyComObj,(void**)&formDummy);
}

HRESULT TForm2::Getformdummy(DummyComObj** outObj)
{
    return formDummy->QueryInterface(IID_IDummyComObj,(void**)outObj);
}

服务器 TestComServerImpl: .cpp

STDMETHODIMP STDMETHODCALLTYPE 
TServerDidleComTestImpl::GetMyObject(IDummyComObj** outObj)
{
    return Form2->Getformdummy(outObj);
}

关于c++ - 应返回 ComObject 的 ComServer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2199951/

相关文章:

c++ - 将针对 msvcrt.dll 编译的库链接到 Visual Studio 10 C++ 应用程序

c++ - 输出字符串用 C++ 在 Linux 中覆盖终端上的最后一个字符串

c# - IUnknown_SetSite 引用计数器行为

utf-8 - C++ builder - 将 UnicodeString 转换为 UTF-8 编码的字符串

c++ - 未处理的异常得到处理(visual studio 2019 这么聪明还是我遗漏了什么?)

c++ - 将模板化方法定义移出头文件的优雅方法

c++ - 什么是 com 对象,我该如何使用它?

delphi - 当客户端连接崩溃时如何释放服务器 DCOM 中的引用

c++ - unique_ptr<TStringList []> dsts(new TStringList[5]) 失败

c++ - 未解析的外部 Vcl::Filectrl::SelectDirectory