assign number to function

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 36
Reputation: mostermand is an unknown quantity at this point 
Solved Threads: 1
mostermand mostermand is offline Offline
Light Poster

assign number to function

 
0
  #1
May 9th, 2009
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
All i've got is a slice of pi
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: assign number to function

 
0
  #2
May 9th, 2009
something like this:
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 36
Reputation: mostermand is an unknown quantity at this point 
Solved Threads: 1
mostermand mostermand is offline Offline
Light Poster

Re: assign number to function

 
0
  #3
May 9th, 2009
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?
All i've got is a slice of pi
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: assign number to function

 
0
  #4
May 9th, 2009
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 36
Reputation: mostermand is an unknown quantity at this point 
Solved Threads: 1
mostermand mostermand is offline Offline
Light Poster

Re: assign number to function

 
0
  #5
May 9th, 2009
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
All i've got is a slice of pi
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: assign number to function

 
0
  #6
May 9th, 2009
If the program knows the HWND handle then call SetWindowLong() to hook into its default proc
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 36
Reputation: mostermand is an unknown quantity at this point 
Solved Threads: 1
mostermand mostermand is offline Offline
Light Poster

Re: assign number to function

 
0
  #7
May 9th, 2009
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

  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. }
All i've got is a slice of pi
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC