深入解析ATL 笔记1 添加一个simple object时做了什么
在图书馆拿到这本不错的书,英文名为“ATL Internals”,第二版,用的是VC2005和ATL8。作者都是很牛的,决定好好看一下,补充一下ATL, COM的知识。一边看一遍记笔记,。。恩。。
首先新建一个ATL dll 工程,添加一个ATL Simple Object, 设置用default,然后用winmerge看看多了什么东西 .
添加的文件:
SimpleObj2.cpp
SimpleObj2.h
SimpleObj2.rgs
修改的文件:
atlDemo.idl
atlDemo.rc
atlDemo.vcproj
resource.h
先看看修改的文件,主要是idl
1[
2 object,
3 uuid(66B9309A-2907-44CD-8DE9-0B5D774147DD),
4 dual,
5 nonextensible,
6 helpstring("ISimpleObj2 Interface"),
7 pointer_default(unique)
8]
9interface ISimpleObj2 : IDispatch{
10};
2 object,
3 uuid(66B9309A-2907-44CD-8DE9-0B5D774147DD),
4 dual,
5 nonextensible,
6 helpstring("ISimpleObj2 Interface"),
7 pointer_default(unique)
8]
9interface ISimpleObj2 : IDispatch{
10};
以及在library atlDemoLib下面添加的
1[
2 uuid(24A41D8D-5367-4C6B-A7D3-3930361A7387),
3 helpstring("_ISimpleObj2Events Interface")
4 ]
5 dispinterface _ISimpleObj2Events
6 {
7 properties:
8 methods:
9 };
10 [
11 uuid(220C4A97-B240-4443-BEE9-73D15D66B92D),
12 helpstring("SimpleObj2 Class")
13 ]
14 coclass SimpleObj2
15 {
16 [default] interface ISimpleObj2;
17 [default, source] dispinterface _ISimpleObj2Events;
2 uuid(24A41D8D-5367-4C6B-A7D3-3930361A7387),
3 helpstring("_ISimpleObj2Events Interface")
4 ]
5 dispinterface _ISimpleObj2Events
6 {
7 properties:
8 methods:
9 };
10 [
11 uuid(220C4A97-B240-4443-BEE9-73D15D66B92D),
12 helpstring("SimpleObj2 Class")
13 ]
14 coclass SimpleObj2
15 {
16 [default] interface ISimpleObj2;
17 [default, source] dispinterface _ISimpleObj2Events;
18 };
多了的文件内容
_ISimpleObj2Events_CP.h:
1#pragma once
2
3template <class T>
4class CProxy_ISimpleObj2Events : public IConnectionPointImpl<T, &__uuidof( _ISimpleObj2Events ), CComDynamicUnkArray>
5{
6 //Warning this class will be regenerated by the wizard.
7public:
8};
9
2
3template <class T>
4class CProxy_ISimpleObj2Events : public IConnectionPointImpl<T, &__uuidof( _ISimpleObj2Events ), CComDynamicUnkArray>
5{
6 //Warning this class will be regenerated by the wizard.
7public:
8};
9
SimpleObj2.cpp:
1// SimpleObj2.cpp : Implementation of CSimpleObj2
2
3#include "stdafx.h"
4#include "SimpleObj2.h"
5
6
7// CSimpleObj2
8
9
2
3#include "stdafx.h"
4#include "SimpleObj2.h"
5
6
7// CSimpleObj2
8
9
SimpleObj2.h
1// SimpleObj2.h : Declaration of the CSimpleObj2
2
3#pragma once
4#include "resource.h" // main symbols
5
6#include "atlDemo_i.h"
7#include "_ISimpleObj2Events_CP.h"
8
9
10#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
11#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
12#endif
13
14
15
16// CSimpleObj2
17
18class ATL_NO_VTABLE CSimpleObj2 :
19 public CComObjectRootEx<CComSingleThreadModel>,
20 public CComCoClass<CSimpleObj2, &CLSID_SimpleObj2>,
21 public IConnectionPointContainerImpl<CSimpleObj2>,
22 public CProxy_ISimpleObj2Events<CSimpleObj2>,
23 public IDispatchImpl<ISimpleObj2, &IID_ISimpleObj2, &LIBID_atlDemoLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
24{
25public:
26 CSimpleObj2()
27 {
28 }
29
30DECLARE_REGISTRY_RESOURCEID(IDR_SIMPLEOBJ2)
31
32
33BEGIN_COM_MAP(CSimpleObj2)
34 COM_INTERFACE_ENTRY(ISimpleObj2)
35 COM_INTERFACE_ENTRY(IDispatch)
36 COM_INTERFACE_ENTRY(IConnectionPointContainer)
37END_COM_MAP()
38
39BEGIN_CONNECTION_POINT_MAP(CSimpleObj2)
40 CONNECTION_POINT_ENTRY(__uuidof(_ISimpleObj2Events))
41END_CONNECTION_POINT_MAP()
42
43
44 DECLARE_PROTECT_FINAL_CONSTRUCT()
45
46 HRESULT FinalConstruct()
47 {
48 return S_OK;
49 }
50
51 void FinalRelease()
52 {
53 }
54
55public:
56
57};
58
59OBJECT_ENTRY_AUTO(__uuidof(SimpleObj2), CSimpleObj2)
60
2
3#pragma once
4#include "resource.h" // main symbols
5
6#include "atlDemo_i.h"
7#include "_ISimpleObj2Events_CP.h"
8
9
10#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
11#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
12#endif
13
14
15
16// CSimpleObj2
17
18class ATL_NO_VTABLE CSimpleObj2 :
19 public CComObjectRootEx<CComSingleThreadModel>,
20 public CComCoClass<CSimpleObj2, &CLSID_SimpleObj2>,
21 public IConnectionPointContainerImpl<CSimpleObj2>,
22 public CProxy_ISimpleObj2Events<CSimpleObj2>,
23 public IDispatchImpl<ISimpleObj2, &IID_ISimpleObj2, &LIBID_atlDemoLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
24{
25public:
26 CSimpleObj2()
27 {
28 }
29
30DECLARE_REGISTRY_RESOURCEID(IDR_SIMPLEOBJ2)
31
32
33BEGIN_COM_MAP(CSimpleObj2)
34 COM_INTERFACE_ENTRY(ISimpleObj2)
35 COM_INTERFACE_ENTRY(IDispatch)
36 COM_INTERFACE_ENTRY(IConnectionPointContainer)
37END_COM_MAP()
38
39BEGIN_CONNECTION_POINT_MAP(CSimpleObj2)
40 CONNECTION_POINT_ENTRY(__uuidof(_ISimpleObj2Events))
41END_CONNECTION_POINT_MAP()
42
43
44 DECLARE_PROTECT_FINAL_CONSTRUCT()
45
46 HRESULT FinalConstruct()
47 {
48 return S_OK;
49 }
50
51 void FinalRelease()
52 {
53 }
54
55public:
56
57};
58
59OBJECT_ENTRY_AUTO(__uuidof(SimpleObj2), CSimpleObj2)
60
SimpleObj2.rgs
1HKCR
2{
3 atlDemo.SimpleObj2.1 = s 'SimpleObj2 Class'
4 {
5 CLSID = s '{220C4A97-B240-4443-BEE9-73D15D66B92D}'
6 }
7 atlDemo.SimpleObj2 = s 'SimpleObj2 Class'
8 {
9 CLSID = s '{220C4A97-B240-4443-BEE9-73D15D66B92D}'
10 CurVer = s 'atlDemo.SimpleObj2.1'
11 }
12 NoRemove CLSID
13 {
14 ForceRemove {220C4A97-B240-4443-BEE9-73D15D66B92D} = s 'SimpleObj2 Class'
15 {
16 ProgID = s 'atlDemo.SimpleObj2.1'
17 VersionIndependentProgID = s 'atlDemo.SimpleObj2'
18 ForceRemove 'Programmable'
19 InprocServer32 = s '%MODULE%'
20 {
21 val ThreadingModel = s 'Apartment'
22 }
23 'TypeLib' = s '{CFE54601-E42B-4851-8A14-D259630A8648}'
24 }
25 }
26}
27
2{
3 atlDemo.SimpleObj2.1 = s 'SimpleObj2 Class'
4 {
5 CLSID = s '{220C4A97-B240-4443-BEE9-73D15D66B92D}'
6 }
7 atlDemo.SimpleObj2 = s 'SimpleObj2 Class'
8 {
9 CLSID = s '{220C4A97-B240-4443-BEE9-73D15D66B92D}'
10 CurVer = s 'atlDemo.SimpleObj2.1'
11 }
12 NoRemove CLSID
13 {
14 ForceRemove {220C4A97-B240-4443-BEE9-73D15D66B92D} = s 'SimpleObj2 Class'
15 {
16 ProgID = s 'atlDemo.SimpleObj2.1'
17 VersionIndependentProgID = s 'atlDemo.SimpleObj2'
18 ForceRemove 'Programmable'
19 InprocServer32 = s '%MODULE%'
20 {
21 val ThreadingModel = s 'Apartment'
22 }
23 'TypeLib' = s '{CFE54601-E42B-4851-8A14-D259630A8648}'
24 }
25 }
26}
27
添加一个叫TestXXX地方法之后,多了这些东东
1
2interface IMySimpleObj : IDispatch{
3 [id(1), helpstring("method Test")] HRESULT TestXXX(void);
4};
5
6
7STDMETHODIMP CMySimpleObj::TestXXX(BSTR str)
8{
9 // TODO: Add your implementation code here
10
11 return S_OK;
12}
13
14
15class ATL_NO_VTABLE CMySimpleObj :
16 public CComObjectRootEx<CComSingleThreadModel>,
17 public CComCoClass<CMySimpleObj, &CLSID_MySimpleObj>,
18 public IDispatchImpl<IMySimpleObj, &IID_IMySimpleObj, &LIBID_atlDemoLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
19{
20 STDMETHOD(TestXXX)(BSTR str);
2interface IMySimpleObj : IDispatch{
3 [id(1), helpstring("method Test")] HRESULT TestXXX(void);
4};
5
6
7STDMETHODIMP CMySimpleObj::TestXXX(BSTR str)
8{
9 // TODO: Add your implementation code here
10
11 return S_OK;
12}
13
14
15class ATL_NO_VTABLE CMySimpleObj :
16 public CComObjectRootEx<CComSingleThreadModel>,
17 public CComCoClass<CMySimpleObj, &CLSID_MySimpleObj>,
18 public IDispatchImpl<IMySimpleObj, &IID_IMySimpleObj, &LIBID_atlDemoLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
19{
20 STDMETHOD(TestXXX)(BSTR str);
添加一个叫MyProperty的属性后,多了这些
1
2interface IMySimpleObj : IDispatch{
3 [propget, id(4), helpstring("property MyProperty")] HRESULT MyProperty([out, retval] BSTR* pVal);
4 [propput, id(4), helpstring("property MyProperty")] HRESULT MyProperty([in] BSTR newVal);
5};
6
7
8STDMETHODIMP CMySimpleObj::get_MyProperty(BSTR* pVal)
9{
10 // TODO: Add your implementation code here
11
12 return S_OK;
13}
14
15STDMETHODIMP CMySimpleObj::put_MyProperty(BSTR newVal)
16{
17 // TODO: Add your implementation code here
18
19 return S_OK;
20}
21
22
23class ATL_NO_VTABLE CMySimpleObj {
24 STDMETHOD(get_MyProperty)(BSTR* pVal);
25 STDMETHOD(put_MyProperty)(BSTR newVal);
2interface IMySimpleObj : IDispatch{
3 [propget, id(4), helpstring("property MyProperty")] HRESULT MyProperty([out, retval] BSTR* pVal);
4 [propput, id(4), helpstring("property MyProperty")] HRESULT MyProperty([in] BSTR newVal);
5};
6
7
8STDMETHODIMP CMySimpleObj::get_MyProperty(BSTR* pVal)
9{
10 // TODO: Add your implementation code here
11
12 return S_OK;
13}
14
15STDMETHODIMP CMySimpleObj::put_MyProperty(BSTR newVal)
16{
17 // TODO: Add your implementation code here
18
19 return S_OK;
20}
21
22
23class ATL_NO_VTABLE CMySimpleObj {
24 STDMETHOD(get_MyProperty)(BSTR* pVal);
25 STDMETHOD(put_MyProperty)(BSTR newVal);