(1) COM component registration unloading and smart pointer
A COM component registration and uninstallation
1. The Regsvr32.exe command is used to register the "Active Control"
command format
Register Regsvr32 path/name.dll
Uninstall Regsvr32 /u path/name.dll
2. The simple object produced by ATL is in the form of ".dll"
Visual Stduio automatically registers it when it is compiled into this dll.
For example, if I register a
It is successfully registered, just add /u in front of it to uninstall
If you uninstall the program, you will not be able to use this component, because the registry cannot find the relevant information.
3. View after registration. Open the registry reg is to register detection Edit is to edit
RegEdit to open the registry
4. Search for the clsid of the component in HKEY_CALSSES_ROOT\CLSID\
InprocServer32 puts the address of the DLL file
ProgID is referred to as the PropID of the COM component
presentation component
5. Significance of COM component registration
The registry information records the CLSID of the component, the road strength of the component, the PropID of the component, etc.
COM components are cross-application, web pages, MFC, c# VB which need to identify and create our components, and ultimately
Get the component Road King, and the Road King may change, such as the user chooses a different directory when installing the program, CLSID
Invariant to PropID, we use CLSID or PropID to create components, no matter where the COM component's DLL is located
Starting from vista, registration requires permissions,
2. Smart pointers
1 Introduction
C++ is very dangerous to call COM interface pointers, because the program uses it everywhere
Strictly and correctly call the AddRef() and Release() methods, once a problem occurs, it will cause
The object cannot be freed, or the object is repeatedly deleted
Therefore, C++ programmers have to be careful when using COM interfaces.
Ways to change this: Make it easier
Just use smart pointers.
2. CComPtr is a smart pointer, a template class provided by ATL
Can encapsulate the AddRef() and Release() methods of the COM interface
The variable declared by CComPtr is an object that encapsulates COM
接口指针,这个对象的使用访问跟COM接口的使用几乎一样.
3.智能指针与使用
HRESULT hr = E_FAIL; hr = CoInitialize(NULL); if(SUCCEEDED(hr)) { //智能指针 CComPtr<IHelloSimpleObject>spHello; //创建实例 指定的类标识符创建一个Com对象 //COM类其实就是组件,COM类标识 就是组件标识 hr = spHello.CoCreateInstance(CLSID_HelloSimpleObject); //如果不使用智能指针则要这样 //hr = CoCreateInstance(CLSID_HelloSimpleObject,NULL,CLSCTX_INPROC_SERVER, // IID_IHelloSimpleObject,(LPVOID*)&pHello); if(SUCCEEDED(hr)) { //接口的方法测试 LONG sum = 0; hr = spHello->SumLong(100,100,&sum); //读取描述 BSTR bstrs = SysAllocString(L""); hr = spHello->get_m_desc(&bstrs); SysAllocString(bstrs); bstrs = NULL; //写入 BSTR strb = SysAllocString(L"MFC中测试"); hr = spHello->put_m_desc(strb); SysAllocString(strb); bstrs = NULL; bstrs = SysAllocString(L""); hr = spHello->get_m_desc(&bstrs); SysAllocString(bstrs); bstrs = NULL; } //这里则可以省略 不用调用 //spHello->Release(); } CoUninitialize();
4.智能指针的使用方式与COM接口指针方法相似,也有区别
智能指针创建对象时,内部有一个智能指针.其实他是一个类对象
是一个模板的,
创建好后,这个对象对象内部有一个IHelloSimpleObjet,初始化NULL
IHelloSimpleObject* pIhello, 一个原始的COM接口指针。
5.这两个在CoCreateInstance中使用方法一模一样,因为对智能指针的&材质
会转变成对智能指针内部的 IHelloSimpleObjedt(其实是个模板变量)进行&操作
6.二者使用->操作用法意义一样,因为智能指针的->会转变成
_NoAddRefReleaseOnCComptr<IHelloSimpleObjectSub>*接口变量->操作
_NoAddRefReleaseOnCComptr<IHelloSimpleObjectSub>是IHelloSimpleObjectSub的子类
7.智能指针把AddRef()与Release()放在 private里,所以你不能手动调用。
8.对COM接口指针的赋值是需要AddRef操作,智能指针则不需要,他会智能的执行.
Release也是智能的,不需要我们去管理了.
9.智能指正的变量是一个对象
如果是局部变量,将这个局部变量声明器结束时,执行智能指针西沟
如果是成员变量,在成员所在类对象析构时,执行智能指针的析构
如果是静态变量.将在程序结束时,执行智能指针的析构
三、智能指针注意点
1如果要使用一个智能指针,直接给它赋值NULL,这样内部COM接口指针
也会执行Release操作,来减少引用计数.
2.当对指取地址时(&)要确保智能指针为NULL,因为他返回的是内部的
COM接口指针,如果不为NULL则旧的COM接口指针,将没有执行Release而
A new COM pointer interface is assigned.
3. But you can rest assured, because at this time, the smart pointer is not NULL, the smart pointer
The code reports an error through assert,