Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
4k views
in .NET FTP by (51.2k points)
A trial-user of edtFTPnet/PRO asked if it's possible to use SecureFTPConnection through a COM interface. In his case, he wanted to use it from ActiveState Perl, but this applies equally to any other COM-capable language or application. This means that you can use FTP, SFTP and FTPS with Delphi, VB6, Microsoft Access, Microsoft Word, Microsoft Excel and many others.

Our response:

Yes, you can use our components from COM, but you'll need to develop a COM wrapper. Don't worry, this is amazingly easy to do. I've included a VS.NET project that exposes SecureFTPConnection as a COM component called "EnterpriseDT.SecureFTPConnection". This implementation is very incomplete and is just intended to demonstrate how it's done, but it should be simple for you to just add the properties and methods that you need.

Below is a snippet of VBA (i.e. MS Access) code that creates an instance of this component and uses it to get a listing of the root directory of the Microsoft FTP server:
Dim ftp As Object
Dim files() As String
        
Set cxn = CreateObject("EnterpriseDT.SecureFTPConnection")
cxn.ServerAddress = "ftp.microsoft.com"
cxn.UserName = "anonymous"
cxn.Password = "anonymous"
cxn.Connect
files = cxn.GetFiles
For Each file In files
    Debug.Print (file)
Next
cxn.Close
Set cxn = Nothing


Below is a listing of the COM wrapper:
using System;
using System.Runtime.InteropServices;

namespace EnterpriseDT
{
   [ClassInterface(ClassInterfaceType.AutoDual)]
   [ProgId("EnterpriseDT.SecureFTPConnection")]
   public class SecureFTPConnection
   {
      private EnterpriseDT.Net.Ftp.SecureFTPConnection cxn = new EnterpriseDT.Net.Ftp.SecureFTPConnection();

      public string ServerAddress
      {
         get { return cxn.ServerAddress; }
         set { cxn.ServerAddress = value; }
      }

      public string UserName
      {
         get { return cxn.UserName; }
         set { cxn.UserName = value; }
      }

      public string Password
      {
         get { return cxn.Password; }
         set { cxn.Password = value; }
      }

      public void Connect()
      {
         cxn.Connect();
      }

      public string[] GetFiles()
      {
         return cxn.GetFiles();
      }

      public void Close()
      {
         cxn.Close();
      }
   }
}

As you can see this is just a pass-through adapter with a couple of COM-related attributes at the beginning. Also, the project has "Register for COM interop" set to True. This setting causes REGASM.EXE to be called after the project has been built. If you are deploying on a machine other than the one you built on then you'll need to make sure that this is called again.

Although the wrapper shown above exposes a fine-grained interface with methods like Connect and GetFiles, in a lot of cases it would be easier to use a coarse-grained approach in which methods do more complex tasks (e.g. connecting, checking if a file is present, uploading if it isn't and then closing). The wrapper wouldn't be as generic, but often that doesn't matter and it saves a lot of time.

If you want to read more about writing COM wrappers for .NET components, have a look at http://www.west-wind.com/presentations/VfpDotNetInterop/DotNetFromVFP.asp.

- Hans (EDT)

Please log in or register to answer this question.

Categories

...