c++ - 如何在 C++ 中包装 CFtpFileFind 示例?

标签 c++ c windows winapi wininet

试图包装 this C++ 中的简短示例。 (自从我这样做以来已经有一段时间了)。

int main(int argc, char* argv[])
{       
    //Objects
    CFtpConnection* pConnect = NULL; //A pointer to a CFtpConnection object
    ftpClient UploadExe; //ftpClient object
 

    pConnect = UploadExe.Connect();    
    UploadExe.GetFiles(pConnect);

system("PAUSE");
    return 0;
}

.h-

class ftpClient
{
    public:    
    
    ftpClient();        
    CFtpConnection* Connect();
    void GetFiles(CFtpConnection* pConnect);    

};

.cpp-

//constructor
ftpClient::ftpClient()
{

}

CFtpConnection* ftpClient::Connect()
{
    // create a session object to initialize WININET library
    // Default parameters mean the access method in the registry
    // (that is, set by the "Internet" icon in the Control Panel)
    // will be used.

    CInternetSession sess(_T("FTP"));

    CFtpConnection* pConnect = NULL;

    try
    {
        // Request a connection to ftp.microsoft.com. Default
        // parameters mean that we'll try with username = ANONYMOUS
        // and password set to the machine name @ domain name
        pConnect = sess.GetFtpConnection("localhost", "sysadmin", "ftp", 21, FALSE );

    }
    catch (CInternetException* pEx)
    {
        TCHAR sz[1024];
        pEx->GetErrorMessage(sz, 1024);
        printf("ERROR!  %s\n", sz);
        pEx->Delete();
     }

    // if the connection is open, close it  MOVE INTO CLOSE FUNCTION
   // if (pConnect != NULL) 
   // {
   //     pConnect->Close();
   //     delete pConnect;
   // }


    return pConnect;

}

void ftpClient::GetFiles(CFtpConnection* pConnect)
{
        // use a file find object to enumerate files
        CFtpFileFind finder(pConnect);
   

if (pConnect != NULL) 
{
   printf("ftpClient::GetFiles - pConnect NOT NULL");
}


     // start looping
        BOOL bWorking = finder.FindFile("*"); //<---ASSERT ERROR

      //  while (bWorking)
      //  {
     //       bWorking = finder.FindNextFile();
     //       printf("%s\n", (LPCTSTR) finder.GetFileURL());
     //   }


}

所以基本上将连接和文件操作分成了 2 个函数。 findFile() 函数正在抛出断言。 (进入findFile(),具体是inet.cpp中的第一个ASSERT_VALID(m_pConnection)。)

我传递 arround CFtpConnection* pConnect 的方式如何?

编辑 - 看起来 CObject vfptr 在 GetFiles() 函数中被覆盖 (0X00000000)。

感谢任何帮助。谢谢。

最佳答案

回答:

这个session对象必须在Connection函数中分配,带指针
声明为类的成员函数。在函数内创建对象时,
"CInternetSession sess(_T("MyProgram/1.0"));" 对象/ session 将在函数退出时终止,并被抛出堆栈。发生这种情况时,我们不能在其他函数中使用 pConnect 指针。

WinInet 对象有一个层次结构, session 位于顶层。如果 session 消失,则无法使用其他任何东西。因此,我们必须使用new来分配内存中的对象,使其在该函数退出后仍然存在。

关于c++ - 如何在 C++ 中包装 CFtpFileFind 示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2156902/

相关文章:

c - 如何处理大型二维数组

windows - 如何从自定义凭据提供程序中的更改密码场景中获取新密码

windows - 如何获取窗口的调整角的大小(以像素为单位)

c++ - 使用 std::vector 编写可调整大小的 vector 时出现问题

c++ - makefile 的问题

c++ - C++ 中的正则表达式(正则表达式)错误: "terminate called after throwing an instance of ' std::regex_error' what(): regex_error 中止(核心转储)

windows - Pyspark to_date() 函数在 Windows 和 WSL Ubuntu 上给出了不同的答案

c++ - C++中数组声明的一个问题

c - 设置 -O3 时到底会发生什么

c - 为什么 strtol() 对于非常大的数字返回 -1?