ATL (Active Template Library)

C++ 2010. 11. 4. 13:18

 

 
 
ATL (Active Template Library)
MFC에 비해 작고 compact한 개발을 위한 Library. (overhead 거의 없음)
UI에 관련된 기능은 거의 지원하지 않음.
주로 COM 관련 기능을 제공하는 자동화 서버, ActiveX 컨트롤, 웹 서버 프로그램 등에 주로 사용.
 
기본클래스 
CComObjectRootEx : IUnknown이 구현된 클래스.
CComCoClass : 클래스 팩토리 기능의 클래스.
IDispatchImpl : Automation 지원을 위한 IDispatch 구현 클래스.
CComPtr : 스마트 포인터 지원 template 클래스.
  소멸자에서 메모리 해제(delete)를 수행하므로 명시적으로 해줄 필요 없음.
CComQIPtr : 스마트 포인터 지원과 QueryInterface 기능의 클래스.
 
 
 
ATL Automation 서버 작성
 
1. ATL Project 생성
"Applicaton Settings > Server type"
Dynamic-link library(DLL) : in-process 서버
Executable (EXE) : local out-of-process server
Service (EXE) : 윈도우 시작시 실행되는 background 윈도우 프로그램
 
ex) 프로젝트명 "AutomationATL", Dynamic-link library(DLL) 선택.
 
2. ATL Simple Object 추가
"Class View > 오른쪽 메뉴 > ATL > ATL Simple Object" 선택.
"Names > Short name"에 Calculator 입력 (ProgID는 "프로젝트명.Short name"으로 정의됨)
"Options > Interface" 에서 Dual 확인.
 
3. 메소드 추가 및 코드 작성
"Class View > ICalculator > 오른쪽 마우스 메뉴 > Add Method..." 에서 메소드 추가
ex)
형식 : HRESULT ADD([in] LONG a, [in] LONG b, [out, retval] LONG* ret);
코드:
STDMETHODIMP CCalculator::ADD(LONG a, LONG b, LONG* ret)
{
	*ret = a + b;
	return S_OK;
}​



 
ATL Automation 클라이언트 작성 - VBScript(.vbs)
[test.vbs]
 
Set test = CreateObject("AutomationATL.Calculator") sum = test.ADD(5, 2) msgBox("ret = " & CStr(sum))
 
ATL Automation 클라이언트 작성 -
 IDispatch/ICalculator/스마트포인터 사용한 호출 방법
 
1. MFC application 프로젝트 생성
2. "From typeLib"으로 .tlb 지정
 
"Class View > MFC Class From TypeLib" 선택 후 
서버 프로그램의 .tlb파일 지정 및 Interfaces 선택
 
3. include 및 호출 코드 작성
#include "CCalculator.h"
AfxOleInit();
long ret; HRESULT hr;


if (false) { //IDispatch를 사용한 간접 호출
	CCalculator cls;
	if (!cls.CreateDispatch(_T("AutomationATL.Calculator"))) 
		AfxMessageBox(_T("server error"));

	ret = cls.ADD(1, 12);
	cls.DetachDispatch();
}

if (false) { //IDispatch를 사용한 간접 호출 + 스마트 포인터 사용
	ICalculatorPtr ptr; hr = ptr.CreateInstance(_T("AutomationATL.Calculator"));
	if (FAILED(hr))
		AfxMessageBox(_T("server error"));

	ret = ptr->ADD(1, 2);
}

if (true) //ICalculator를 사용한 직접 호출 + 스마트 포인터 사용
{
	ICalculatorPtr ptr; hr = ptr.CreateInstance(_T("AutomationATL.Calculator"));
	if (FAILED(hr))
		AfxMessageBox(_T("server error"));

	hr = ptr->raw_ADD(1, 3, &ret);

	if (FAILED(hr)) AfxMessageBox(_T("ADD function call error"));
}

CString str;
str.Format(_T("%ld"), ret);

AfxMessageBox(str);​



ATL Automation 클라이언트 작성 - COM interface 사용
 
IDispatch 드라이버 사용보다 좋은 성능
1. MFC application 프로젝트 생성
 
2. 해더 작성
//{CDDF1B9B-29E6-4E45-B33B-F10074A81C4D}
const CLSID CLSID_Calculator =
{ 0xCDDF1B9B, 0x29E6, 0x4E45, 0xB3, 0x3B, 0xF1, 0x00, 0x74, 0xA8, 0x1C, 0x4D };

//{E6B6FF28-B915-404B-A539-23B2D625BEDC}
const IID IID_Calculator =
{ 0xE6B6FF28, 0xB915, 0x404B, 0xA5, 0x39, 0x23, 0xB2, 0xD6, 0x25, 0xBE, 0xDC };

DECLARE_INTERFACE(ICalculator, IDispatch)
{
	STDMETHOD(ADD)(THIS_ long a, long b, long *c) PURE;
};​


3. 호출
AfxOleInit();

ICalculator* iCalculator;
HRESULT hr = CoCreateInstance(CLSID_Calculator, NULL, CLSCTX_INPROC_SERVER, IID_Calculator, (void**)&iCalculator);
if (FAILED(hr))
	AfxMessageBox(_T("server connection error"));

long ret = 0;
iCalculator->ADD(1, 2, &ret);

//**. iCalculator->ADD(1, 2, &ret) 에서 메모리 참조 에러난다..
//**. 뭘 잘못했지..? X(  
 
 
ATL ActiveX Control
 
1. "ATL Project" 생성
 
2. "ATL Control" 추가
"Class View > 오른족 마우스 메뉴 > Add > Add Class > ATL Control" 선택
"Names > Short name" 에 클래스명 작성. ex) TestCtrl
"Options > Connection points" 선택.
"Stock Properties" 에서 Background Color, Caption, Font, Foreground Color 선택
 
: )
 
 
 
 

'C++' 카테고리의 다른 글

log4cxx  (0) 2010.11.06
Singleton (싱글톤)  (0) 2010.11.05
Win32 메모리 누수(Leak) 체크  (1) 2010.11.04
Distributed Environment (분산 환경)  (0) 2010.11.04
ActiveX control  (0) 2010.11.04
OLE (Object Linking and Embedding)  (0) 2010.11.04
Automation (자동화)  (0) 2010.11.02
COM (component object model)  (1) 2010.11.02