# Calling External Functions from within a class.

This topic is 4256 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello All I have a dll which handles all my networking and a min app. What I am trying to do is create my own cut down version of DirectPlay to work on Win32 and Linux. And I am trying to get my networking dll to call a function in my main app. Like what Microsoft used in DirectPlay and their other libraries to pass information:
HRESULT WINAPI MessageHandler(PVOID pvUserContext,
DWORD dwMessageId, PVOID pMsgBuffer);
Does anyone know how to do these type of callbacks? I have tried creating threads, external treads with no joy. Thanks in advance. Paul Kirby

##### Share on other sites
Quote:
 Original post by TheMightyDudeHello AllI have a dll which handles all my networking and a min app.What I am trying to do is create my own cut down version of DirectPlay to work on Win32 and Linux.And I am trying to get my networking dll to call a function in my main app.Like what Microsoft used in DirectPlay and their other libraries to pass information:HRESULT WINAPI MessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer);Does anyone know how to do these type of callbacks?I have tried creating threads, external treads with no joy.

You seem to be interested in RPC (Remote Procedure Calls)
RPC support is integrated in OpenTNL/The Torque Network Library.

##### Share on other sites
Quote:
 Original post by Anonymous PosterYou seem to be interested in RPC (Remote Procedure Calls)RPC support is integrated in OpenTNL/The Torque Network Library.

Thanks for the reply, but I want to do this in my own library.

##### Share on other sites
You'll need to have a function in your dll that you can register a function for it to call. If you have messed with threads then you've already done something like that when passing the function pointer for the threads entry point.

Example:
// in your main app.void TheCallback(void){// do something}void main (){RegisterCallBack(&TheCallBack);}//in your dllvoid RegisterCallBack(void(CallBackFunc*)(void)){StoredFunc = CallBackFunc;}void RunCallBack(){StoredFunc();}

Hope this is enough to get you started.

##### Share on other sites
Quote:
 Original post by dormlockYou'll need to have a function in your dll that you can register a function for it to call. If you have messed with threads then you've already done something like that when passing the function pointer for the threads entry point.Example:// in your main app.void TheCallback(void){// do something}void main (){RegisterCallBack(&TheCallBack);}//in your dllvoid RegisterCallBack(void(CallBackFunc*)(void)){StoredFunc = CallBackFunc;}void RunCallBack(){StoredFunc();}Hope this is enough to get you started.

Had to reverse the * as shown below:
void RegisterCallBack(void(*CallBackFunc)(void))
{
StoredFunc = CallBackFunc;
}

also had to define StoredFunc as void (*StoredFunc)(void);
Plus a few other minor changes, due to using classes, but apart from that it seems to work fine :)

Thanks

btw, is this the only way to do this?
Like not having to have RegisterCallBack(&TheCallBack); ?

Because i dont see them doing this in Direct play?

Paul Kirby

##### Share on other sites
There are other ways you could do it that would probably be more automated. This was just the basics. You can try googling something like function callbacks c++ and go from there.

As far as moving the * I was typing from memory.

##### Share on other sites
Oops that was me.

##### Share on other sites
Quote:
 Original post by dormlockThere are other ways you could do it that would probably be more automated. This was just the basics. You can try googling something like function callbacks c++ and go from there.As far as moving the * I was typing from memory.

Heh, I was using something like that.

in dll:
int (*callback)(int index, int item, void *param);

in main app:
In WinMaincallback = TestLoop;int TestLoop(int val1, int val2, void  *data){    char tmpbuf[255] = {0};    sprintf(tmpbuf,"val1: %d, val2: %d, and data: %s\n",val1,val2,data);    MessageBox(0,tmpbuf,"Info",0);    return 1234;}

Then to call back function in main app I was using the following in the dll:
callback(11,7,(void*)"Test CALL BACK");

##### Share on other sites
Oh btw, I have just noticed that they do it like the way you said...

They use the following:
pDPServer->Initialize(NULL,MessageHandler,0);

LOL, init funny that there is always a line that you miss :P

I will most prob do it this way :)

Thanks for you help :)

Paul Kirby