Starting in Chilkat v9.5.0.56, the “C” API will support callback function pointers. Here is a sample program, which can be downloaded at http://www.chilkatsoft.com/download/C/main.c
#include <stdio.h> #include <C_CkHttp.h> #include <C_CkGlobal.h> #include <C_CkTask.h> #ifndef TRUE #define TRUE 1 #endif #ifndef FALSE #define FALSE 0 #endif // Callback functions BOOL myAbortCheck() { printf("Abort Check!\n"); // Return TRUE to abort, return FALSE to continue.. return FALSE; } BOOL myPercentDone(int pctDone) { printf("Percent Done: %d\n",pctDone); // Return TRUE to abort, return FALSE to continue.. return FALSE; } void myProgressInfo(const char *name, const char *value) { printf("Progress Info: %s: %s\n",name,value); } void myTaskCompleted(HCkTask hTask) { BOOL taskSuccess; // When a task is running asynchronously, all callbacks are being called in the background thread. // Be careful with accessing data structures or variables shared with the foreground thread. // Your C program may need to use synchronization features, such as critical sections, semaphores, mutexes, etc. // to make your application thread-safe. // This callback is made when the QuickGetStrAsync completes // Determine if it succeeded, and get the HTML that was returned. printf("------------- myTaskCompleted -----------------\n"); taskSuccess = CkTask_getTaskSuccess(hTask); if (taskSuccess) { // Output the HTML that was received. printf("%s\n",CkTask_getResultString(hTask)); } else { const char *errText = CkTask_resultErrorText(hTask); printf("%s\n",errText); } printf("^------------ myTaskCompleted ----------------^\n"); } BOOL unlockGlobal(void) { HCkGlobal global; BOOL success; global = CkGlobal_Create(); // Any string unlocks the all Chilkat objects for the 1st 30-days. success = CkGlobal_UnlockBundle(global,"Anything for 30-day trial"); if (success != TRUE) { printf("%s\n",CkGlobal_lastErrorText(global)); } CkGlobal_Dispose(global); return success; } void httpSample(void) { HCkHttp http; const char *html; http = CkHttp_Create(); // Setup callback functions: CkHttp_setAbortCheck(http,myAbortCheck); CkHttp_setPercentDone(http,myPercentDone); CkHttp_setProgressInfo(http,myProgressInfo); // Send the HTTP GET and return the content in a string. html = CkHttp_quickGetStr(http,"http://www.wikipedia.org/"); printf("---------------------------------------\n"); printf("---------------------------------------\n"); printf("%s\n",html); printf("---------------------------------------\n"); printf("---------------------------------------\n"); CkHttp_Dispose(http); } void httpAsyncSample(void) { HCkHttp http; BOOL success; HCkTask task; http = CkHttp_Create(); // Setup callback functions: CkHttp_setAbortCheck(http,myAbortCheck); CkHttp_setPercentDone(http,myPercentDone); CkHttp_setProgressInfo(http,myProgressInfo); CkHttp_setTaskCompleted(http,myTaskCompleted); // Send the HTTP GET and return the content in a string. task = CkHttp_QuickGetStrAsync(http,"http://www.wikipedia.org/"); if (task == NULL) { printf("%s\n",CkHttp_lastErrorText(http)); CkHttp_Dispose(http); return; } // Schedule the task for running on the thread pool. This changes the task's state // from Inert to Live. success = CkTask_Run(task); if (success != TRUE) { printf("%s\n",CkTask_lastErrorText(task)); CkTask_Dispose(task); CkHttp_Dispose(http); return; } // The application is now free to do anything else // while the email is being sent. // For this example, we'll simply sleep and periodically // check to see if the SendEmail if finished. While checking // however, we'll report on the progress. while (CkTask_getFinished(task) != TRUE) { // Sleep 100 ms. CkTask_SleepMs(task,100); } // A finished task could be one that was canceled, aborted, or truly finished. // If the task was "canceled", it was canceled prior to actually starting. This could // happen if the task was canceled while waiting in a thread pool queue to be scheduled by Chilkat's // background thread pool scheduler. // If the task was "aborted", it indicates that it was canceled while running in a background thread. // The ResultErrorText will likely indicate that the task was aborted. // If the task "completed", then it ran to completion, but the actual success/failure of the method // is determined by the result obtained via a GetResult* method. (A "completed" task will // have a StatusInt equal to 7. If the task finished, but was not completed, then it must've // been aborted or canceled: if (CkTask_getStatusInt(task) != 7) { printf("Task did not complete.\n"); printf("task status: %s\n",CkTask_status(task)); CkTask_Dispose(task); CkTask_Dispose(task); CkHttp_Dispose(http); return; } CkTask_Dispose(task); CkHttp_Dispose(http); return; } int main() { freopen("stdout.txt","w",stdout); if (unlockGlobal() == FALSE) return 0; httpSample(); // The same thing, but using the async version of the method w/ a task object.. httpAsyncSample(); return 0; }