Chilkat Event Callbacks in C++

Here’s a complete C++ example using the Chilkat C++ library to perform an SFTP download with support for the following event callbacks:

  • PercentDone — to track download progress
  • AbortCheck — to allow canceling the operation
  • ProgressInfo — to monitor additional info such as transfer rate

While this example focuses on callbacks for an SFTP download, the same method applies to other Chilkat C++ classes.


Chilkat Progress Class Inheritance

When creating a custom progress class in C++, you should inherit from the appropriate Chilkat base progress class depending on the Chilkat class you're working with. This allows your class to receive event callbacks specific to that Chilkat class.

Chilkat Class Inherit From
CkZip CkZipProgress
CkFtp2 CkFtp2Progress
CkHttp CkHttpProgress
CkSFtp CkSFtpProgress
CkMailMan CkMailManProgress
CkTar CkTarProgress
All other Chilkat classes CkBaseProgress

1. Create a Class that Inherits from CkSFtpProgress


#include <CkSFtp.h>
#include <CkGlobal.h>
#include <CkSFtpProgress.h>
#include <iostream>

class MyProgress : public CkSFtpProgress {
public:
    bool Abort;

    MyProgress() : Abort(false) {} 

    // Called periodically with % done
    // Return true to abort.
    bool PercentDone(int pctDone) override {
        std::cout << "Progress: " << pctDone << "%\n";
        return Abort;
    }

    // Called periodically to allow user to cancel
    // Return true to abort.
    bool AbortCheck(void) override {
        return Abort;
    }

    // Called with other status messages
    void ProgressInfo(const char *name, const char *value) override {
        std::cout << "[ProgressInfo] " << name << ": " << value << "\n";
        return;
    }
};

2. Main Function to Connect and Download

int main() {
    CkGlobal glob;

    if (!glob.UnlockBundle("YOUR_CHILKAT_UNLOCK_CODE")) {
        std::cerr << "Unlock failed: " << glob.lastErrorText() << "\n";
        return 1;
    }

    CkSFtp sftp;

    bool success = sftp.Connect("sftp.example.com", 22);
    if (!success) {
        std::cerr << sftp.lastErrorText() << "\n";
        return 1;
    }

    success = sftp.AuthenticatePw("username", "password");
    if (!success) {
        std::cerr << sftp.lastErrorText() << "\n";
        return 1;
    }

    success = sftp.InitializeSftp();
    if (!success) {
        std::cerr << sftp.lastErrorText() << "\n";
        return 1;
    }

    MyProgress progress;
    sftp.put_EventCallbackObject(&progress);

	// AbortCheck callbacks are called every 250 milliseconds.
	// (A PercentDone callback counts as an AbortCheck.)
	sftp.put_HeartbeatMs(250);
	
    const char *remoteFile = "remote/path/file.txt";
    const char *localFile = "downloaded.txt";

    success = sftp.DownloadFileByName(remoteFile, localFile);
    if (!success) {
        std::cerr << sftp.lastErrorText() << "\n";
        return 1;
    }

    std::cout << "Download completed successfully.\n";

    return 0;
}

Notes

  • Replace YOUR_CHILKAT_UNLOCK_CODE with your actual license key or trial code.
  • This example uses password authentication; modify if using public key authentication.
  • PercentDone is only called if the SFTP library can determine total size (for large files).
  • You can trigger cancellation by setting progress.Abort = true; from another thread.