⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 globalhelperfunc.cpp

📁 vc++网络程序设计实例详解 人民邮电出版社1-2章源码
💻 CPP
字号:
#include "GlobalHelperFunc.h"

void PopError(DWORD dwError)
{
    CString strErrorDescription = ::FormatError(dwError);
    
   ::AfxMessageBox(strErrorDescription);

}


CString FormatError(DWORD dwError)
{
    HLOCAL hlocal = NULL;   // Buffer that gets the error message string
    CString strErrorDescription = _T("");

    // Get the error code's textual description 
    BOOL bOK = ::FormatMessage(
                    FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, 
                    NULL, 
                    dwError, 
                    MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), 
                    reinterpret_cast<PTSTR>(&hlocal),
                    0,
                    NULL
                    );

    if (hlocal != NULL) 
    {
        strErrorDescription = static_cast<PCSTR>(hlocal);
        ::LocalFree(hlocal);
    }
    else
    {
        strErrorDescription = _T("Failed to retrive error description !");
    }

    return strErrorDescription;
}


void PopLastError()
{
    DWORD dwLastError = ::GetLastError();
    ::PopError(dwLastError);
}


CString FormatLastError()
{
    DWORD dwLastError = ::GetLastError();

    CString strErrorDescription = ::FormatError(dwLastError);

    return strErrorDescription;
}


void LaunchApp(CString strAppPath, CString strCommandLine)
{
    TCHAR* szAppPath     = strAppPath.GetBuffer(0);
    TCHAR* szCommandline = strCommandLine.GetBuffer(0);

    STARTUPINFO si = {sizeof(si)};
    PROCESS_INFORMATION pi;

    BOOL bSuccess = ::CreateProcess(
                          szAppPath,
                          szCommandline,
                          NULL,
                          NULL,
                          FALSE,
                          0,
                          NULL,
                          NULL,
                          &si,
                          &pi
                          );
                        
    if (bSuccess)
    {
        // Added by Prateek Kaul on Sep. 28, 2002

        // --*******PREVENT MEMORY LEAKS---*************
        // This function was written for this purpose, as 
        // developers forget to call the next 2 calls to
        // close handle

        // Close the thread handle as soon as it is no longer needed!
        ::CloseHandle(pi.hThread);

        // Close the process handle as soon as it is no longer needed.
        ::CloseHandle(pi.hProcess);
    }

    else
    {
       ::PopLastError();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -