想成为COM高手请来讨论几个问题,高分相送

Kylix_XP 2002-06-08 04:50:47
这是BCB自带的一个关于Automation技术的问题:
1.Automation 服务器程序源代码如下:

// EDITSERVERIMPL : Implementation of TEditServerImpl (CoClass: EditServer, Interface: IEditServer)

#include <vcl.h>
#pragma hdrstop

#include "EDITSERVERIMPL.H"
#include "Unit1.h"
/////////////////////////////////////////////////////////////////////////////
// TEditServerImpl

STDMETHODIMP TEditServerImpl::Clear()
{
try
{
Form1->Edit1->Text="";
}
catch(Exception &e)
{
return Error(e.Message.c_str(),IID_IEditServer);
}
return S_OK;
}

STDMETHODIMP TEditServerImpl::get_EditNum(int* Value)
{
try
{
int val = atoi(Form1->Edit1->Text.c_str());
*Value = val;
}
catch(Exception &e)
{
return Error(e.Message.c_str(), IID_IEditServer);
}
return S_OK;
};


STDMETHODIMP TEditServerImpl::get_EditStr(BSTR* Value)
{
try
{
*Value = WideString(Form1->Edit1->Text).Detach();
}
catch(Exception &e)
{
return Error(e.Message.c_str(), IID_IEditServer);
}
return S_OK;
};


STDMETHODIMP TEditServerImpl::set_EditNum(int Value)
{
try
{
Form1->Edit1->Text = AnsiString(Value);
}
catch(Exception &e)
{
return Error(e.Message.c_str(), IID_IEditServer);
}
return S_OK;
};


STDMETHODIMP TEditServerImpl::set_EditStr(BSTR Value)
{
try
{
Form1->Edit1->Text = AnsiString(Value);
}
catch(Exception &e)
{
return Error(e.Message.c_str(), IID_IEditServer);
}
return S_OK;
};


STDMETHODIMP TEditServerImpl::SetThreeStr(BSTR s1, BSTR s2, BSTR s3,
BSTR* result)
{
try
{
WideString retval(s1);
retval += L" ";
retval += s2;
retval += L" ";
retval += s3;
Form1->Edit1->Text = retval;

// Send out a copy of our concatenated BSTR
// NOTE: result is not an [in, out], so we're not responsible for freeing what was there before
*result = retval.Copy();
}
catch(Exception &e)
{
return Error(e.Message.c_str(), IID_IEditServer);
}
return S_OK;
}



//---------------------------------------------------------------------

客户程序如下:(客户程序要调用刚才创建的Automaiton服务器)

//------------------------------------------------------------------------//-头文件 Unit1.h

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "Server_TLB.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TEdit *Edit1;
TButton *Button2;
TButton *Button3;
TButton *Button4;
TLabel *Label1;
TLabel *Label2;
TButton *Button5;
TButton *Button6;
TButton *Button7;
TButton *Button8;
TLabel *Label3;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall Button4Click(TObject *Sender);
void __fastcall Button5Click(TObject *Sender);
void __fastcall Button6Click(TObject *Sender);
void __fastcall Button7Click(TObject *Sender);
void __fastcall Button8Click(TObject *Sender);
private: // User declarations
IEditServerDisp AutoServer;
TCOMIEditServer EditServer;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//------------------------------------------------------------------------
//Unit1.cpp
#endif

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"



//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
try
{
AutoServer.BindDefault();
EditServer = CoEditServer::Create();//创建COM实例
}
catch (...)
{
ShowMessage("Please build and run the \\AutoSrv\\autosrv example before this one.");
Application->Terminate();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AutoServer.EditStr = WideString(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Edit1->Text = AutoServer.EditStr;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
// Call a server function
AutoServer.Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
WideString retval;

AutoServer.SetThreeStr(WideString("one"),WideString("two"),
WideString("three"), &retval);
Edit1->Text = retval;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
//call the server's setter function
EditServer->set_EditStr(WideString(Edit1->Text));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
WideString str;
EditServer->get_EditStr(&str);
Edit1->Text = AnsiString(str);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
// Call a server function
EditServer->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button8Click(TObject *Sender)
{
WideString p1("This ");
WideString p2("is a ");
WideString p3("test.");
WideString retval;

// The [out, retval] parameter most come last.
EditServer->SetThreeStr(p1, p2, p3, &retval);
Edit1->Text = retval;
}
//------------------


现在请教高手几个问题:
在服务器:
STDMETHODIMP TEditServerImpl::Clear()中,STDMETHODIMP是什么意思?返回的什么完意?

return S_OK; S_OK是什么东西?

数据类型 BSTR, WideString ,AnsiString 有什么不同?

在客户:

AutoServer.BindDefault();
EditServer = CoEditServer::Create();//创建COM实例(对象)

而在Uni1.h中,IEditServerDisp AutoServer;
TCOMIEditServer EditServer;

EditServer对象不是由CoEditServer:;Create()创造了吗?为什么还要由TCOMIEditServer再创造?
TCOMIEditServerS是什么东西?

IEditServerDisp 是双接口中的一个?












...全文
36 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Siney 2002-06-08
  • 打赏
  • 举报
回复
//AutoServer.BindDefault();
// EditServer = CoEditServer::Create();//创建COM实例(对象)

//TCOMIEditServer EditServer 这行代码是不是意思是创建EditServer 对象,
//EditServer已经由EditServer = CoEditServer::Create()创建了呀,干吗还要再TCOMIEditServer又创建一次呢?
// return S_OK 又是指返回什么呢?

举一个简单的例子吧,
Tfrmmain* frm;
frm=new Tfrmmain(this); //看起来是不是和你的理解一样啊
所以:
TCOMIEditServer EditServer 只是申明了一个对象,但并没有创建

EditServer = CoEditServer::Create()才是将对象的指针赋给EditServer

S_OK是预定义的常数,具体等于多少没有必要管它,只要你知道
return S_OK 表示正常返回(没有错误)


Kylix_XP 2002-06-08
  • 打赏
  • 举报
回复
AutoServer.BindDefault();
EditServer = CoEditServer::Create();//创建COM实例(对象)

TCOMIEditServer EditServer 这行代码是不是意思是创建EditServer 对象,
EditServer已经由EditServer = CoEditServer::Create()创建了呀,干吗还要再TCOMIEditServer又创建一次呢?
return S_OK 又是指返回什么呢?
kingcaiyao 2002-06-08
  • 打赏
  • 举报
回复
象,第二种方法就是用IDISPATCH接口,第三种办法就是用VARIANT万能对象.
kingcaiyao 2002-06-08
  • 打赏
  • 举报
回复
STDMEHTOD是用于说明COM中的标准方法。
之所以用WIDESTRING 而不用ANSISTRING,这样方便在不同的进程之间进行通讯。
TCOMIEditServeR是系统自动生成的一个COM对象
IEDITSERVERSDISP是众IDISPATCH接口派生而出,那么我们通常有三办法来调用COM对象,一种是用TCOM对

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧