应该返回ComObject的ComServer

时间:2010-02-04 13:28:44

标签: c++ com c++builder

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

此Serversidecode看起来是否正确?

服务器表格: .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;
}

Server TestComServerImpl: 的.cpp

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

2 个答案:

答案 0 :(得分:1)

你的GetMyObject()方法很糟糕,它返回一个指向局部变量的指针。这只会偶然发生,永远不会用于包装COM的任何语言。修复(减去错误处理):

DummyComObj *myDum = Form2->Getformdummy();
return myDum->QueryInterface(__uuidof(IDummyComObj), outObj);

不确定__uuidof()是否在Builder中可用,使用你得到的任何东西来获得界面的IID。

答案 1 :(得分:1)

请求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);
}

Server TestComServerImpl:.cpp

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