Question

i'm trying to create a simple dll which has a .rc file with simple dialogbox and listbox within it. i have created the resource by the help of visual studio and by use of drag and drop the controls. i have exposed a function which is intern calls DialogBox() API.

I'm dynamically loading the dll from sample windows application and calling the exposed function. dialog box creation failing with error code 126

could any one help me why it is behaving like this !?

Here is the code:

INT_PTR CALLBACK WndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {

    case WM_INITDIALOG:
        {
               InitCommonControls();
               PopulateList(hwndDlg);
               return TRUE;
        }
    case WM_COMMAND:
        {
          switch(wParam)
          {
          case IDOK:
              SaveSelectedItem(hwndDlg);
               EndDialog(hwndDlg,0);    
               return TRUE;
         case IDCANCEL:
               EndDialog(hwndDlg, 0);
               return TRUE;

          }

        }   
    default:
        DefWindowProc(hwndDlg, uMsg, wParam, lParam);

    }
}
HINSTANCE gInstance;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, int nCmdShow) 
{
DialogBox(gInstance, MAKEINTRESOURCE(IDD_DIALOG), hwnd, WndProc);

return TRUE;
}
Was it helpful?

Solution

You never assign to gInstance and so it is default initialised to NULL. You then pass that to DialogBox.

Assign hInstance to gInstance in WinMain.

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, int nCmdShow) 
{
    gInstance = hInstance;
    DialogBox(gInstance, MAKEINTRESOURCE(IDD_DIALOG), hwnd, WndProc);
    return TRUE;
}

Or just do away with gInstance altogether since you don't use it anywhere else. Delete the variable and make your WinMain like this:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, int nCmdShow) 
{
    DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), hwnd, WndProc);
    return TRUE;
}

There is more code that you are omitting though because I cannot see the declaration or initialisation of hwnd. It's always best to show a complete SSCCE if possible, and it's clearly possible here.

And also take heed of Raymond's comment to the question and move the call to InitCommonControls into WinMain.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top