Archived Forum Post

Index of archived forum posts

Question:

Event Callbacks in the Chilkat C++ API

May 14 '13 at 11:44

How do I get event callbacks in the Chilkat C++ API?


Answer

Many Chilkat C++ classes have methods with event callback capabilities. Event callbacks are implemented in an application by creating a C++ class that derives from a Chilkat "progress" class such as CkFtpProgress, and overrides one or more of the virtual event callback methods. The event callback object is provided to the Chilkat object via the put_EventCallbackObject method.

For example:

// To monitor SFTP progress, derive a class from CkSFtpProgress and
// provide implementations for AbortCheck and PercentDone:
class MySFtpProgress : public CkSFtpProgress
{ public: MySFtpProgress() { } virtual ~MySFtpProgress() { }

// Called periodically during any SFTP method that communicates with
// the server.  The HeartbeatMs property controls the frequency
// of callbacks.  The default HeartbeatMs value = 0, which disables
// AbortCheck callbacks.
void AbortCheck(bool *abort) 
    {

    // To abort any operation while in progress, set the "abort" argument
    // equal to true, like this:
    //*abort = true;
    }

// The PercentDone callbacks is called for any method where it it is possible
// to monitor a percentage completion, such as uploading and downloading files.
void PercentDone(int pctDone, bool *abort)
    {
    printf("SFTP percent done: %d\n", pctDone);

    // To abort an operation while in progress, set the "abort" argument
    // equal to true, like this:
    //*abort = true;
    }

void UploadRate(unsigned long byteCount, unsigned long bytesPerSec) 
    { 
    printf("UploadRate: byteCount = %d, bytesPerSec = %d\n",
    (int)byteCount,(int)bytesPerSec);
    }

void DownloadRate(unsigned long byteCount, unsigned long bytesPerSec) 
    { 
    printf("DownloadRate: byteCount = %d, bytesPerSec = %d\n",
    (int)byteCount,(int)bytesPerSec);
    }

void ProgressInfo(const char *name, const char *value)
    {
    printf("%s, %s\n",name,value);
    }

};

The application's event callback object is instantiated and used like this:

    CkSFtp sftp;

MySFtpProgress myProgress;
sftp.put_EventCallbackObject(&myProgress);