1
ps3000aGetStreamingLatestValues
(
int16_t handle,
ps3000aStreamingReady lpPs3000AReady,
void * pParameter
)
  1. lpPs3000AReady, a pointer to your ps3000aStreamingReady callback function

  2. pParameter, a void pointer that will be passed to the ps3000aStreamingReady callback. The callback may optionally use this pointer to return information to the application.

And here is the ps3000aStreamingReady callback function:

typedef void (CALLBACK *ps3000aStreamingReady)
(
int16_t handle,
int32_t noOfSamples,
uint32_t startIndex,
int16_t overflow,
uint32_t triggerAt,
int16_t triggered,
int16_t autoStop,
void * pParameter
)

My question is how I can declare pParameter in my main function? pParameter use as a communicator between those two functions and it's a void pointer because it might have to pass any kinds of data.Somehow I have to allocate memory before pass this void pointer as a arguments on those function. But how can do that?

7
  • What do you need the void* parameter to contain? Commented Dec 11, 2020 at 15:26
  • * pParameter, a void pointer passed from ps3000aGetStreamingLatestValues.ps3000aStreamingReady callback function can write to this location to send any data, such as a status flag, back to the application. Commented Dec 11, 2020 at 15:30
  • void* userData is the C-way to capture extra data, C++ way with std::function allow capturing lambda and even custom functor. Commented Dec 11, 2020 at 15:39
  • You can pass a pointer to (almost) anything as a parameter that is declared as void*. And you can add an explicit cast (in the function call) if you really want to. Commented Dec 11, 2020 at 15:40
  • @AnikIslam which parameters do you need to get from pParameter so that you can call lpPs3000AReady? Commented Dec 11, 2020 at 16:11

1 Answer 1

2

Traditional C-way for callback to allow extra parameter is to have extra void* that user provides.

using callback = bool (int, int, void* userData);

struct CallBack
{
    bool operator ()(int a, int b) { /*..*/ }

    // extra members ...
};

bool run_callback(int a, int b, void* userData) {
    CallBack* c = reinterpret_cast<CallBack*>(userData);

    return (*c)(a, b);
}

with possible usage:

CallBack t(true); // Care to lifetime, should still be there when `run_callback` is called
Register(&run_callback, &t); // Would call later run_callback(2, 3, &t);

If you don't need that extra parameter, you might pass your regular function and nullptr

bool run_callback(int a, int b, void*) {
    // ...
}

and

Register(&run_callback, nullptr); // Would call later run_callback(2, 3, nullptr);

For C++ way of type erasure, you might use std::function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.