Question

I'm currently writing a wrapper library for windows mobile in C/C++. I have to implement and export the following functions:

void start_scanning();
int wait_for_scanning_result();
void stop_scanning();

start_scanning() is called to start the scanning process. wait_for_scanning_result() will wait until a result is available and return it, and stop_scanning will abort the process.

The library I am using has a callback function that is executed when a result is available.

void on_scanning_result(int result)
{
   /* My code goes here */
}

Unfortunately I have to implement the functions above, so my plan was to solve it like this:

void on_scanning_result(int result)
{
   scan_result_available = 1;
   scan_result = result;
}

int wait_for_scanning_result()
{
   /* ... wait until scan_result_available == 1 */
   return scan_result;
}

I have no idea how to do this in windows/C and I would be very glad if someone could help me or tell me which functions I have to use to accomplish this.

Was it helpful?

Solution

You can use windows Synchronization Functions.

Basically all you have to do is:
* CreateEvent - create an event
* WaitForSingleObject - wait for this event to become signaled
* SetEvent - signal the event

OTHER TIPS

Something like this, I expect:

//declare as volatile to inform C that another thread
//may change the value
volatile int scan_result_available;

int wait_for_scanning_result()
{
    while(scan_result_available == 0) {
        //do nothing
    }
    return scan_result;
}

You should find out whether the callback runs in another thread, or runs asynchronously in the same thread, or whether the library needs some other means to allow the callback to run.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top