这是BCB自带的一个关于Automation技术的问题:
1.Automation 服务器程序源代码如下:// EDITSERVERIMPL : Implementation of TEditServerImpl (CoClass: EditServer, Interface: IEditServer)#include <vcl.h>
#pragma hdrstop#include "EDITSERVERIMPL.H"
#include "Unit1.h"
/////////////////////////////////////////////////////////////////////////////
// TEditServerImplSTDMETHODIMP 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"
/////////////////////////////////////////////////////////////////////////////
// TEditServerImplSTDMETHODIMP 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 是双接口中的一个?
解决方案 »
- 大家帮忙忙看看这段汇编代码是如何加密密码的??100分求!!
- 关于combobox的绘制,有经验的人进来帮忙下啦,谢谢
- 关于串口编程基础请教
- 为什么对话框拉动改变窗体大小和最大化窗体时所得到的数值不一样?
- 如何手动修改对话框中各控件的属性?(在线等)
- 一个关于FTP向服务器上传文件的问题~帮我看看吧~
- 怪事!音箱为什么会响?
- 我要创建这样的切分窗口:先分成两个列在把左边的一列分成两个行,为什么编译时总出错,如果我把右边的一列分成两个行编译就没有问题了?
- 关于侯俊杰《深入浅出MFC WINDOWS编程》第二版
- 小弟主框架是用的CFormView,我想在一个click事件后关闭这个框,再打开另外一个对话框做主框架,应该如何做?
- !!大送100分!!关于DDRAW播放AVI!!!
- VC编程,手机话费查询程序,高分讨论!
S_OK也是宏定义相当于SUCCESS,成功标志
下面问题请高手回答
***在服务器:
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 是双接口中的一个?
STDMETHODIMP =
#define STDMETHODIMP HRESULT STDMETHODCALLTYPE
S_OK
#define S_OK ((HRESULT)0x00000000L)