Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Again a callback question... m(__)m

Again a callback question... m(__)m

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiontutorial
15 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mr Freeze
    wrote on last edited by
    #1

    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

    D L J 3 Replies Last reply
    0
    • M Mr Freeze

      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

      D Offline
      D Offline
      David Wengier
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • D 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

        M Offline
        M Offline
        Mr Freeze
        wrote on last edited by
        #3

        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 "???"

        1 Reply Last reply
        0
        • M Mr Freeze

          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

          L Offline
          L Offline
          Lim Bio Liong
          wrote on last edited by
          #4

          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.

          M 1 Reply Last reply
          0
          • L Lim Bio Liong

            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.

            M Offline
            M Offline
            Mr Freeze
            wrote on last edited by
            #5

            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??

            L 1 Reply Last reply
            0
            • M Mr Freeze

              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??

              L Offline
              L Offline
              Lim Bio Liong
              wrote on last edited by
              #6

              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.

              M 1 Reply Last reply
              0
              • L Lim Bio Liong

                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.

                M Offline
                M Offline
                Mr Freeze
                wrote on last edited by
                #7

                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

                L 1 Reply Last reply
                0
                • M Mr Freeze

                  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

                  J Offline
                  J Offline
                  Joaquin M Lopez Munoz
                  wrote on last edited by
                  #8

                  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

                  L 1 Reply Last reply
                  0
                  • J Joaquin M Lopez Munoz

                    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

                    L Offline
                    L Offline
                    Lim Bio Liong
                    wrote on last edited by
                    #9

                    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.

                    J 1 Reply Last reply
                    0
                    • M Mr Freeze

                      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

                      L Offline
                      L Offline
                      Lim Bio Liong
                      wrote on last edited by
                      #10

                      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.

                      M 1 Reply Last reply
                      0
                      • L Lim Bio Liong

                        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.

                        J Offline
                        J Offline
                        Joaquin M Lopez Munoz
                        wrote on last edited by
                        #11

                        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

                        L M 2 Replies Last reply
                        0
                        • J Joaquin M Lopez Munoz

                          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

                          L Offline
                          L Offline
                          Lim Bio Liong
                          wrote on last edited by
                          #12

                          Hello Joaquin, You're most welcome, Joaquin. Thanks very much for clarifying. Many Thansk, Bio.

                          1 Reply Last reply
                          0
                          • L Lim Bio Liong

                            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.

                            M Offline
                            M Offline
                            Mr Freeze
                            wrote on last edited by
                            #13

                            Hello Bio, Thanks a lot for your clarification and your precious help :) Best regards, Marc

                            L 1 Reply Last reply
                            0
                            • J Joaquin M Lopez Munoz

                              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

                              M Offline
                              M Offline
                              Mr Freeze
                              wrote on last edited by
                              #14

                              Hello Joaquin, Thanks for your reply to my problem which gave me a small hope to solve the problem very quickly and easily. It seems that I'll have to take to more difficult route anyway (RPC,...) ;) Best regards, Marc

                              1 Reply Last reply
                              0
                              • M Mr Freeze

                                Hello Bio, Thanks a lot for your clarification and your precious help :) Best regards, Marc

                                L Offline
                                L Offline
                                Lim Bio Liong
                                wrote on last edited by
                                #15

                                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.

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups