Chilkat Event Callbacks in wchar_t (Unicode) 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 progressAbortCheck
— to allow canceling the operationProgressInfo
— 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 (wchar_t classes)
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 |
---|---|
CkZipW | CkZipProgressW |
CkFtp2W | CkFtp2ProgressW |
CkHttpW | CkHttpProgressW |
CkSFtpW | CkSFtpProgressW |
CkMailManW | CkMailManProgressW |
CkTarW | CkTarProgressW |
All other Chilkat classes | CkBaseProgressW |
1. Create a Class that Inherits from CkSFtpProgressW
#include <CkSFtpW.h>
#include <CkGlobalW.h>
#include <CkSFtpProgressW.h>
#include <iostream>
class MyProgressW : public CkSFtpProgressW {
public:
bool Abort;
MyProgressW() : Abort(false) {}
// Called periodically with % done
// Set abort = true to abort. abort should be non-null, but always check for safety..
void PercentDone(int pctDone, bool *abort) override {
std::cout << "Progress: " << pctDone << "%\n";
if (abort) *abort = Abort;
return;
}
// Called periodically to allow user to cancel
// Set abort = true to abort. abort should be non-null, but always check for safety..
void AbortCheck(bool *abort) override {
if (abort) *abort = Abort;
return;
}
// Called with other status messages
void ProgressInfo(const wchar_t *name, const wchar_t *value) override {
std::wcout << "[ProgressInfo] " << name << ": " << value << "\n";
return;
}
};
2. Main Function to Connect and Download
int main() {
CkGlobalW glob;
if (!glob.UnlockBundle(L"YOUR_CHILKAT_UNLOCK_CODE")) {
std::wcerr << "Unlock failed: " << glob.lastErrorText() << "\n";
return 1;
}
CkSFtpW sftp;
bool success = sftp.Connect(L"sftp.example.com", 22);
if (!success) {
std::wcerr << sftp.lastErrorText() << "\n";
return 1;
}
success = sftp.AuthenticatePw(L"username", L"password");
if (!success) {
std::wcerr << sftp.lastErrorText() << "\n";
return 1;
}
success = sftp.InitializeSftp();
if (!success) {
std::wcerr << sftp.lastErrorText() << "\n";
return 1;
}
MyProgressW progress;
sftp.put_EventCallbackObject(&progress);
// AbortCheck callbacks are called every 250 milliseconds.
// (A PercentDone callback counts as an AbortCheck.)
sftp.put_HeartbeatMs(250);
const wchar_t *remoteFile = L"remote/path/file.txt";
const wchar_t *localFile = L"downloaded.txt";
success = sftp.DownloadFileByName(remoteFile, localFile);
if (!success) {
std::wcerr << sftp.lastErrorText() << "\n";
return 1;
}
std::wcout << "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.