Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ callback question

Options
  • 30-11-2005 7:03pm
    #1
    Registered Users Posts: 250 ✭✭


    Can somebody please give me some advice and a link to a good tutorial?

    I am not an experienced C++ developer, but need to write some code that would enable a function callback. I am not entirely sure if this is what I need - but essentially, i need to execute a function call in dll "2" from dll "1" if a certain condition is met. i understand that it may involve passing the address of the function i want execute in "2" to "1" so "1" can call it.

    I googled a bit but there seems to be a number of ways to do this so i am not sure which is the best way. Also, a number of examples includes tutorials on doing it from VB... but i need to do it between two apps (dll's) written in C.

    Any advice would be greatly appreaciated.

    TIA


Comments

  • Registered Users Posts: 23 RazorEdge


    Google "function pointer callback"...first link will tell you everything you need to know

    http://www.newty.de/fpt/index.html


  • Registered Users Posts: 250 ✭✭ikoonman


    Thanks for that, but I have learned that call backs between dll's and exe's are a bit more complicated than that.

    I may have to go with shared memory access.


  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    Is it not (just) a matter of DLL 2 exporting a function and DLL 1 code importing it?
    Every Windows function you use is in a DLL somewhere. You access them by including a header file and linking with the library file.

    The setup might be something like:
    // dll1.h
    __declspec(dllexport) int DllOneFunction();
    
    // dll2.cpp
    __declspec(dllexport) int DllOneFunction()
    {
      return 42;
    }
    
    // dll2.cpp
    #include "dll1.h";
    
    __declspec(dllexport) int DllTwoCaller()
    {
      int n = DllOneFunction();
      // Do stuff with n.
    
      return 0;
    }
    

    Then, when building DLL2, you'd link with DLL1.lib.
    It's been quite a while since I did this so the specifics may be off.


  • Registered Users Posts: 250 ✭✭ikoonman


    daymobrew wrote:
    Is it not (just) a matter of DLL 2 exporting a function and DLL 1 code importing it?

    Thanks for you reply.

    This is correct - but what happens when one app is a dll and the other is an (console?) EXE (where the dll raises the event and the exe intercepts it)?


  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    ikoonman wrote:
    This is correct - but what happens when one app is a dll and the other is an (console?) EXE (where the dll raises the event and the exe intercepts it)?
    Maybe look at Windows that you provide a callback for e.g. EnumWindows() function. The Windows function is in a DLL (like you have) and the callback function will be in your executable. You provide the address of the callback function when you call the DLL code.

    I can't find an example of how the DLL invokes the callback function, but I didn't search too hard or look at Google Groups.


  • Advertisement
  • Registered Users Posts: 23 RazorEdge


    I am not an experienced C++ developer, but need to write some code that would enable a function callback. I am not entirely sure if this is what I need - but essentially, i need to execute a function call in dll "2" from dll "1" if a certain condition is met. i understand that it may involve passing the address of the function i want execute in "2" to "1" so "1" can call it.


    I am unsure about what it is exactly you want to do. Maybe you can explain what it is you are attempting and we can better help you.:)

    Is the DLL loaded by the application that you refer to? In that case, both modules reside in the same memory space so callbacks are very simple. My link above is all you need there. It will get more complicated if there is more than a single thread of execution in your process though.

    If the DLL is loaded by a different application, then you are into the realm of interprocess communication which is more complicated.

    I won't go into too much more detail until I know what it is you are looking for exactly.


  • Registered Users Posts: 250 ✭✭ikoonman


    RazorEdge wrote:
    I am unsure about what it is exactly you want to do. Maybe you can explain what it is you are attempting and we can better help you.:)

    Is the DLL loaded by the application that you refer to? In that case, both modules reside in the same memory space so callbacks are very simple. My link above is all you need there. It will get more complicated if there is more than a single thread of execution in your process though.

    If the DLL is loaded by a different application, then you are into the realm of interprocess communication which is more complicated.

    I won't go into too much more detail until I know what it is you are looking for exactly.

    sorry, my mistake. you are right, that was pretty much all i needed. but i suspect that i may need to make provision for multiple threads. i'd imagine that i need a single "entry" function that spawns a new thread everytime the function is called and process it?


  • Registered Users Posts: 23 RazorEdge


    You are asking me to validate your solution when I don't know what the problem is in the first place!:)

    You must appreciate that anything that I'd advise you therefore is just a waste of your time.

    PM me if neccessary but do describe what problem you are attempting to solve before the solution ends up as a white elephant.


Advertisement