设为首页 加入收藏

TOP

C++编程丨让你的C++代码变的更加健壮!(四)
2019-03-25 18:07:57 】 浏览:239
Tags:编程 代码 更加 健壮
Loc) {
// Failed to create IWbemLocator object. return FALSE; } hres = pLoc->ConnectServer( _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace NULL, // User name. NULL = current user NULL, // User password. NULL = current 0, // Locale. NULL indicates current NULL, // Security flags. 0, // Authority (e.g. Kerberos) 0, // Context object &pSvc // pointer to IWbemServices proxy ); if (FAILED(hres) || !pSvc) { // Couldn't conect server if(pLoc) pLoc->Release(); return FALSE; } hres = CoSetProxyBlanket( pSvc, // Indicates the proxy to set RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx NULL, // Server principal name RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx NULL, // client identity EOAC_NONE // proxy capabilities ); if (FAILED(hres)) { // Could not set proxy blanket. if(pSvc) pSvc->Release(); if(pLoc) pLoc->Release(); return FALSE; }

Using Smart Pointers

如果你经常使用用享对象指针,如COM 接口等,那么建议使用智能指针来处理。智能指针会自动帮助你维护对象引用记数,并且保证你不会访问到被删除的对象。这样,不需要关心和控制接口的生命周期。关于智能指针的进一步知识可以看看Smart Pointers – What, Why, Which??和 Implementing a Simple Smart Pointer in C++这两篇文章。

如面是一个展示使用ATL’s CComPtr template 智能指针的代码,该部分代码来至于MSDN。

#include <windows.h>

#include <shobjidl.h>

#include <atlbase.h> // Contains the declaration of CComPtr.

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)

{

    HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |

        COINIT_DISABLE_OLE1DDE);

    if (SUCCEEDED(hr))

    {

        CComPtr<IFileOpenDialog> pFileOpen;

        // Create the FileOpenDialog object.

        hr = pFileOpen.CoCreateInstance(__uuidof(FileOpenDialog));

        if (SUCCEEDED(hr))

        {

            // Show the Open dialog box.

            hr = pFileOpen->Show(NULL);

            // Get the file name from the dialog box.

            if (SUCCEEDED(hr))

            {

                CComPtr<IShellItem> pItem;

                hr = pFileOpen->GetResult(&pItem);

                if (SUCCEEDED(hr))

                {

                    PWSTR pszFilePath;

                    hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);

                    // Display the file name to the user.

                    if (SUCCEEDED(hr))

                    {

                        MessageBox(NULL, pszFilePath, L"File Path", MB_OK);

                        CoTaskMemFree(pszFilePath);

                    }

                }

                // pItem goes out of scope.

            }

            // pFileOpen goes out of scope.

        }

        CoUninitialize();

    }

    return 0;

}

Using == Operator Carefully

先来看看如下代码;

CVehicle* pVehicle = GetCurrentVehicle();

// Validate pointer

if(pVehicle==NULL) // Using == operator to compare pointer with NULL

   return FALSE;

// Do something with the pointer

pVehicle->Run();

上面的代码是正确的,用语指针检测。但是如果不小心用“=”替换了“==”,如下代码;

CVehicle* pVehicle = GetCurrentVehicle();

// Validate pointer

if(pVehicle=NULL) // Oops! A mistyping here!

return FALSE;

// Do something with the pointer

pVehicle->Run(); // Crash!!!

看看上面的代码,这个的一个失误将导致程序崩溃。

这样的错误是可以避免的,只需要将等号左右两边交换一下就可以了。如果在修改代码的时候,你不小心产生这种失误,这个错误在程序编译的时候将被检测出来。

// Validate pointer

if(NULL==pVehicle)

// Exchange left side and right side of the equality operator

    return FALSE;

// Validate pointer
首页 上一页 1 2 3 4 5 下一页 尾页 4/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇#leetcode刷题之路33-搜索旋转排.. 下一篇5分钟速成C++14多线程编程

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目