2009년 02월 11일
WinINet
Check List
Creating Status Callback Functions
WinInet?
windows Ineternet application programming interface
.png)
.png)
| Function | Description |
|---|---|
| FtpCreateDirectory | Creates a new directory on the server. This function requires a handle created by InternetConnect. |
| FtpDeleteFile | Deletes a file from the server. This function requires a handle created by InternetConnect. |
| FtpFindFirstFile | Starts file enumeration or file search in the current directory. This function requires a handle created by InternetConnect. |
| FtpGetCurrentDirectory | Returns the client's current directory on the server. This function requires a handle created by InternetConnect. |
| FtpGetFile | Retrieves a file from the server. This function requires a handle created by InternetConnect. |
| FtpOpenFile | Initiates access to a file on the server for either reading or writing. This function requires a handle created by InternetConnect. |
| FtpPutFile | Writes a file to the server. This function requires a handle created by InternetConnect. |
| FtpRemoveDirectory | Deletes a directory on the server. This function requires a handle created by InternetConnect. |
| FtpRenameFile | Renames a file on the server. This function requires a handle created by InternetConnect. |
| FtpSetCurrentDirectory | Changes the client's current directory on the server. This function requires a handle created by InternetConnect. |
| InternetWriteFile | Writes data to an open file on the server. This function requires a handle created by FtpOpenFile. |
- // Starting FTP session
- #include "windows.h"
#include "strsafe.h"
#include "wininet.h"
#define FTP_FUNCTIONS_BUFFER_SIZE MAX_PATH+8 - // 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 ); - 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; - 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;
} - 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 ) ); - if( ( dwError = GetLastError( ) ) == ERROR_NO_MORE_FILES )
{
InternetCloseHandle(hFind);
return( TRUE );
}
szFailedFunctionName = TEXT( "FtpFindNextFile( )" );
InternetCloseHandle( hFind ); - DisplayDirError_3:
InternetErrorOut( hDlg, dwError, szFailedFunctionName );
return( FALSE ); - DisplayDirError_2:
InternetCloseHandle( hFind );
DisplayDirError_1:
MessageBox( hDlg,
(LPCTSTR) szMsgBuffer,
TEXT( "DisplayFtpDir( ) Problem" ),
MB_OK | MB_ICONERROR );
return( retVal );
}
- Navigation Directories
- 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; - dwSize = FTP_FUNCTIONS_BUFFER_SIZE;
- if( !GetDlgItemText( hDlg, nDirNameId, szNewDirName, dwSize ) )
{
szFailedFunctionName = TEXT( "GetDlgItemText" );
goto ChangeFtpDirError;
} - if ( !FtpGetCurrentDirectory( hConnection, szOldDirName, &dwSize ))
{
szFailedFunctionName = TEXT( "FtpGetCurrentDirectory" );
goto ChangeFtpDirError;
} - if( !SetDlgItemText( hDlg, nDirNameId, szOldDirName ) )
{
szFailedFunctionName = TEXT( "SetDlgItemText" );
goto ChangeFtpDirError;
} - if( !FtpSetCurrentDirectory( hConnection, szNewDirName ) )
{
szFailedFunctionName = TEXT( "FtpSetCurrentDirectory" );
goto ChangeFtpDirError;
}
return( DisplayFtpDir( hDlg, hConnection, 0, nListBoxId ) ); - ChangeFtpDirError:
InternetErrorOut( hDlg, GetLastError( ), szFailedFunctionName );
DisplayFtpDir( hDlg, hConnection, INTERNET_FLAG_RELOAD, nListBoxId);
return( FALSE );
}
- Getting Files on an FTP Server
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..." );- 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 );
} - dwTransferType =
( MessageBox( hDlg,
szAsciiQuery,
szBoxTitle,
MB_YESNO ) == IDYES ) ?
FTP_TRANSFER_TYPE_ASCII : FTP_TRANSFER_TYPE_BINARY;
dwTransferType |= INTERNET_FLAG_RELOAD; - if( !FtpGetFile( hConnection, szFtpFileName, szLocalFileName, FALSE,
FILE_ATTRIBUTE_NORMAL, dwTransferType, 0 ) )
{
InternetErrorOut( hDlg, GetLastError( ), TEXT( "FtpGetFile" ) );
return( FALSE );
} - MessageBox( hDlg,
( dwTransferType ==
(FTP_TRANSFER_TYPE_ASCII | INTERNET_FLAG_RELOAD)) ?
szAsciiDone : szBinaryDone, szBoxTitle, MB_OK );
return( TRUE );
}
- Placing Files on an FTP Server
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..." );- 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 );
} - dwTransferType =
( MessageBox( hDlg,
szASCIIQuery,
szBoxTitle,
MB_YESNO ) == IDYES ) ?
FTP_TRANSFER_TYPE_ASCII : FTP_TRANSFER_TYPE_BINARY; - if( !FtpPutFile( hConnection,
szLocalFileName,
szFtpFileName,
dwTransferType,
0 ) )
{
InternetErrorOut( hDlg, GetLastError( ), TEXT( "FtpGetFile" ) );
return( FALSE );
} - 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)



