2013-10-08 60 views
-2

我正在嘗試創建一個簡單的dll,其中包含一個簡單的對話框和列表框.rc文件。我通過visual studio的幫助創建了資源,並使用拖放控件。我已經暴露了一個實習生請求的函數DialogBox() API。Win32 GUI:未能創建win32 GUI對話框

我從樣本Windows應用程序動態加載DLL並調用暴露的函數。對話框創建失敗,錯誤代碼爲126

任何人都可以幫助我爲什麼它的行爲如此!

下面是代碼:

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; 
} 
+0

你的程序調用'InitCommonControlsEx()'嗎? –

+1

你可以顯示一行或兩行代碼嗎? –

+0

你的代碼在哪裏?我看不到任何代碼。 –

回答

0

你從來沒有分配給gInstance,所以它是默認初始化爲NULL。然後你將它傳遞給DialogBox

hInstance分配到gInstanceWinMain

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

或者只是廢除gInstance完全因爲你不使用它,其他任何地方。刪除變量,讓你WinMain這樣的:

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

還有就是你會忽略,但因爲我看不到的hwnd聲明或初始化更多的代碼。如果可能的話,最好展示一個完整的SSCCE,這顯然是可能的。

同時也要注意雷蒙德對這個問題的評論,並將調用InitCommonControls移到WinMain中。