Ok, what I thought would just be a tiny program, done in an hour or so, again gives me a bit of a headache.
All I'm trying to do is open a connection to an FTP server and get a list of files/download a file.
Connection seems to work fine.
LPCWSTR user, password,...
LPCWSTR folder = "\xyz";
FTPOpen = InternetOpen(LPCTSTR("Title"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (!FTPOpen)
return false;
std::cout << "Internet connection opened" << std::endl;
FTPConnect = InternetConnect(FTPOpen, url, 5553, user, password, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
if (!FTPConnect)
return false;
std::cout << "Connected to FTP server" << std::endl;
if (FtpSetCurrentDirectory(FTPConnect, folder) == FALSE)
return false;
std::cout << "Moved to folder." << std::endl;
return true;
This works fine (it also works if folder ist just "xyz" or "/xyz").
Now when I try to download a file or use FtpFindFirstFile(...), it always fails.
WIN32_FIND_DATA dirInfo;
HINTERNET hFind;
DWORD dwError;
BOOL retVal = FALSE;
TCHAR szMsgBuffer[FTP_FUNCTIONS_BUFFER_SIZE];
TCHAR szFName[FTP_FUNCTIONS_BUFFER_SIZE];
if (FtpGetFile(FTPConnect, L"filename.zip", L"C:\filename.zip", FALSE,
FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_UNKNOWN | INTERNET_FLAG_RESYNCHRONIZE, 0) == FALSE) {
dwError = GetLastError();
if (dwError == ERROR_INTERNET_EXTENDED_ERROR) {
DWORD code;
DWORD size_needed = 0;
InternetGetLastResponseInfo(&code, NULL, &size_needed);
char *message = (char*)malloc(size_needed + 1);
InternetGetLastResponseInfo(&code, LPWSTR(message), &size_needed);
std::cout << "ERROR FtpGetFile(): " << message << std::endl;
}
std::cout << "Fileload not successful." << std::endl;
}
hFind = FtpFindFirstFile(FTPConnect, L"*", &dirInfo, INTERNET_FLAG_DONT_CACHE, 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;
return;
}
DWORD code;
DWORD size_needed = 0;
InternetGetLastResponseInfo(&code, NULL, &size_needed);
char *message = (char*)malloc(size_needed + 1);
InternetGetLastResponseInfo(&code, LPWSTR(message), &size_needed);
std::cout << "ERROR FtpFindFirstFile(): " << message << std::endl;
StringCchCopy(szMsgBuffer, FTP_FUNCTIONS_BUFFER_SIZE, TEXT("FtpFindFirstFile failed."));
goto DisplayDirError;
return;
}
The error message I get from InternetGetLastResponse() is "2", which - according to msdn - is "FileNotFound". I'm sure the file exists though, and at least FtpFindFirstFile() shouldn't return that same error. hFind is always NULL.
I googled alot already, but just can't find out what I'm doing wrong. Can anyone help please?
Thanks so far.