#pragma once
#include <windows.h>
#include <wininet.h>
#include <commctrl.h>
#define FTP_IP L"172.16.12.65"
#define FTP_USER_NAME L"karma"
#define FTP_PASSWD L"5018"
#define MAX_NAME_SIZE 256
#define DOWN_DIR L"files"
typedef struct _ftp_data{
HINTERNET internet_handle;
HINTERNET ftp_handle;
TCHAR server_name[MAX_NAME_SIZE];
TCHAR user_name[MAX_NAME_SIZE];
TCHAR passwd[MAX_NAME_SIZE];
}FtpData;
void ftp_init(FtpData *ftp_data);
BOOL ftp_connect(FtpData *ftp_data);
void ftp_disconnect(FtpData *ftp_data);
BOOL ftp_download(FtpData *ftp_data, TCHAR *file_name);
================================
#include <windows.h>
#include <wininet.h>
#include <commctrl.h>
#include <stdio.h>
#include <assert.h>
#include "Ftp.h"
#pragma comment(lib, "Wininet.lib")
BOOL ftp_connect(FtpData *ftp_data)
{
assert(ftp_data != NULL);
ftp_data->internet_handle = InternetOpen(L"ftp_test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(ftp_data->internet_handle == NULL){
return FALSE;
}
assert(ftp_data->server_name != NULL);
assert(ftp_data->user_name != NULL);
assert(ftp_data->passwd != NULL);
ftp_data->ftp_handle = InternetConnect(ftp_data->internet_handle, ftp_data->server_name, INTERNET_DEFAULT_FTP_PORT,
ftp_data->user_name, ftp_data->passwd, INTERNET_SERVICE_FTP, 0, 0);
if(ftp_data->ftp_handle == NULL) {
InternetCloseHandle(ftp_data->internet_handle);
return FALSE;
}
return TRUE;
}
void ftp_disconnect(FtpData *ftp_data)
{
assert(ftp_data != NULL);
ftp_data->internet_handle = NULL;
ftp_data->ftp_handle = NULL;
InternetCloseHandle(ftp_data->ftp_handle);
InternetCloseHandle(ftp_data->internet_handle);
}
BOOL ftp_download(FtpData *ftp_data, TCHAR *file_name)
{
assert(ftp_data != NULL);
assert(file_name != NULL);
TCHAR new_file[MAX_PATH];
TCHAR buf[MAX_PATH];
GetCurrentDirectory(sizeof(buf), buf);
wsprintf(new_file, L"%s\\%s\\%s", buf, DOWN_DIR, file_name);
wprintf(L"%s\n", new_file);
return FtpGetFile(ftp_data->ftp_handle, file_name, new_file, FALSE,
FILE_ATTRIBUTE_NORMAL,
FTP_TRANSFER_TYPE_BINARY | INTERNET_FLAG_RELOAD,
0);
}