c++ callback question (function pointer)

Hi, I need a little help here.

I am working on a video project and I have a dll that receives video data coming in from the network.

I want to do the following.

1. Create a static global function in an existing class that will be used as a callback function.
2. Pass a pointer to this function to the dll that receives the video.
3. Whenever the dll receives video data, I want to call by global function defined in step 1 above.

I have access to the dll code so I can modify the function signature that creates it so it can receive a function pointer to my global function.


Can someone show me an example of how this is done??


You mean something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void function(){
    printf("This is the callback function.\n");
}

void DLLFunc(void(*callbackfunc)()){
    // Check if recieves Video then
    callbackfunc();
}

int main(){
    DLLFunc(function);
    return 0;
}
Yes I think this is what I want - I'm new to function pointers and callbacks :(

My function has to return two int values.

so can you show me how to simply modify it so that it passes back e.g.

1
2
3
4
5
6
void DLLFunc(void(*callbackfunc)()){
    // Check if recieves Video then
    int id = 1;
    int length = 100;
    callbackfunc(id, length);
}



Thanks very much





I have a nice tuto of function pointers but it's in spanish, you have to do it like this:
1
2
3
4
5
6
void DLLFunc(void(*callbackfunc)(int,int)){
    // Check if recieves Video then
    int id = 1;
    int length = 100;
    callbackfunc(id, length);
}
Thanks very much this is good enough for me.

gracias :)
I think it's good form to typedef your function pointer.

It is also good practice to specify the calling convention, which is usually __stdcall for callbacks (windows.h defines CALLBACK accordingly).

1
2
3
4
5
6
7
8
typedef void(CALLBACK * PFNMYCALLBACK)(int,int);

void DLLFunc(PFNMYCALLBACK callbackfunc){
    // Check if recieves Video then
    int id = 1;
    int length = 100;
    callbackfunc(id, length);
}
Topic archived. No new replies allowed.