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

Recommended Answers

All 6 Replies

something like this:

void foo( int x = 0)
{
   switch( x )
   {
       case 0:
           printf("case 0\n");
           break;
         case 1:
           printf("case 1\n");
           break;
        default:
           printf("default\n");
           break;
    }
}

int main()
{
    foo();
    foo(1);
}

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?

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.

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

If the program knows the HWND handle then call SetWindowLong() to hook into its default proc

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

class msghandler
{
public:
    void HandleMessage(WNDPROC func, UINT msg);
    LRESULT CALLBACK handle(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
private:
    std::vector<msg_func> msglist;
};

LRESULT msghandler::handle(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    std::vector<msg_func>::iterator iter;

    for(iter = msglist.begin(); iter < msglist.end(); iter++)  //loop until find match
    {
        if((*iter).msg == msg)
        {
            return (*iter).function(hWnd, msg, wParam, lParam); //return user defined function's return value
        }
    }
    return DefWindowProc(hWnd, msg, wParam, lParam);  //return DefWindowProc
}

void msghandler::HandleMessage(WNDPROC func, UINT msg)
{
    msg_func temp;

    temp.function = func;
    temp.msg = msg;

    msglist.push_back(temp);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.