0

Before when I tried to make a window wrapper class, I learned that you cannot pass a dialog procedure to CreateDialogParam() that is in a class, because it being a class member changes the signature and therefor doesn't match that of DLGPROC. I used a workaround where all dialogs used one global procedure that used a map to find the class member procedure from the window handle passed to the global procedure. It would find the correct class pointer in the map, and pass the arguments to its procedure and return the result.

Now I am using this same method, but, in this project everything is going to be in a namespace. Is this valid?

namespace MyNamespace
{
    INT_PTR MyProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
        return 0;
    }

    class MyDlg
    {
    public:
        HWND hwnd;

        MyDlg(void) {
            hwnd = CreateDialogParam(
                GetModuleHandle(NULL),
                MAKEINTRESOURCE(IDD_MYDLG),
                HWND_DESKTOP,
                (DLGPROC)MyProc, // Maybe 'MyNamespace::MyProc'?
                NULL
            );
        }
    };
}

I'm not sure if namespaces change the function type signature like classes do.

4
  • Did you try it? (yes, it works fine) Commented Oct 5, 2012 at 14:06
  • @tenfour Sorry, I realize I kind of failed in the 'What have you tried?' area, but I am working on a static library and I guess I got lazy and went ahead and asked before whipping up a test project to see if it would work. Commented Oct 5, 2012 at 14:18
  • Note that you declared MyProc incorrectly. The compiler error was trying to tell you that, but you appear to have cast the error message away. Now your code compiles, but it will also corrupt memory. Commented Oct 5, 2012 at 16:46
  • @RaymondChen I never got a compiler error, because I never compiled it. This was just example code because I didn't take the time to create a test project. After looking at my code for half of a second I realize I didn't use CALLBACK in the return type of MyProc, my mistake. Commented Oct 5, 2012 at 17:13

1 Answer 1

2

Yes it does change how you address the function, but in this case you don't need to qualify because you're already inside MyNamespace. So:

Valid:

namespace MyNamespace
{
    INT_PTR MyProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
    class MyDlg
    {
        //....
        MyDlg(void) {
            hwnd = CreateDialogParam(
                GetModuleHandle(NULL),
                MAKEINTRESOURCE(IDD_MYDLG),
                HWND_DESKTOP,
                (DLGPROC)MyProc,
                NULL
            );
        }
    };
}

Invalid:

namespace MyNamespace
{
    INT_PTR MyProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
}
class MyDlg
{
    //....
    MyDlg(void) {
        hwnd = CreateDialogParam(
            GetModuleHandle(NULL),
            MAKEINTRESOURCE(IDD_MYDLG),
            HWND_DESKTOP,
            (DLGPROC)MyProc, // need to use 'MyNamespace::MyProc'?
            NULL
        );
    }
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.