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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Callback function arguments

Callback function arguments

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 2 Posters 1 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.
  • V Offline
    V Offline
    vasanth1004
    wrote on last edited by
    #1

    I'm writing a DLL that has to call a function after a certain event. However from all the articles I've read, you need to declare a function prototype inside the dll to be able to properly call the function. Eg : typedef int (CALLBACK *someFunction)(const byte*, const byte*); But what if I want to callback a function that has an arbitrary number of arguments, or return value? Thanks.

    C 1 Reply Last reply
    0
    • V vasanth1004

      I'm writing a DLL that has to call a function after a certain event. However from all the articles I've read, you need to declare a function prototype inside the dll to be able to properly call the function. Eg : typedef int (CALLBACK *someFunction)(const byte*, const byte*); But what if I want to callback a function that has an arbitrary number of arguments, or return value? Thanks.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      I'm not sure to understand fully your question. You want to be able to call a callback without knowing in advance the number of parameters you need to send ? The only way of doing that is by using '...' in the argument list. But this is really ugly. May I ask why you want to do that ? This looks like a bad design for me.


      Cédric Moonen Software developer
      Charting control

      V 1 Reply Last reply
      0
      • C Cedric Moonen

        I'm not sure to understand fully your question. You want to be able to call a callback without knowing in advance the number of parameters you need to send ? The only way of doing that is by using '...' in the argument list. But this is really ugly. May I ask why you want to do that ? This looks like a bad design for me.


        Cédric Moonen Software developer
        Charting control

        V Offline
        V Offline
        vasanth1004
        wrote on last edited by
        #3

        The reason I need to do this, is because my dll needs to support an arbitrary callback function. The user would "register" the callback function with the dll, by passing a function pointer. However, I need to have a prototype inside the dll anyways to callback any function. I was thinking I could make the user pass in the parameters, and number of parameters when the function is registered, then I could push it on the stack with inline assembly before I call the function. However, this is a little bit tardy way of doing it. Any better suggestions? Thanks

        C 1 Reply Last reply
        0
        • V vasanth1004

          The reason I need to do this, is because my dll needs to support an arbitrary callback function. The user would "register" the callback function with the dll, by passing a function pointer. However, I need to have a prototype inside the dll anyways to callback any function. I was thinking I could make the user pass in the parameters, and number of parameters when the function is registered, then I could push it on the stack with inline assembly before I call the function. However, this is a little bit tardy way of doing it. Any better suggestions? Thanks

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          vasanth1004 wrote:

          The reason I need to do this, is because my dll needs to support an arbitrary callback function.

          Clearly, this is something that I don't understand. Why do you want to do such a thing ? The purpose of the callback is to notify the 'registered' that something happened and in every case, the prototype of your function is the same.

          vasanth1004 wrote:

          I was thinking I could make the user pass in the parameters, and number of parameters when the function is registered, then I could push it on the stack with inline assembly before I call the function. However, this is a little bit tardy way of doing it.

          Yes, this is quite tricky and not elegant at all :~

          vasanth1004 wrote:

          Any better suggestions?

          Yes, change your design so you don't have to use different prototypes :-D. Maybe, if you describe the situation why you need to have an arbitrary function, I can help you to find a better solution. But please, you have to describe the situation so that I can fully understand the problem (and maybe some RELEVANT code snippet can help, altough it's not necessary).


          Cédric Moonen Software developer
          Charting control

          V 1 Reply Last reply
          0
          • C Cedric Moonen

            vasanth1004 wrote:

            The reason I need to do this, is because my dll needs to support an arbitrary callback function.

            Clearly, this is something that I don't understand. Why do you want to do such a thing ? The purpose of the callback is to notify the 'registered' that something happened and in every case, the prototype of your function is the same.

            vasanth1004 wrote:

            I was thinking I could make the user pass in the parameters, and number of parameters when the function is registered, then I could push it on the stack with inline assembly before I call the function. However, this is a little bit tardy way of doing it.

            Yes, this is quite tricky and not elegant at all :~

            vasanth1004 wrote:

            Any better suggestions?

            Yes, change your design so you don't have to use different prototypes :-D. Maybe, if you describe the situation why you need to have an arbitrary function, I can help you to find a better solution. But please, you have to describe the situation so that I can fully understand the problem (and maybe some RELEVANT code snippet can help, altough it's not necessary).


            Cédric Moonen Software developer
            Charting control

            V Offline
            V Offline
            vasanth1004
            wrote on last edited by
            #5

            Basically, I am writing an openGL library to render a scene multiple times from different viewpoints. So, the way my design works is I have the user pass in a pointer to their "Draw()" function (callback method), and my dll can call the Draw function multiple times, with different configurations each time. This is done so that the user doesn't have to explicitly call the Draw function multiple times. My DLL code can work fine if there were no arguments, or I knew the arguments. eg: typedef void (__stdcall *Draw)(void); But what if the user's Draw method had many arguments? How would I change the prototype in the dll to reflect this? This is my problem. Thanks for your help.

            C 1 Reply Last reply
            0
            • V vasanth1004

              Basically, I am writing an openGL library to render a scene multiple times from different viewpoints. So, the way my design works is I have the user pass in a pointer to their "Draw()" function (callback method), and my dll can call the Draw function multiple times, with different configurations each time. This is done so that the user doesn't have to explicitly call the Draw function multiple times. My DLL code can work fine if there were no arguments, or I knew the arguments. eg: typedef void (__stdcall *Draw)(void); But what if the user's Draw method had many arguments? How would I change the prototype in the dll to reflect this? This is my problem. Thanks for your help.

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              Ok I see. But basically, I think there is a design problem there. Ok, let's suppose you have several Draw functions with each a different parameter list. How, in your dll, will you know what parameters you will need to pass ? There is a big problem there. You simply cannot call a function without knowing what you will pass to it, this makes non-sense. I think you need probably to rethink a little bit the design. As I see, it is the users that will use your dll (so you don't have control over this 'code'). Why not simply have a standard callback function ? It work always like that: you have a specific function prototype that can be used as a callback and it's up to the user to follow this prototype (that seems logical).


              Cédric Moonen Software developer
              Charting control

              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