Again a callback question... m(__)m
-
Hi :) If someone knows how to deal with the following problem, please help me:rose: : I need to install a callback between 2 applications and I would like to install that callback through a dll. (please don't tell me to use messages between my two applications: I really need application1 to call and work in a function in application2. Application1 & application2 don't know each other, they just know the dll). How do I have to declare thing in application1, application2 and in the dll:confused: Thanks
-
Hi :) If someone knows how to deal with the following problem, please help me:rose: : I need to install a callback between 2 applications and I would like to install that callback through a dll. (please don't tell me to use messages between my two applications: I really need application1 to call and work in a function in application2. Application1 & application2 don't know each other, they just know the dll). How do I have to declare thing in application1, application2 and in the dll:confused: Thanks
Okay, I have done this sort of thing in VB, so maybe this will help, or maybe I am on the completly wrong track. Application1 knows about a DLL which contains an interface. Application2 has a class that supports the interface. On install, application2 puts something in the registry that says "I am a valid thing for you to talk to. Here is my ProgID" On run, Application1 check the registry, finds all of the "things it can talk to", and uses them, by calling methods, properties etc. of the interface. Is that what you are looking for or is it completly wrong? -- David Wengier
-
Okay, I have done this sort of thing in VB, so maybe this will help, or maybe I am on the completly wrong track. Application1 knows about a DLL which contains an interface. Application2 has a class that supports the interface. On install, application2 puts something in the registry that says "I am a valid thing for you to talk to. Here is my ProgID" On run, Application1 check the registry, finds all of the "things it can talk to", and uses them, by calling methods, properties etc. of the interface. Is that what you are looking for or is it completly wrong? -- David Wengier
Thanks, but my problem is much more "how to write the code": I don't need the registry because each application registers itself inside of the dll everytime they are launched. I just want to know how my interfaces have to look like, eg: ********** Application2 ****************** installTheCallback(callbackAddress); ... LResult callback callbackAddress(???) { ... } ********** DLL ************** BOOL declspec(dllexport) __stdcall installTheCallback(???) { ??? } BOOL declspec(dllexport) __stdcall jumpToTheCallbackRoutine() { ??? } ************** Application1 ****************** jumpToTheCallbackRoutine(); Basicaly I don't know what is needed where I put "???"
-
Hi :) If someone knows how to deal with the following problem, please help me:rose: : I need to install a callback between 2 applications and I would like to install that callback through a dll. (please don't tell me to use messages between my two applications: I really need application1 to call and work in a function in application2. Application1 & application2 don't know each other, they just know the dll). How do I have to declare thing in application1, application2 and in the dll:confused: Thanks
Hello Mr Freeze, From the description of your question, it seems you need one of the following options : 1. RPC (Remote Procedure Call) 2. COM (Component Object Model) RPC seems the most appropriate option for you. The COM option will be largely based on RPC also. These two options will have no need for any middle DLL between your two apps. I also need to explain carefully that it is generally NOT POSSIBLE to have one app call a CALLBACK function on another app. This is so even if the callback function resides in a DLL. The reason is that the two apps are in two different address spaces. RPC seems the best choice for you, Mr Freeze. I'll try to search for a sample for you. Best Regards, Bio.
-
Hello Mr Freeze, From the description of your question, it seems you need one of the following options : 1. RPC (Remote Procedure Call) 2. COM (Component Object Model) RPC seems the most appropriate option for you. The COM option will be largely based on RPC also. These two options will have no need for any middle DLL between your two apps. I also need to explain carefully that it is generally NOT POSSIBLE to have one app call a CALLBACK function on another app. This is so even if the callback function resides in a DLL. The reason is that the two apps are in two different address spaces. RPC seems the best choice for you, Mr Freeze. I'll try to search for a sample for you. Best Regards, Bio.
-
Thanks for your reply :) When I install a callback with "glutDisplayFunc(functionAddress)" (it's a function which installs a callback to render a scene in OpenGL), is that function also using RPC??
Hello Mr Freeze, I haven't used OpenGL before. I'm not familiar with it. But I believe that to use it, you would link with OpenGL DLLs. The function you mentioned, glutDisplayFunc(), is probably exported from one of the OpenGL DLLs. Assuming that this is the case, the OpenGL DLLs are in the same address space as your app. No RPC is required in this case. The OpenGL functions (like glutDisplayFunc()) is free to call your functions (callback or otherwise) directly. This same principle is used with several Win32 APIs like EnumWindows() which takes a pointer to an EnumWindowsProc(). This EnumWindowsProc() must reside in your application. The EnumWindows() function is in USER32.DLL which is loaded into the same address space as your app. Let me know if you need further clarifications, Mr Freeze. :-) Best Regards, Bio.
-
Hello Mr Freeze, I haven't used OpenGL before. I'm not familiar with it. But I believe that to use it, you would link with OpenGL DLLs. The function you mentioned, glutDisplayFunc(), is probably exported from one of the OpenGL DLLs. Assuming that this is the case, the OpenGL DLLs are in the same address space as your app. No RPC is required in this case. The OpenGL functions (like glutDisplayFunc()) is free to call your functions (callback or otherwise) directly. This same principle is used with several Win32 APIs like EnumWindows() which takes a pointer to an EnumWindowsProc(). This EnumWindowsProc() must reside in your application. The EnumWindows() function is in USER32.DLL which is loaded into the same address space as your app. Let me know if you need further clarifications, Mr Freeze. :-) Best Regards, Bio.
Thanks again Bio:rose: I am quite new to all this and might be a bit slow understanding... but a last question: The dll linking my two applications has a shared data segment. Does this simplify something? What about just overriding an unused window function in application2 and then, in application1 calling : FromHandle(app2WindowHandle)->the_overrided_function() ?? Best regards
-
Hi :) If someone knows how to deal with the following problem, please help me:rose: : I need to install a callback between 2 applications and I would like to install that callback through a dll. (please don't tell me to use messages between my two applications: I really need application1 to call and work in a function in application2. Application1 & application2 don't know each other, they just know the dll). How do I have to declare thing in application1, application2 and in the dll:confused: Thanks
In one of your posts following this, you said you've declared a shared data segment. Keep it, you will need it for what you're after. The trick is having a function pointer storing the callback in the shared data segment:
// in the DLL header
typedef BOOL (WINAPI * MY_DLL_CALLBACK)(int); // or whatever signature you want to use
// in the DLL .cpp
#pragma data_seg(".MYSEC")
MY_DLL_CALLBACK pMyDLLCallback=0;
#pragma dara_seg()Now you just export functions setting and using this pointer:
extern "C" __declspec(dllexport) void setCallback(MY_DLL_CALLBACK *pCallback)
{
pMyDLLCallback=pCallback;
}extern "C" __declspec(dllexport) BOOL callCallback(int parm1)
{
return pMyDLLCallback(parm1);
}Having the pointer declared in a shared data segment is required so that both apps "see" the same variable (by default, EXEs have their own copy of all the data in DLLs, regardless of whether the DLLs are being used by other apps). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
In one of your posts following this, you said you've declared a shared data segment. Keep it, you will need it for what you're after. The trick is having a function pointer storing the callback in the shared data segment:
// in the DLL header
typedef BOOL (WINAPI * MY_DLL_CALLBACK)(int); // or whatever signature you want to use
// in the DLL .cpp
#pragma data_seg(".MYSEC")
MY_DLL_CALLBACK pMyDLLCallback=0;
#pragma dara_seg()Now you just export functions setting and using this pointer:
extern "C" __declspec(dllexport) void setCallback(MY_DLL_CALLBACK *pCallback)
{
pMyDLLCallback=pCallback;
}extern "C" __declspec(dllexport) BOOL callCallback(int parm1)
{
return pMyDLLCallback(parm1);
}Having the pointer declared in a shared data segment is required so that both apps "see" the same variable (by default, EXEs have their own copy of all the data in DLLs, regardless of whether the DLLs are being used by other apps). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
Hello Joaquin, Your suggestion is very good, Joaquin. Yes, the use of a Shared Data Section seems quite useful and may have great potential for Mr Freeze's problem. I tried using Shared Data Section with the following architecture : 1. App1 links with Shared DLL and calls setCallback(). 1.1 This sets "pMyDLLCallback" to the address of Callback Func in App1. 2. App2 links with Shared DLL and calls callCallback(). 2.1 This makes callCallback() call a function whose address is stored in "pMyDLLCallback". There is a problem in 2.1 because the address in "pMyDLLCallback" points to is in the address space of App2. And App2 does not contain the Callback function in App1. In this case, App2 will either crash or may execute some function which is not the same Callback function as that in App1. It may even dive straight into the middle of some function in itself. Is my analysis correct, Joaquin ? Please let me know as I may have totally misunderstood your solution. Many Thanks, Bio.
-
Thanks again Bio:rose: I am quite new to all this and might be a bit slow understanding... but a last question: The dll linking my two applications has a shared data segment. Does this simplify something? What about just overriding an unused window function in application2 and then, in application1 calling : FromHandle(app2WindowHandle)->the_overrided_function() ?? Best regards
Hello Mr Freeze, You're most welcome, Mr Freeze. Joaquin has supplied a possible solution to your problem with the use of Shared Data Segments (please read his message to you in this thread). I have tried it myself but there are some issues with it (please refer to my response to Joaquin's message). >> What about just overriding an unused window function in application2 >> and then, in application1 calling : >> FromHandle(app2WindowHandle)->the_overrided_function() ?? I'm not 100% sure about this, Mr Freeze, but I doubt if it will accomplish what you are setting out to achieve. Let's see what Joaquin says about my concerns over the Shared Data Section. Regards, Bio.
-
Hello Joaquin, Your suggestion is very good, Joaquin. Yes, the use of a Shared Data Section seems quite useful and may have great potential for Mr Freeze's problem. I tried using Shared Data Section with the following architecture : 1. App1 links with Shared DLL and calls setCallback(). 1.1 This sets "pMyDLLCallback" to the address of Callback Func in App1. 2. App2 links with Shared DLL and calls callCallback(). 2.1 This makes callCallback() call a function whose address is stored in "pMyDLLCallback". There is a problem in 2.1 because the address in "pMyDLLCallback" points to is in the address space of App2. And App2 does not contain the Callback function in App1. In this case, App2 will either crash or may execute some function which is not the same Callback function as that in App1. It may even dive straight into the middle of some function in itself. Is my analysis correct, Joaquin ? Please let me know as I may have totally misunderstood your solution. Many Thanks, Bio.
Hello Lim, I'm afraid your analysis is 100% correct. Conclusion: my approach is useless. Didn't think about the address space issue. As you pointed out, one cannot execute a function belonging in a different module. So, seems like viable solutions involve some kind of interprocess marshalling --hand-made sockets or Windows messages stuff, COM and RPC are the candidate technologies that come to my mind. Thanks for pointing out the flaw in my (so-called) solution. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
Hello Lim, I'm afraid your analysis is 100% correct. Conclusion: my approach is useless. Didn't think about the address space issue. As you pointed out, one cannot execute a function belonging in a different module. So, seems like viable solutions involve some kind of interprocess marshalling --hand-made sockets or Windows messages stuff, COM and RPC are the candidate technologies that come to my mind. Thanks for pointing out the flaw in my (so-called) solution. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
Hello Joaquin, You're most welcome, Joaquin. Thanks very much for clarifying. Many Thansk, Bio.
-
Hello Mr Freeze, You're most welcome, Mr Freeze. Joaquin has supplied a possible solution to your problem with the use of Shared Data Segments (please read his message to you in this thread). I have tried it myself but there are some issues with it (please refer to my response to Joaquin's message). >> What about just overriding an unused window function in application2 >> and then, in application1 calling : >> FromHandle(app2WindowHandle)->the_overrided_function() ?? I'm not 100% sure about this, Mr Freeze, but I doubt if it will accomplish what you are setting out to achieve. Let's see what Joaquin says about my concerns over the Shared Data Section. Regards, Bio.
-
Hello Lim, I'm afraid your analysis is 100% correct. Conclusion: my approach is useless. Didn't think about the address space issue. As you pointed out, one cannot execute a function belonging in a different module. So, seems like viable solutions involve some kind of interprocess marshalling --hand-made sockets or Windows messages stuff, COM and RPC are the candidate technologies that come to my mind. Thanks for pointing out the flaw in my (so-called) solution. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
Hello Bio, Thanks a lot for your clarification and your precious help :) Best regards, Marc
Hello Mr Freeze, You're most welcome, Mr Mreeze. I'm searching for a suitable RPC sample and will email you if I manage to find one. Best Regards, Bio.