FTP from Axapta

from Development Axapta by (Dahlsgaard Jan) add at the class declaration of the WinInet class the following: DLLFunction internetConnect; DLLFunction ftpGetFile; DLLFunction ftpPutFile; DLLFunction setCurrentDirectory;

DLLFunction ftpFindFirstFile; DLLFunction internetFindNextFile; DLLFunction ftpDeleteFile; DLLFunction ftpRenameFile;

int _handle; int _result;

2
 * 1) localmacro.FTP_TRANSFER_TYPE_BINARY
 * 1) endmacro

and this in the 'new':

internetConnect = new DLLFunction(_winInet,"InternetConnectA"); internetConnect.returns(ExtTypes::DWORD); internetConnect.arg(ExtTypes::DWORD); internetConnect.arg(ExtTypes::STRING); internetConnect.arg(ExtTypes::DWORD); internetConnect.arg(ExtTypes::STRING); internetConnect.arg(ExtTypes::STRING); internetConnect.arg(ExtTypes::DWORD); internetConnect.arg(ExtTypes::DWORD); internetConnect.arg(ExtTypes::DWORD); ftpGetFile = new DLLFunction(_winInet,"FtpGetFileA"); ftpGetFile.returns(ExtTypes::DWORD); ftpGetFile.arg(ExtTypes::DWORD); ftpGetFile.arg(ExtTypes::STRING); ftpGetFile.arg(ExtTypes::STRING); ftpGetFile.arg(ExtTypes::DWORD); ftpGetFile.arg(ExtTypes::DWORD); ftpGetFile.arg(ExtTypes::DWORD); ftpGetFile.arg(ExtTypes::DWORD); ftpPutFile = new DLLFunction(_winInet,"FtpPutFileA"); ftpPutFile.returns(ExtTypes::DWORD); ftpPutFile.arg(ExtTypes::DWORD); ftpPutFile.arg(ExtTypes::STRING); ftpPutFile.arg(ExtTypes::STRING); ftpPutFile.arg(ExtTypes::DWORD); ftpPutFile.arg(ExtTypes::DWORD);

setCurrentDirectory = new DLLFunction(winInetDLL, 'FtpSetCurrentDirectoryA'); setCurrentDirectory.returns(ExtTypes::DWord); setCurrentDirectory.arg(ExtTypes::DWord,ExtTypes::String);

ftpFindFirstFile = new DLLFunction(_winInet, 'FtpFindFirstFileA'); ftpFindFirstFile.returns(ExtTypes::DWORD); ftpFindFirstFile.arg(ExtTypes::DWORD); ftpFindFirstFile.arg(ExtTypes::String); ftpFindFirstFile.arg(ExtTypes::Pointer); ftpFindFirstFile.arg(ExtTypes::DWORD); ftpFindFirstFile.arg(ExtTypes::DWORD);

internetFindNextFile = new DLLFunction(_winInet, 'InternetFindNextFileA'); internetFindNextFile.returns(ExtTypes::DWord); internetFindNextFile.arg(ExtTypes::DWord); internetFindNextFile.arg(ExtTypes::Pointer);

ftpDeleteFile = new DLLFunction(_winInet, 'FtpDeleteFileA'); ftpDeleteFile.returns(ExtTypes::DWord); ftpDeleteFile.arg(ExtTypes::DWord); ftpDeleteFile.arg(ExtTypes::String);

ftpRenameFile = new DLLFunction(_winInet, 'FtpRenameFileA'); ftpRenameFile.returns(ExtTypes::DWord); ftpRenameFile.arg(ExtTypes::DWord); ftpRenameFile.arg(ExtTypes::String); ftpRenameFile.arg(ExtTypes::String);

and the following methods:

int internetConnect(str 60 _server, str 99 _userName, str 99 _password ) {   return internetConnect.call(_handle,_server,0,_userName,_password,1,0,0); }

int FtpGetFile(int _hdl, str 255 _remoteFile, str 255 _localFile) {   return ftpGetFile.call(_hdl,_remoteFile,_localFile,false,0,#FTP_TRANSFER_TYPE_BINARY,0); }

int FtpPutFile(int _hdl, str 255 _localFile, str 255 _remoteFile) {   return ftpPutFile.call(_hdl,_localFile,_remoteFile,#FTP_TRANSFER_TYPE_BINARY,0); }

boolean FtpSetCurrentDirectory(int _hdl, str _name) {    return setCurrentDirectory.call(_hdl, _name) != 0; }

container FtpFindFirstFile(int _hdl, str 255 _fileMask, int flags = 0, int context = 0) {   Binary      data = new Binary(0x200); ;

return [ftpFindFirstFile.call(_hdl, _fileMask, data, flags, context), data.string(44)]; }

str FtpFindNextFile(int _hdl) {   Binary      data = new Binary(0x200); ;

if (internetFindNextFile.call(_hdl, data)) return data.string(44);

return ""; }

boolean FtpDeleteFile(int _hdl, str _filename) {   return ftpDeleteFile.call(_hdl, _filename); }

boolean FtpRenameFile(int _hdl, str _existingFilename, str _newFilename) {   return ftpRenameFile.call(_hdl, _existingFilename, _newFilename); } use the internetconnect method to connect ftp server, ftpgetfile, ftpputfile to get and put files, and ftpSetCurrentDirectory for change current diectory

Upload File
static void TestUploadFTP(Args _args) {   int handle; WinInet inet = new WinInet; ;

handle = inet.internetConnect("ftp.company.com","user123","password123"); inet.FtpSetCurrentDirectory(handle,"docs"); inet.FtpPutFile(handle,"C:/Dokumente und Einstellungen/User123/Eigene Dateien/test.txt","test.txt"); inet.internetCloseHandle(handle); }

Download File
static void TestUploadFTP(Args _args) {   int handle; WinInet inet = new WinInet; ;

handle = inet.internetConnect("ftp.microsoft.com","",""); inet.FtpSetCurrentDirectory(handle,"MISC"); inet.FtpGetFile(handle,"CBCP.TXT","C:/Users/markus/CBCP.TXT"); inet.internetCloseHandle(handle); }

List, delete or rename/move files
static void ListFilesFTP(Args _args) {   int             handle; WinInet        inet = new WinInet; fileNameOpen   _fileName; int            _fileHandle; ;

handle = inet.internetConnect("ftp.microsoft.com", "", ""); inet.FtpSetCurrentDirectory(handle, "MISC");

[_fileHandle,_fileName] = WinInet.FtpFindFirstFile(handle, "*.txt");

while(_fileName) {       info(_filename);

//if you want to delete the file WinInet.FtpDeleteFile(handle, _filename);

//if you want to move the file to a subfolder WinInet.FtpRenameFile(handle, _filename, "OLD/" + _filename);

_fileName = WinInet.FtpFindNextFile(_fileHandle); } }

Troubleshooting
Problem: Connection can be established and directory change works. Download from FTP Server doesnt work, Dynamics AX ist not responding for a while. Afterwards FtpGetFile was not successful.

Solution: Create an exception for program Ax32.exe in your Windows Firewall