Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
DLL theory,
I want to call function of calling application from called dll.
what i mean is that if i have application of function XYZ() and ABC()
in function ABC() have called of dll function DLLCALL()
now from DLLCALL() I want to call XYZ().

ALL SUGGESTIONS ARE WELCOME.
Posted
Updated 23-Mar-11 1:52am
v2

Your question is not very clear.

If I understood well, you have a function in your main application and you want to call that function from a dll. If I didn't understand then rephrase your question.

If I am correct, the function from your main application is called a callback. You will give to the dll a pointer to that function, and the dll will use this pointer to make the call.

For example, in your dll:

C++
//declare the callback prototype
//it must match the function prototype in your main application
typedef void (*YOUR_CALLBACK)(int someParameter);

//this will store the pointer
//I made it a global variable just to show how it works
//DON'T DO THAT in your dll!
YOUR_CALLBACK _theCallback = NULL;

//this function will store the callback pointer
//you must export it to call it from the main application
__declspec(dllexport) void SetCallback(YOUR_CALLBACK callback)
{
    //store this pointer
    _theCallback = callback;
}

//call the callback somewhere in your dll
void function()
{
   ...
   //call the callback with a parameter
   if (_theCallback != NULL)
       _theCallback(10);
}


Now in your main application:
C++
//this is the function you want to call from the dll
//its prototype must match YOUR_CALLBACK
void YourCallback(int someParameters)
{
    ....
}

//give the function pointer during your application initialization or anywhere else
void Init()
{
   ...
   //this function is imported from the dll
   //just give 
   SetCallback(YourCallback);
   ...
}
 
Share this answer
 
Comments
CPallini 23-Mar-11 9:30am    
5.
Olivier Levrey 23-Mar-11 9:36am    
Thanks
Sergey Alexandrovich Kryukov 23-Mar-11 17:47pm    
Agree, a 5.
--SA
[no name] 24-Mar-11 1:08am    
Please can you explain what is YourCallback in SetCallback(YourCallback); in main application.
Olivier Levrey 24-Mar-11 4:55am    
It is the function which will be called from the dll. It is your XYZ function.
In application export the function:
int __declspec(dllexport) XYZ(int parameter)
{
   printf("param: %d\n", parameter);
   return parameter;
}


In DLL define a pointer for the callback function. In the initial section (ie: dllmain) fill it with the application's function address, then You can call the function.
int (*XYZ)(int parameter);

// init
HANDLE app;
app = GetModuleHandle(NULL);
XYZ = (void*) GetProcAddress(app, "XYZ");

// call
i = ZYX(123);
printf("return: %d\n", i);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900