想成为COM高手请来讨论几个问题,高分相送
这是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 是双接口中的一个?
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 是双接口中的一个?
...全文
相关推荐
要想成为一名优秀的程序员需要重点掌握那些知识.doc
要想成为一名优秀的程序员需要重点掌握那些知识要想成为一名优秀的程序员,需要掌握的知识非常广泛。今天,我们来讨论一下 Web 程序员应该具备的知识点。首先,让我们来谈谈脚本技术。
banwannabefamousbots:ban想成为著名的机器人
禁令想成为著名机器人Twitch聊天机器人可帮助打击来自聊天机器人产品的垃圾邮件,以及更多待售产品。动机如果您在Twitch聊天室中看到类似于波纹管的消息。 想成名吗? 购买关注者,素数并查看...
彻底解决COM端口被占用(在使用中)问题的办法
今天,我们将讨论如何彻底解决COM端口被占用(在使用中)问题的办法。一、释放被占用的COM口在使用蓝牙驱动时,COM5~COM15可能会被占用。如果你成功安装驱动后,COM16也可能会被占用。
com+目录问题_event_com目录问题_
标题 "com+目录问题_event_com目录问题_" 涉及的是Windows操作系统中的COM+技术和与事件日志(Event Log)相关的目录问题。
prozuktischool.com:我想学习,所以我教
"prozuktischool.com: 我想学习,所以我教" 这个标题代表的是一种教育理念,即通过自我学习并分享知识的方式来提升个人技能,同时也帮助他人学习。


