📄 ch27.htm
字号:
return A * A;
}
void __fastcall RegisterTSimpleDCOM()
{
TAutoClassInfo AutoClassInfo;
AutoClassInfo.AutoClass = __classid(TSimpleDCOM);
AutoClassInfo.ProgID = "EasyDCOM.SimpleDCOM";
AutoClassInfo.ClassID = "{E2674A60-2DF2-11D0-92C5-000000000000}";
AutoClassInfo.Description = "Easiest possible DCOM program";
AutoClassInfo.Instancing = acMultiInstance;
Automation->RegisterClass(AutoClassInfo);
}
int Initialization()
{
RegisterTSimpleDCOM();
return 0;
}
</FONT></PRE>
<P><A NAME="Heading17"></A><FONT COLOR="#000077"><B>Listing 27.5. The header for
the main source file for the EasyDCOM OLE server.</B></FONT></P>
<PRE><FONT
COLOR="#0066FF">#ifndef MainH
#define MainH
#include <vcl\Classes.hpp>
#include <vcl\Controls.hpp>
#include <vcl\StdCtrls.hpp>
#include <vcl\Forms.hpp>
class TForm1 : public TForm
{
__published:
TLabel *Label1;
private:
public:
virtual __fastcall TForm1(TComponent* Owner);
};
extern TForm1 *Form1;
#endif
</FONT></PRE>
<P><A NAME="Heading18"></A><FONT COLOR="#000077"><B>Listing 27.6. The main source
file for the EasyDCOM OLE server.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">#include <vcl\vcl.h>
#pragma hdrstop
#include "Main.h"
#pragma resource "*.dfm"
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
</FONT></PRE>
<P>This
program is meant to be run from a client. As such, it has no controls on
it and no public interface other than the OLE object itself. I do, however, give
the main form a distinctive look, as you can see in Figure 27.2.<BR>
<BR>
<A
NAME="Heading19"></A><A HREF="27ebu02.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/27/27ebu02.jpg">FIGURE 27.2.</A><FONT COLOR="#000077">
</FONT><I>The main form for the EasyDCOM program.</I></P>
<P>Of course, there is no reason that a single program could not simultaneously have
an OLE server
interface and a set of standard controls. For example, Word and Excel
are both OLE servers, and standard applications run through a set of menus and other
controls. In fact, the same application can work as a server, a standard application,
and as a
client.</P>
<P>Note that, by using two different approaches, you can ensure that the application
is registered each time it is run. One technique involves including an initialization
procedure:</P>
<PRE><FONT COLOR="#0066FF">int Initialization()
{
RegisterTSimpleDCOM();
return 0;
}
</FONT></PRE>
<P>The second technique, shown earlier in the chapter, involves a <TT>pragma</TT>:</P>
<PRE><FONT COLOR="#0066FF">#pragma startup RegisterTSimpleDCom
</FONT></PRE>
<P>Both technologies achieve the
same effect. As a rule, you don't have to think
about this part of the process because the code will be inserted automatically by
the Automation Wizard. Needless to say, nothing is magic about the Automation expert,
and you can simply create the code
yourself by typing it in. In that case, you are
free to use either technique, though the <TT>pragma</TT> is probably easier to write.</P>
<P>That's all I'm going to say for now about creating the server side of a BCB DCOM
project. Remember that this
code will not work unless you first register the <TT>TSimpleDCOM</TT>
object with the system by running the server once. After you run the server the first
time, you never have to run it again, as it will be called automatically by the client
program
described in the next section. Let me repeat that the whole point of this
exercise is that the client program can be located on a separate machine.
<H3><A NAME="Heading20"></A><FONT COLOR="#000077">Creating the DCOM Client</FONT></H3>
<P>The GetDCOM
program found on the CD that accompanies this book will call the functions
in the server program described in the preceding section. In particular, GetDCOM
can automatically launch the server program and then call its <TT>GetName</TT> and
<TT>Square</TT> functions.
<DL>
<DT></DT>
</DL>
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>When I say that GetDCOM can automatically
launch the server, I'm assuming that the server is either on the current system (in
which case, it is launched via COM) or on an NT machine (in which case, it is launched
via DCOM). DCOM cannot launch an application residing on a remote Windows 95 box.
<HR>
</BLOCKQUOTE>
<P>You can run this application in two different modes.
You can run it as a client
to a local Automation server or as a client to a remote Automation server. If you
look at the main form for the program, shown in Figure 27.3, you can see that it
has three buttons: one for launching the server remotely, one
for launching it locally,
and a third that will be used to call a simple function on the server.<BR>
<BR>
<A NAME="Heading22"></A><A HREF="27ebu03.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/27/27ebu03.jpg">FIGURE 27.3.</A><FONT COLOR="#000077">
</FONT><I>The main form for the GetDCOM
application.</I></P>
<P>The source for the GetDCOM program is shown in Listing 27.7 and Listing 27.8.
This program uses a routine called <TT>CreateRemoteObject</TT> that is declared in
the <TT>CodeBox</TT> unit found in the <TT>Utils</TT> subdirectory
on the CD that
accompanies this book. You need to add the <TT>CodeBox</TT> unit to your project;
otherwise, it will not compile. I do not include the entire <TT>CodeBox</TT> unit
in this chapter, but it is available on the CD, and I do include the
<TT>CreateRemoteObject</TT>
function in its entirety later in this chapter. Notice also that this project includes
the <TT>OleAuto</TT> unit to call <TT>CreateOleObject</TT> to retrieve a local instance
of <TT>IDispatch</TT>.</P>
<P>When using this
program, please note that I have hard coded the IP address of
my server into the source. You will need to change this so that it works with your
server. When making the connection between a Windows 95 and Windows NT machine, you
should start by
calling from the Windows 95 machine to the Windows NT machine; that
is, put the client on the Windows 95 machine. You should also start by signing on
to both machines with the same name and password. That way you don't have to worry
about security
issues on the server while you are first getting the technology up
and running. Also, give yourself all possible rights on the server. Make yourself
an administrator.<BR>
<BR>
<A NAME="Heading23"></A><FONT COLOR="#000077"><B>Listing 27.7. The header
for the
GetDCOM OLE client application.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">///////////////////////////////////////
// Main.h
// Project: GetDCOM
// Copyright (c) 1997 by Charlie Calvert
//
#ifndef MainH
#define MainH
#include
<vcl\Classes.hpp>
#include <vcl\Controls.hpp>
#include <vcl\StdCtrls.hpp>
#include <vcl\Forms.hpp>
#include <vcl\Buttons.hpp>
class TForm1 : public TForm
{
__published:
TBitBtn *GetLocalObjectBtn;
TBitBtn
*GetRemoteObjectBtn;
TEdit *Edit1;
TBitBtn *SquareBtn;
void __fastcall GetLocalObjectBtnClick(TObject *Sender);
void __fastcall GetRemoteObjectBtnClick(TObject *Sender);
void __fastcall SquareBtnClick(TObject *Sender);
void
__fastcall FormDestroy(TObject *Sender);
private:
Variant V;
public:
virtual __fastcall TForm1(TComponent* Owner);
};
extern TForm1 *Form1;
#endif
</FONT></PRE>
<P><A NAME="Heading24"></A><FONT COLOR="#000077"><B>Listing 27.8. The main
source
file for the GetDCOM application.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">///////////////////////////////////////
// Main.cpp
// Project: GetDCOM
// Copyright (c) 1997 by Charlie Calvert
//
#include <vcl\vcl.h>
#include
<vcl\OleAuto.hpp>
#include <vcl\ole2.hpp>
#include <initguid.h>
#pragma hdrstop
#include "Main.h"
#include "codebox.h"
#pragma resource "*.dfm"
TForm1 *Form1;
__fastcall
TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
CoInitialize(NULL);
}
void __fastcall TForm1::GetLocalObjectBtnClick(TObject *Sender)
{
V = CreateOleObject("EasyDCOM.SimpleDCOM");
ShowMessage(V.OleFunction("GetName"));
}
DEFINE_GUID(ClassID, 0xE2674A60, 0x2DF2, 0x11D0, 0x92,0xC5,
0x00,0x00,0x00,0x00,0x00,0x00);
void __fastcall TForm1::GetRemoteObjectBtnClick(TObject *Sender)
{
Screen->Cursor =
crHourGlass;
if (CreateRemoteObject(ClassID, "143.186.149.228", V))
{
ShowMessage(V.OleFunction("GetName"));
}
else
{
ShowMessage("Failed");
}
Screen->Cursor = crDefault;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -