943,694 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 478
  • C++ RSS
May 9th, 2009
0

assign number to function

Expand Post »
Hi and sorry for the inconvenience

I am trying to make an interface where a function is run given a number(windows API).

I want a default function to be run if it hasn't been overwritten by the user.

So my question is can i assign a number to a function/method
like using this number I am going to access this function.

Any help is deeply appreciated
Similar Threads
Reputation Points: 12
Solved Threads: 1
Light Poster
mostermand is offline Offline
36 posts
since Sep 2008
May 9th, 2009
0

Re: assign number to function

something like this:
C++ Syntax (Toggle Plain Text)
  1. void foo( int x = 0)
  2. {
  3. switch( x )
  4. {
  5. case 0:
  6. printf("case 0\n");
  7. break;
  8. case 1:
  9. printf("case 1\n");
  10. break;
  11. default:
  12. printf("default\n");
  13. break;
  14. }
  15. }
  16.  
  17. int main()
  18. {
  19. foo();
  20. foo(1);
  21. }
Last edited by Ancient Dragon; May 9th, 2009 at 1:17 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
May 9th, 2009
0

Re: assign number to function

Yes but in an automated way so I can add functions at runtime and make it have a value.

I know I could do this with a loop and an if statement but it would be optimal if could check if the message is being handled and abort if not

btw should I use function pointers for this?
Reputation Points: 12
Solved Threads: 1
Light Poster
mostermand is offline Offline
36 posts
since Sep 2008
May 9th, 2009
0

Re: assign number to function

Yes, function pointers might be helpful, but you would still have to have a list of pre-defined functions just as I had in the code I posted. For example if the command string were "say hello" the program could not call a function say() at run time unless it was already known at compile time.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
May 9th, 2009
0

Re: assign number to function

The problem is that it is the windows API we are talking about and I can't have a predefined function for every windows message.

I was thinking something like obtaining a function pointer and the message number from the user and let that overwrite the default behavior(DefWindowProc()).

so I was thinking maybe there was a way of connecting the function and the number
Reputation Points: 12
Solved Threads: 1
Light Poster
mostermand is offline Offline
36 posts
since Sep 2008
May 9th, 2009
0

Re: assign number to function

If the program knows the HWND handle then call SetWindowLong() to hook into its default proc
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
May 9th, 2009
0

Re: assign number to function

Thank You!

this is what I've got.
There are some problems and you can't replace a function but I can handle that myself

CPP Syntax (Toggle Plain Text)
  1. class msghandler
  2. {
  3. public:
  4. void HandleMessage(WNDPROC func, UINT msg);
  5. LRESULT CALLBACK handle(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  6. private:
  7. std::vector<msg_func> msglist;
  8. };
  9.  
  10. LRESULT msghandler::handle(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  11. {
  12. std::vector<msg_func>::iterator iter;
  13.  
  14. for(iter = msglist.begin(); iter < msglist.end(); iter++) //loop until find match
  15. {
  16. if((*iter).msg == msg)
  17. {
  18. return (*iter).function(hWnd, msg, wParam, lParam); //return user defined function's return value
  19. }
  20. }
  21. return DefWindowProc(hWnd, msg, wParam, lParam); //return DefWindowProc
  22. }
  23.  
  24. void msghandler::HandleMessage(WNDPROC func, UINT msg)
  25. {
  26. msg_func temp;
  27.  
  28. temp.function = func;
  29. temp.msg = msg;
  30.  
  31. msglist.push_back(temp);
  32. }
Reputation Points: 12
Solved Threads: 1
Light Poster
mostermand is offline Offline
36 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how to read inful file by using command line?
Next Thread in C++ Forum Timeline: Choosing a Node in a Linked List





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC