C++快速开发例子--app-autosrv

本文介绍了如何使用C++和ATL实现一个名为'autosrv'的库,包括设计Form界面、编写的COM接口和CoClass,展示了如何通过IEditServer接口进行字符串操作和数值编辑。


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)
{

}
//---------------------------------------------------------------------------
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

moneytree

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值