WinINet

Check List


Creating Status Callback Functions


Using WinInet


Error Code


WinInet?


windows Ineternet application programming interface


 FTP functions dependent on FTP session handle returned by InternetConnect


 


FTP functions that return HINTERNET handles


 


 





















































FunctionDescription
FtpCreateDirectoryCreates a new directory on the server. This function requires a handle created by InternetConnect.
FtpDeleteFileDeletes a file from the server. This function requires a handle created by InternetConnect.
FtpFindFirstFileStarts file enumeration or file search in the current directory. This function requires a handle created by InternetConnect.
FtpGetCurrentDirectoryReturns the client's current directory on the server. This function requires a handle created by InternetConnect.
FtpGetFileRetrieves a file from the server. This function requires a handle created by InternetConnect.
FtpOpenFileInitiates access to a file on the server for either reading or writing. This function requires a handle created by InternetConnect.
FtpPutFileWrites a file to the server. This function requires a handle created by InternetConnect.
FtpRemoveDirectoryDeletes a directory on the server. This function requires a handle created by InternetConnect.
FtpRenameFileRenames a file on the server. This function requires a handle created by InternetConnect.
FtpSetCurrentDirectoryChanges the client's current directory on the server. This function requires a handle created by InternetConnect.
InternetWriteFileWrites data to an open file on the server. This function requires a handle created by FtpOpenFile.

 


 



  1. // Starting FTP session

  2. #include "windows.h"

    #include "strsafe.h"

    #include "wininet.h"

    #define  FTP_FUNCTIONS_BUFFER_SIZE          MAX_PATH+8

  3. // For sample source code of the extern InternetErrorOut( ) function,

    // see the "Handling Errors" topic under "Using WinInet" in the SDK

    // documentation

    extern BOOL WINAPI InternetErrorOut( HWND hWnd, DWORD dwError,

                                         LPCTSTR szFailingFunctionName );

  4. BOOL WINAPI DisplayFtpDir(

                               HWND hDlg,

                               HINTERNET hConnection,

                               DWORD dwFindFlags,

                               int nListBoxId )

    {

      WIN32_FIND_DATA dirInfo;

      HINTERNET       hFind;

      DWORD           dwError;

      BOOL            retVal = FALSE;

      TCHAR           szMsgBuffer[FTP_FUNCTIONS_BUFFER_SIZE];

      TCHAR           szFName[FTP_FUNCTIONS_BUFFER_SIZE];

      LPCTSTR         szFailedFunctionName;

  5.   SendDlgItemMessage( hDlg, nListBoxId, LB_RESETCONTENT, 0, 0 );

      hFind = FtpFindFirstFile( hConnection, TEXT( "*.*" ),

                                &dirInfo, dwFindFlags, 0 );

      if ( hFind == NULL )

      {

        dwError = GetLastError( );

        if( dwError == ERROR_NO_MORE_FILES )

        {

          StringCchCopy( szMsgBuffer, FTP_FUNCTIONS_BUFFER_SIZE,

            TEXT( "No files found at FTP location specified." ) );

          retVal = TRUE;

          goto DisplayDirError_1;

        }

        szFailedFunctionName = TEXT( "FtpFindFirstFile" );

        goto DisplayDirError_3;

      }

  6.   do

      {

        if( FAILED( StringCchCopy( szFName, FTP_FUNCTIONS_BUFFER_SIZE,

                      dirInfo.cFileName ) ) ||

            ( ( dirInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) &&

            ( FAILED( StringCchCat( szFName, FTP_FUNCTIONS_BUFFER_SIZE,

             TEXT( " <DIR> " ) ) ) ) ) )

        {

          StringCchCopy( szMsgBuffer, FTP_FUNCTIONS_BUFFER_SIZE,

            TEXT( "Failed to copy a file or directory name." ) );

          retVal = FALSE;

          goto DisplayDirError_2;

        }

        SendDlgItemMessage( hDlg, nListBoxId, LB_ADDSTRING,

                            0, (LPARAM) szFName );

      } while( InternetFindNextFile( hFind, (LPVOID) &dirInfo ) );

  7.   if( ( dwError = GetLastError( ) ) == ERROR_NO_MORE_FILES )

      {

        InternetCloseHandle(hFind);

        return( TRUE );

      }

      szFailedFunctionName = TEXT( "FtpFindNextFile( )" );

      InternetCloseHandle( hFind );

  8. DisplayDirError_3:

      InternetErrorOut( hDlg, dwError, szFailedFunctionName );

      return( FALSE );

  9. DisplayDirError_2:

      InternetCloseHandle( hFind );

    DisplayDirError_1:

      MessageBox( hDlg,

        (LPCTSTR) szMsgBuffer,

        TEXT( "DisplayFtpDir( ) Problem" ),

        MB_OK | MB_ICONERROR );

      return( retVal );

    }


 



  1.  Navigation Directories

  2.  

  3.  BOOL WINAPI ChangeFtpDir( HWND hDlg,

                                             HINTERNET hConnection,

                                             int nDirNameId,

                                             int nListBoxId )

    {

      DWORD dwSize;

      TCHAR szNewDirName[FTP_FUNCTIONS_BUFFER_SIZE];

      TCHAR szOldDirName[FTP_FUNCTIONS_BUFFER_SIZE];

      TCHAR* szFailedFunctionName;

  4.   dwSize = FTP_FUNCTIONS_BUFFER_SIZE;

  5.   if( !GetDlgItemText( hDlg, nDirNameId, szNewDirName, dwSize ) )

      {

        szFailedFunctionName = TEXT( "GetDlgItemText" );

        goto ChangeFtpDirError;

      }

  6.   if ( !FtpGetCurrentDirectory( hConnection, szOldDirName, &dwSize ))

      {

        szFailedFunctionName = TEXT( "FtpGetCurrentDirectory" );

        goto ChangeFtpDirError;

      }

  7.   if( !SetDlgItemText( hDlg, nDirNameId, szOldDirName ) )

      {

        szFailedFunctionName = TEXT( "SetDlgItemText" );

        goto ChangeFtpDirError;

      }

  8.   if( !FtpSetCurrentDirectory( hConnection, szNewDirName ) )

      {

        szFailedFunctionName = TEXT( "FtpSetCurrentDirectory" );

        goto ChangeFtpDirError;

      }

      return( DisplayFtpDir( hDlg, hConnection, 0, nListBoxId ) );

  9. ChangeFtpDirError:

      InternetErrorOut( hDlg, GetLastError( ), szFailedFunctionName );

      DisplayFtpDir( hDlg, hConnection, INTERNET_FLAG_RELOAD, nListBoxId);

      return( FALSE );

    }


 



  1. Getting Files on an FTP Server

  2.  



  3. BOOL WINAPI GetFtpFile( HWND hDlg, HINTERNET hConnection,

                            int nFtpFileNameId, int nLocalFileNameId )

    {

      TCHAR szFtpFileName[FTP_FUNCTIONS_BUFFER_SIZE];

      TCHAR szLocalFileName[FTP_FUNCTIONS_BUFFER_SIZE];

      DWORD dwTransferType;

      TCHAR szBoxTitle[] = TEXT( "Download FTP File" );

      TCHAR szAsciiQuery[] =

        TEXT("Do you want to download as ASCII text?(Default is binary)");

      TCHAR szAsciiDone[] =

        TEXT( "ASCII Transfer completed successfully..." );

      TCHAR szBinaryDone[] =

        TEXT( "Binary Transfer completed successfully..." );

  4.   if( !GetDlgItemText( hDlg, nFtpFileNameId, szFtpFileName,

                           FTP_FUNCTIONS_BUFFER_SIZE ) ||

          !GetDlgItemText( hDlg, nLocalFileNameId, szLocalFileName,

                           FTP_FUNCTIONS_BUFFER_SIZE ) )

      {

        MessageBox( hDlg,

                    TEXT( "Target File or Destination File Missing" ),

                    szBoxTitle,

                    MB_OK | MB_ICONERROR );

        return( FALSE );

      }

  5.   dwTransferType =

        ( MessageBox( hDlg,

                      szAsciiQuery,

                      szBoxTitle,

                      MB_YESNO ) == IDYES ) ?

                      FTP_TRANSFER_TYPE_ASCII : FTP_TRANSFER_TYPE_BINARY;

      dwTransferType |= INTERNET_FLAG_RELOAD;

  6.   if( !FtpGetFile( hConnection, szFtpFileName, szLocalFileName, FALSE,

                       FILE_ATTRIBUTE_NORMAL, dwTransferType, 0 ) )

      {

        InternetErrorOut( hDlg, GetLastError( ), TEXT( "FtpGetFile" ) );

        return( FALSE );

      }

  7.   MessageBox( hDlg,

                  ( dwTransferType ==

                       (FTP_TRANSFER_TYPE_ASCII | INTERNET_FLAG_RELOAD)) ?

                        szAsciiDone : szBinaryDone, szBoxTitle, MB_OK );

      return( TRUE );

    }


 



  1. Placing Files on an FTP Server

  2.  



  3. BOOL WINAPI PutFtpFile( HWND hDlg, HINTERNET hConnection,

                            int nFtpFileNameId, int nLocalFileNameId )

    {

      TCHAR szFtpFileName[FTP_FUNCTIONS_BUFFER_SIZE];

      TCHAR szLocalFileName[FTP_FUNCTIONS_BUFFER_SIZE];

      DWORD dwTransferType;

      TCHAR szBoxTitle[] = TEXT( "Upload FTP File" );

      TCHAR szASCIIQuery[] =

        TEXT("Do you want to upload as ASCII text? (Default is binary)");

      TCHAR szAsciiDone[] =

        TEXT( "ASCII Transfer completed successfully..." );

      TCHAR szBinaryDone[] =

        TEXT( "Binary Transfer completed successfully..." );

  4.   if( !GetDlgItemText( hDlg, nFtpFileNameId, szFtpFileName,

                           FTP_FUNCTIONS_BUFFER_SIZE ) ||

          !GetDlgItemText( hDlg, nLocalFileNameId, szLocalFileName,

                           FTP_FUNCTIONS_BUFFER_SIZE ) )

      {

        MessageBox( hDlg,

                    TEXT("Target File or Destination File Missing"),

                    szBoxTitle,

                    MB_OK | MB_ICONERROR );

        return( FALSE );

      }

  5.   dwTransferType =

        ( MessageBox( hDlg,

                      szASCIIQuery,

                      szBoxTitle,

                      MB_YESNO ) == IDYES ) ?

        FTP_TRANSFER_TYPE_ASCII : FTP_TRANSFER_TYPE_BINARY;

  6.   if( !FtpPutFile( hConnection,

                       szLocalFileName,

                       szFtpFileName,

                       dwTransferType,

                       0 ) )

      {

        InternetErrorOut( hDlg, GetLastError( ), TEXT( "FtpGetFile" ) );

        return( FALSE );

      }

  7.   MessageBox( hDlg,

                  ( dwTransferType == FTP_TRANSFER_TYPE_ASCII ) ?

                    szAsciiDone : szBinaryDone, szBoxTitle, MB_OK );

      return( TRUE );  // Remember to refresh directory listing

    }


 


 


이 글은 스프링노트에서 작성되었습니다.

by 사내양 | 2009/02/11 09:14 | 트랙백 | 덧글(0)

◀ 이전 페이지          다음 페이지 ▶