1. Form设计

2. 源码
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <atl\atlmod.h>
#include "EditServerImpl.h"
USERES("autosrv.res");
USEFORM("main.cpp", Form1);
USETLB("autosrv.tlb");
USEUNIT("autosrv_TLB.cpp");
USEUNIT("EditServerImpl.cpp"); /* EditServer: CoClass */
USEUNIT("autosrv_ATL.cpp");
//---------------------------------------------------------------------------
TComModule _ProjectModule(0 /*InitATLServer*/);
TComModule &_Module = _ProjectModule;
// The ATL Object map holds an array of _ATL_OBJMAP_ENTRY structures that
// described the objects of your OLE server. The MAP is handed to your
// project's CComModule-derived _Module object via the Init method.
//
BEGIN_OBJECT_MAP(ObjectMap)
OBJECT_ENTRY(CLSID_EditServer, TEditServerImpl)
END_OBJECT_MAP()
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return 0;
}
//---------------------------------------------------------------------------

[
uuid(14452774-CA96-4F0A-B4B1-F464D6150463),
version(1.0),
helpstring("autosrv Library")
]
library autosrv
{
importlib("stdole2.tlb");
[
uuid(4C58BE81-EF79-4C7E-ADE7-A52B8F090C97),
version(1.0),
helpstring("Dispatch interface for EditServer Object"),
dual,
oleautomation
]
interface IEditServer: IDispatch
{
[
id(0x00000001)
]
HRESULT _stdcall Clear( void );
[
id(0x00000002)
]
HRESULT _stdcall SetThreeStr([in] BSTR s1, [in] BSTR s2, [in] BSTR s3, [out, retval] BSTR * result );
[
propget,
id(0x00000003)
]
HRESULT _stdcall EditNum([out, retval] int * Value );
[
propput,
id(0x00000003)
]
HRESULT _stdcall EditNum([in] int Value );
[
propget,
id(0x00000004)
]
HRESULT _stdcall EditStr([out, retval] BSTR * Value );
[
propput,
id(0x00000004)
]
HRESULT _stdcall EditStr([in] BSTR Value );
};
[
uuid(E75CBA81-036A-4B11-A570-4B3107E335FC),
version(1.0),
helpstring("EditServer Object")
]
coclass EditServer
{
[default] interface IEditServer;
};
};
//$$---- ATL Unit CPP ---- (stATLUnitSource)
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#define USING_ATL
#include "autosrv_ATL.h"
#pragma option -w-aus // Var. assigned value never used
#ifdef _ATL_STATIC_REGISTRY
#include <statreg.h>
#include <statreg.cpp>
#endif
#include <atl\atlimpl.cpp>
#include <atl\atlctl.cpp>
#if defined(USING_ATLVCL)
#include <atl\atlvcl.cpp>
#else
#include <atl\atlwin.cpp>
#endif
//---------------------------------------------------------------------------
#pragma package(smart_init)
// ************************************************************************ //
// WARNING
// -------
// The types declared in this file were generated from data read from a
// Type Library. If this type library is explicitly or indirectly (via
// another type library referring to this type library) re-imported, or the
// 'Refresh' command of the Type Library Editor activated while editing the
// Type Library, the contents of this file will be regenerated and all
// manual modifications will be lost.
// ************************************************************************ //
// C++ TLBWRTR : $Revision: 1.0 $
// File generated on 11/12/1999 5:13:40 PM from Type Library described below.
// *************************************************************************//
// NOTE:
// Items guarded by $IFDEF_LIVE_SERVER_AT_DESIGN_TIME are used by properties
// which return objects that may need to be explicitly created via a function
// call prior to any access via the property. These items have been disabled
// in order to prevent accidental use from within the object inspector. You
// may enable them by defining LIVE_SERVER_AT_DESIGN_TIME or by selectively
// removing them from the $IFDEF blocks. However, such items must still be
// programmatically created via a method of the appropriate CoClass before
// they can be used.
// ************************************************************************ //
// Type Lib: C:\Program Files\Borland\Rampage\Projects\autosrv\autosrv.tlb (1)
// IID\LCID: {14452774-CA96-4F0A-B4B1-F464D6150463}\0
// Helpfile:
// DepndLst:
// (1) v2.0 stdole, (C:\WIN2K\System32\STDOLE2.TLB)
// (2) v4.0 StdVCL, (C:\WIN2K\System32\STDVCL40.DLL)
// ************************************************************************ //
#include <vcl.h>
#pragma hdrstop
#include "autosrv_TLB.h"
#if !defined(__PRAGMA_PACKAGE_SMART_INIT)
#define __PRAGMA_PACKAGE_SMART_INIT
#pragma package(smart_init)
#endif
namespace Autosrv_tlb
{
// *********************************************************************//
// GUIDS declared in the TypeLibrary
// *********************************************************************//
const GUID LIBID_autosrv = {0x14452774, 0xCA96, 0x4F0A,{ 0xB4, 0xB1, 0xF4, 0x64, 0xD6, 0x15, 0x04, 0x63} };
const GUID IID_IEditServer = {0x4C58BE81, 0xEF79, 0x4C7E,{ 0xAD, 0xE7, 0xA5, 0x2B, 0x8F, 0x09, 0x0C, 0x97} };
const GUID CLSID_EditServer = {0xE75CBA81, 0x036A, 0x4B11,{ 0xA5, 0x70, 0x4B, 0x31, 0x07, 0xE3, 0x35, 0xFC} };
}; // namespace Autosrv_tlb
// EDITSERVERIMPL : Implementation of TEditServerImpl (CoClass: EditServer, Interface: IEditServer)
#include <vcl.h>
#pragma hdrstop
#include "main.h"
#include "EDITSERVERIMPL.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::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;
}
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;
};
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
}
//---------------------------------------------------------------------------
本文介绍了如何使用C++和ATL实现一个名为'autosrv'的库,包括设计Form界面、编写的COM接口和CoClass,展示了如何通过IEditServer接口进行字符串操作和数值编辑。
5942

被折叠的 条评论
为什么被折叠?



