c++ - 如何从 GetFile() 获取具体的错误信息?

标签 c++ c winapi file ftp

void GetFtpFile(LPCTSTR pszServerName, LPCTSTR pszRemoteFile, LPCTSTR pszLocalFile)
{
   CInternetSession session(_T("My FTP Session"));
   CFtpConnection* pConn = NULL;

   pConn = session.GetFtpConnection(pszServerName);
   //get the file
   if (!pConn->GetFile(pszRemoteFile, pszLocalFile))
   {
      //display an error
   }
   delete pConn;
   session.Close();
}

如何从 GetFile() 获取具体的错误信息?

谢谢。

最佳答案

根据MSDN :

Return Value

Nonzero if successful;

otherwise 0. If the call fails, the Win32 function GetLastError may be called to determine the cause of the error.

GetLastError()返回错误代码,但您可以调用 FormatMessage()从错误代码中获取人类可读的字符串。这是一个可以为您完成此操作的实用函数:

std::string formatwinerr(unsigned long errCode)
{
    LPVOID lpMsgBuf;

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM ,
        0,
        errCode,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    std::string ret((const char*)lpMsgBuf);

    LocalFree(lpMsgBuf);

    return ret;
}

关于c++ - 如何从 GetFile() 获取具体的错误信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2158438/

相关文章:

c++ - 参数列表中间的默认参数?

c++ - mk-livestatus 1.1.6p1 无法在 Ubuntu 12.04 上编译(精确)

c - 为什么 modf 的 intpart 输出强制为 double ?

c - ucontext.h 和 uc_link 没有从主线程返回

windows - 整数原子的使用

c++ - 如何保持 Windows ListView 控件和它们映射到的对象同步?

c++ - redis::protocol_error 来自 redis-cplusplus-client 的异常

c - 打印网络掩码

windows - 如何为仅用于远程 COM 对象的 TLB 创建 list 文件?

c++ - 他们如何使 "Get Windows 10"图标默认具有 "Show icon and notifications"可见性?