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

Kylix_XP 2002-06-08 04:58:04
这是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 是双接口中的一个?






...全文
66 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
fantong 2002-06-10
  • 打赏
  • 举报
回复
我眼花,晕了
STDMETHODIMP =
#define STDMETHODIMP HRESULT STDMETHODCALLTYPE
S_OK
#define S_OK ((HRESULT)0x00000000L)
bjhua 2002-06-08
  • 打赏
  • 举报
回复
现在请教高手几个问题:
***在服务器:
STDMETHODIMP TEditServerImpl::Clear()中,STDMETHODIMP是什么意思?返回的什么完意?
stdmethodimp = _stdcall
return S_OK; S_OK是什么东西?
S_OK是一个long,表示成功
***数据类型 BSTR, WideString ,AnsiString 有什么不同?
BSTR是包含长度前缀、以NULL结尾的OLECHAR.OLECHAR 在windows中是用typedef 定义的wchar_t,WideString就是双子节字符的字符串了,AnsiString是C++Builder里定义的字符串。好像与WideString差不多。

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

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

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

你查一下 TCOMIEditServer是不是一个智能指针,或者别的什么类似于指针的东西。


IEditServerDisp 是双接口中的一个?
quaker1234 2002-06-08
  • 打赏
  • 举报
回复
STDMETHODIMP、 S_OK这些都是宏定义,查一下相应的.h文件应该能找到,我用vc,声明接口时有STDMETHOD的宏定义,比如STDMETHOD(methodname)就是声明接口成员函数methodname(),此成员函数返回HRESULT。
S_OK也是宏定义相当于SUCCESS,成功标志
下面问题请高手回答

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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