环境vc60 + win2000 + 补丁
1、建立工程Test
2、在TestApp中增加成员函数 void Connect()
3、在Connect() 中增加代码:
         CInternetSession session ;
CFtpConnection * pFtpConnection ; try
{
pFtpConnection = session.GetFtpConnection (....) ;
}
catch(...)
         { .... }    这段代码运行正常,然后我现在想把 session 和 pFtpConnection 这两个变量在TestApp中进行声明,也就是做为TestApp的public成员。去掉Connect()中的变量去掉。变成如下形式。
         // CInternetSession session ;
// CFtpConnection * pFtpConnection ;         try
{
pFtpConnection = session.GetFtpConnection (....) ;
}
catch(...)
         { .... }程序就不能运行了,一启动程序就报错。我应该怎样改,才能使程序能正常运行?做为正在学习的新手,实再是没有多少分可送,请老手们指点一下,谢谢。

解决方案 »

  1.   

    很明显这个指针为空,造成访问异常....// 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.public:
        CInternetSession sess(_T("My FTP Session"));
        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(_T("ftp.microsoft.com"));   // use a file find object to enumerate files
       CFtpFileFind finder(pConnect);   // start looping
       BOOL bWorking = finder.FindFile(_T("*"));   while (bWorking)
       {
          bWorking = finder.FindNextFile();
          _tprintf_s(_T("%s\n"), (LPCTSTR)finder.GetFileURL());
       }
    }
    catch (CInternetException* pEx)
    {
       TCHAR sz[1024];
       pEx->GetErrorMessage(sz, 1024);
       _tprintf_s(_T("ERROR!  %s\n"), sz);
       pEx->Delete();
    }
      

  2.   

    我想应该是你这里有错误吧?(GetFtpConnection...)上面的代码修正一下:#include <afxwin.h>
    #include <afxinet.h>
    #include <stdio.h>class MySesion : public CWinApp
    {
    public:
        CInternetSession    sess;
        CFtpConnection*     pConnect;public:
        BOOL Connect();
    };BOOL MySesion::Connect()
    {
        try
        {
            pConnect = sess.GetFtpConnection(_T("ftp.microsoft.com"));
            CFtpFileFind finder(pConnect);
            BOOL bWorking = finder.FindFile(_T("*"));        while (bWorking)
            {
                bWorking = finder.FindNextFile();
                printf(_T("%s\n"), (LPCTSTR)finder.GetFileURL());
            }
            return TRUE;
        }
        catch (CInternetException* pEx)
        {
            TCHAR sz[1024];
            pEx->GetErrorMessage(sz, 1024);
            printf(_T("ERROR!  %s\n"), sz);
            pEx->Delete();
        }
        return FALSE;
    }void main()
    {
        MySesion ms;
        if(ms.Connect())
            printf("操作成功!\r\n");
        else
            printf("操作失败!\r\n");
    }
    cl Connect.cpp /MT运行结果:
      

  3.   

    没看明白,如果是因为指针为空,为什么在CONNECT()函数中就没有问题?把它变为TestApp的成员就不行?
      

  4.   

    如果是(GetFtpConnection...)有问题,那么我在没有改动之前程序运行是没有问题的,
    try
    {
    pFtpConnection = session.GetFtpConnection("192.168.0.26","test_user","abc123",NULL,FALSE) ;
    }在我把 
    CInternetSession session ;
    CFtpConnection * pFtpConnection ;
    放到TestApp()里就不行了。另外我建立的不是控制台的工程,是对话框工程。
      

  5.   


    你的App类构造函数
    CXXApp::CXXApp() : session(_T("My Session"))
    {
     ....
    }