Created On:  07 July 2011
 
It is possible to use WinInet.dll through SilkTest to communicate with an ftp server. You will need to call the dll in a test script, using something similar to the code below:

string gcsUser = "anonymous"

string gcsPassword = "anyone@noplace.com"

type HINTERNET is LONG

type DWORD is UNSIGNED LONG

type BOOL is INT

type LPCSTR is STRING

type LPSTR is STRING

 

dll "Wininet.dll"

    HINTERNET MyInternetOpen (LPCSTR lpszAgent, DWORD dwAccessType, LPCSTR lpszProxyName, LPCSTR lpszProxyBypass, DWORD dwFlags) alias "InternetOpenW"

                 

    INT MyInternetCloseHandle (HINTERNET hInternetSession) alias "InternetCloseHandle"

                 

    HINTERNET MyInternetConnect (HINTERNET hInternet, LPCSTR lpszServerName, DWORD nServerPort, LPCSTR lpszUserName, LPCSTR lpszPassword, DWORD dwService, DWORD dwFlags, DWORD dwContext) alias "InternetConnectW"

                 

    BOOL MyFtpGetCurrentDirectory (HINTERNET hConnect, LPSTR lpszCurrentDirectory, DWORD lpdwCurrentDirectory) alias "FtpGetCurrentDirectoryW"

                 

    BOOL MyFtpSetCurrentDirectory (HINTERNET hConnect, LPCSTR lpszDirectory) alias "FtpSetCurrentDirectoryW"

                 

    BOOL MyFtpGetFile (HINTERNET hConnect, LPCSTR lpszRemoteFile, LPCSTR lpszNewFile, BOOL fFailIfExists, DWORD dwFlagsAndAttributes, DWORD dwFlags, DWORD dwContext) alias "FtpGetFileW"

                 

    BOOL MyFtpPutFile (HINTERNET hConnect, LPCSTR lpszLocalFile, LPCSTR lpszNewRemoteFile, DWORD dwFlags, DWORD dwContext) alias "FtpPutFileW"

 

This declares everything in the dll that is needed to connect to the ftp server. Once you have declared this you will have to insert the following code – altering it to reflect the relevant ftp url, username and password.

 

type ActionType is enum 

     Get

     Send

                 

 ConnectToFTP()

                 

      BOOLEAN bResult = FALSE

                 

      LPCSTR lpszAgent = "partner.exe"           // name of the program calling wininet api's.

                 

      DWORD INTERNET_FLAG_PASSIVE  = 1

      DWORD INTERNET_OPEN_TYPE_DIRECT = 1        // these next values are from flags in wininet.h

      DWORD INTERNET_SERVICE_FTP = 1

      DWORD INTERNET_DEFAULT_FTP_PORT = 21

      DWORD FILE_ATTRIBUTE_NORMAL = 0x00000080

      DWORD INTERNET_FLAG_DONT_CACHE = 0x04000000  // set the caching flags

      DWORD FTP_TRANSFER_TYPE_ASCII = 0x00000001   // all of the transfers in here are ASCII ...

      DWORD INTERNET_FLAG_ASYNC = 1

      DWORD FTP_TRANSFER_TYPE_BINARY = 1

                 

      HINTERNET hOpenTgtSession = NULL            // variable initialization

      HINTERNET hOpenTgtSession2 = NULL

      LPCSTR szDir = Space ( 1024 )               // this will hold the working directory on the target

      DWORD dwLength = 1024                       // essentially a buffer size ...

      INT fFailIfExists = [INT] FALSE

                 

      LPCSTR lpszServerName = "10.10.10.10."      // host to connect to

      LPCSTR lpszUsername = "username"            // global user name

      LPCSTR lpszPassword = "password"            // global password

                 

      // Open a session with partner.exe as the "agent"

      hOpenTgtSession = MyInternetOpen ( "TestFTP", INTERNET_OPEN_TYPE_DIRECT,"", "",INTERNET_FLAG_ASYNC )

      print(hOpenTgtSession)

      // Make an FTP connection

      hOpenTgtSession2 = MyInternetConnect ( hOpenTgtSession, lpszServerName, INTERNET_DEFAULT_FTP_PORT, lpszUsername, lpszPassword, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE , 0 )

      print(hOpenTgtSession2)

      // if we don't get a directory, the connection failed for some reason

      if ( ! MyFtpGetCurrentDirectory ( hOpenTgtSession2, szDir, [out] dwLength ) )  

            LogError ( "Could not connect to the server ")

            Print ( "Check Servername, Username, Password" )

                                 

      print(MyFtpGetCurrentDirectory ( hOpenTgtSession2, szDir, [out] dwLength ))

      MyFtpGetFile (hOpenTgtSession2, "/Outgoing/Mick/VJ.stp","c:\VJ2.stp",0, 0, FTP_TRANSFER_TYPE_BINARY, 0)//, BOOL fFailIfExists, DWORD dwFlagsAndAttributes, DWORD dwFlags, DWORD dwContext) alias "FtpGetFileA"

               


In this example we have included some print commands to show what handles are being returned by the function. These can be removed. Once this code is in place, you then need to call the ConnectToFTP() function in a testcase, like so:

 

testcase ftpTest() appstate none

                 

                ConnectToFTP()

               

 


Then run the testcase and it will connect to the ftp server.

Note: this dll does not support connecting to a secure FTP (SFTP) site so it cannot be used for this purpose.
Incident #2526417