Application's main window a dialog?
Author |
Message |
Damit Senanayak #1 / 13
|
 Application's main window a dialog?
Hi -- Does anyone know how to make a Win32 program have a dialog as its main application window? I know that this can be done using MFC, but I'm trying to hold out as long as possible using the Win32 API. <g> Here's my (non-working) code: BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_INITDIALOG: // get window handles hwndOption[0] = GetDlgItem(hwndDlg, IDC_DELETEALL); hwndOption[1] = GetDlgItem(hwndDlg, IDC_DELETESOME); hwndOption[2] = GetDlgItem(hwndDlg, IDC_RADIOSTATIONS); break; case WM_DESTROY: // if using DialogBox, then uncomment the following line // EndDialog(hwndDlg, 0); break; } return 1; Quote: }
int WINAPI WinMain(...) { HWND hwndDesk = GetDesktopWindow(); // int rc = DialogBox(hInst, MAKEINTRESOURCE(IDD_MYDLG), hwndDesk, DialogProc); CreateDialog(hInst, MAKEINTRESOURCE(IDD_MYDLG), hwndDesk, DialogProc); // CreateDialog(hInst, MAKEINTRESOURCE(IDD_MYDLG), NULL, DialogProc); GPF's in user32 /* etc. */ return 0; Quote: }
-- Damit Senanayake http://www.*-*-*.com/ ICQ: 6930718 Please reply to the group, unless your newsreader doesn't work or you don't have a reliable connection!
|
Sun, 16 Sep 2001 03:00:00 GMT |
|
 |
Tomas Restrep #2 / 13
|
 Application's main window a dialog?
Damit, Are you creating a modal or a modeless dialog as your main window? If it's the first, simply doing WinMain sort of like this works for me: int WINAPI _tWinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpszCmdLine, int nCmdShow) { DialogBox (hInstance, MAKEINTRESOURCE (IDD_MAINWINDOW), NULL, MainDlgProc); return 0; Quote: } // _tWinMain
If you need a modeless dialog (very useful!), I do it like this: int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) { MSG msg; // Create Dialog Window HWND hDlg = CreateDialog(hInst, MAKEINTRESOURCE(IDD_MAIN_DLG), NULL, MainDlgProc); if (hDlg == NULL) { MessageBox (NULL, _T("Can't create Window!"), _T("Tray Menu"), MB_OK | MB_ICONERROR); return 1; } ShowWindow (hDlg, SW_HIDE); SetForegroundWindow (hDlg); while (GetMessage (&msg, NULL, 0, 0) && hDlg != NULL) { if (!IsDialogMessage (hDlg, &msg)) { TranslateMessage (&msg); DispatchMessage (&msg); } } Quote: } /* WinMain */
Both have worked fine for me. As for your code, you were creating a modeless dialog, but I don't see a message loop in your winmain. Without, the dialog would never show. Also, make sure you have the Visisble style in your dialog template, or explicitly call ShowWindow() for it. Hope it helps. -- Tomas Restrepo
http://members.xoom.com/trestrep/
Quote: > Hi -- > Does anyone know how to make a Win32 program have a dialog as its main > application window? I know that this can be done using MFC, but I'm trying > to hold out as long as possible using the Win32 API. <g> > Here's my (non-working) code: > int WINAPI WinMain(...) > { > HWND hwndDesk = GetDesktopWindow(); > // int rc = DialogBox(hInst, MAKEINTRESOURCE(IDD_MYDLG), hwndDesk, > DialogProc); > CreateDialog(hInst, MAKEINTRESOURCE(IDD_MYDLG), hwndDesk, DialogProc); > // CreateDialog(hInst, MAKEINTRESOURCE(IDD_MYDLG), NULL, DialogProc); GPF's > in user32 > /* > etc. > */ > return 0; > } > -- > Damit Senanayake > http://members.xoom.com/damit > ICQ: 6930718 > Please reply to the group, unless your newsreader doesn't work or you don't > have a reliable connection!
|
Sun, 16 Sep 2001 03:00:00 GMT |
|
 |
David Lownd #3 / 13
|
 Application's main window a dialog?
Quote: >Does anyone know how to make a Win32 program have a dialog as its main >application window?
Damit, This is all you need for a dialog application: LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return TRUE; } break; } return FALSE; Quote: }
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, NULL, (DLGPROC)About); return 0; Quote: }
Dave ---- My address is altered to discourage junk mail. Please post responses to the newsgroup thread, there's no need for follow-up email copies.
|
Sun, 16 Sep 2001 03:00:00 GMT |
|
 |
Damit Senanayak #4 / 13
|
 Application's main window a dialog?
Tomas, Dave: I tested this on NT4 SP4 (but I haven't updated my symbols yet), and the dialog failed to create, using this code. However, if I set the "No fail create" style in the dialog's properties (I'm using VC5) then it is created (obviously) but GetLastError() is still 183 (ERROR_ALREADY_EXISTS). This is the revised code (I decided to use a modal dialog box): #define STRICT #include "resource.h" #include <windows.h> BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: if(LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hwndDlg, LOWORD(wParam)); return TRUE; } break; case WM_DESTROY: break; } return FALSE; Quote: }
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR lpCmdLine, int nCmdShow) { int rc = DialogBox(hInst, (LPCTSTR) IDD_MYDLG, NULL, (DLGPROC) DialogProc); LPSTR buf = (LPSTR) HeapAlloc(GetProcessHeap(), 0, sizeof(int)); wsprintf(buf, "%d", rc); MessageBox(NULL, buf, "DialogBox returned", MB_OK); HeapFree(GetProcessHeap(), 0, buf); return GetLastError(); Quote: }
Loaded 'C:\WINNT\System32\ntdll.dll', no matching symbolic information found. Loaded 'C:\WINNT\system32\KERNEL32.DLL', no matching symbolic information found. Loaded 'C:\WINNT\system32\USER32.DLL', no matching symbolic information found. Loaded 'C:\WINNT\system32\GDI32.DLL', no matching symbolic information found. Loaded 'C:\WINNT\system32\ADVAPI32.DLL', no matching symbolic information found. Loaded 'C:\WINNT\system32\RPCRT4.DLL', no matching symbolic information found. Loaded 'C:\Program Files\Winamp\Plugins\GEN_DOCK.DLL', no matching symbolic information found. Loaded 'C:\WINNT\system32\msidle.dll', no matching symbolic information found. Loaded 'C:\Program Files\Microsoft Hardware\Mouse\msh_zwf.dll', no matching symbolic information found. Loaded symbols for 'C:\WINNT\system32\version.dll' Loaded 'C:\WINNT\system32\SHELL32.DLL', no matching symbolic information found. Loaded 'C:\WINNT\system32\COMCTL32.DLL', no matching symbolic information found. Loaded symbols for 'C:\WINNT\system32\lz32.dll' The thread 0x118 has exited with code 183 (0xB7). The program 'C:\Program Files\DevStudio\MyProjects\MyDlg\Debug\MyDlg.exe' has exited with code 183 (0xB7). -- Damit Senanayake http://members.xoom.com/damit ICQ: 6930718 Please reply to the group, unless your newsreader doesn't work or you don't have a reliable connection!
: Hi -- : : Does anyone know how to make a Win32 program have a dialog as its main : application window? I know that this can be done using MFC, but I'm trying : to hold out as long as possible using the Win32 API. <g> : : Here's my (non-working) code: : : BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM : lParam) : { : switch(uMsg) : { : case WM_INITDIALOG: : // get window handles : hwndOption[0] = GetDlgItem(hwndDlg, IDC_DELETEALL); : hwndOption[1] = GetDlgItem(hwndDlg, IDC_DELETESOME); : hwndOption[2] = GetDlgItem(hwndDlg, IDC_RADIOSTATIONS); : break; : case WM_DESTROY: : // if using DialogBox, then uncomment the following line : // EndDialog(hwndDlg, 0); : break; : } : return 1; : } : : : int WINAPI WinMain(...) : { : HWND hwndDesk = GetDesktopWindow(); : // int rc = DialogBox(hInst, MAKEINTRESOURCE(IDD_MYDLG), hwndDesk, : DialogProc); : CreateDialog(hInst, MAKEINTRESOURCE(IDD_MYDLG), hwndDesk, DialogProc); : // CreateDialog(hInst, MAKEINTRESOURCE(IDD_MYDLG), NULL, DialogProc); GPF's : in user32 : /* : etc. : */ : return 0; : } : : : -- : Damit Senanayake : http://members.xoom.com/damit : ICQ: 6930718 : Please reply to the group, unless your newsreader doesn't work or you don't : have a reliable connection! : :
|
Tue, 18 Sep 2001 03:00:00 GMT |
|
 |
Tom Allebran #5 / 13
|
 Application's main window a dialog?
Quote:
> Tomas, Dave: > I tested this on NT4 SP4 (but I haven't updated my symbols yet), and the > dialog failed to create, using this code. However, if I set the "No fail > create" style in the dialog's properties (I'm using VC5) then it is created > (obviously) but GetLastError() is still 183 (ERROR_ALREADY_EXISTS).
Did you remember to include your .RC file in the list of files for the project? That's what I usually forget to do when I do this... --- Tom
TA Software Systems C{*filter*}tesville, VA, USA
|
Tue, 18 Sep 2001 03:00:00 GMT |
|
 |
Damit Senanayak #6 / 13
|
 Application's main window a dialog?
Hi Tom -- : Did you remember to include your .RC file in the list of files for the : project? That's what I usually forget to do when I do this... No, the RC file is already included, and I #include'd "resource.h" in the source file. I'm using VC5 (SP3), BTW. -- Damit Senanayake http://members.xoom.com/damit ICQ: 6930718 Please reply to the group, unless your newsreader doesn't work or you don't have a reliable connection!
|
Tue, 18 Sep 2001 03:00:00 GMT |
|
 |
Tomas Restrep #7 / 13
|
 Application's main window a dialog?
Damit, Is rc == -1 when DialogBox() returns? Because if it isn't, then GetLastError() is most likely returning {*filter*}and you can't trust it's value. Also, Just as a comment, consider that the code you are using to display the number can cause a memory overwrite because the buffer allocated is too small. Other than that I can't see any particular problems with your code. -- Tomas Restrepo
http://www.*-*-*.com/
Quote: > Tomas, Dave: > I tested this on NT4 SP4 (but I haven't updated my symbols yet), and the > dialog failed to create, using this code. However, if I set the "No fail > create" style in the dialog's properties (I'm using VC5) then it is created > (obviously) but GetLastError() is still 183 (ERROR_ALREADY_EXISTS). > This is the revised code (I decided to use a modal dialog box): > #define STRICT > #include "resource.h" > #include <windows.h> > BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg WPARAM wParam, LPARAM > lParam) > { > switch(uMsg) > { > case WM_INITDIALOG: > return TRUE; > case WM_COMMAND: > if(LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) > { > EndDialog(hwndDlg, LOWORD(wParam)); > return TRUE; > } > break; > case WM_DESTROY: > break; > } > return FALSE; > } > int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR lpCmdLine, > int nCmdShow) > { > int rc = DialogBox(hInst, (LPCTSTR) IDD_MYDLG, NULL, (DLGPROC) DialogProc); > LPSTR buf = (LPSTR) HeapAlloc(GetProcessHeap(), 0, sizeof(int)); > wsprintf(buf, "%d", rc); > MessageBox(NULL, buf, "DialogBox returned", MB_OK); > HeapFree(GetProcessHeap(), 0, buf); > return GetLastError(); > } > Loaded 'C:\WINNT\System32\ntdll.dll', no matching symbolic information > found. > Loaded 'C:\WINNT\system32\KERNEL32.DLL', no matching symbolic information > found. > Loaded 'C:\WINNT\system32\USER32.DLL', no matching symbolic information > found. > Loaded 'C:\WINNT\system32\GDI32.DLL', no matching symbolic information > found. > Loaded 'C:\WINNT\system32\ADVAPI32.DLL', no matching symbolic information > found. > Loaded 'C:\WINNT\system32\RPCRT4.DLL', no matching symbolic information > found. > Loaded 'C:\Program Files\Winamp\Plugins\GEN_DOCK.DLL', no matching symbolic > information found. > Loaded 'C:\WINNT\system32\msidle.dll', no matching symbolic information > found. > Loaded 'C:\Program Files\Microsoft Hardware\Mouse\msh_zwf.dll', no matching > symbolic information found. > Loaded symbols for 'C:\WINNT\system32\version.dll' > Loaded 'C:\WINNT\system32\SHELL32.DLL', no matching symbolic information > found. > Loaded 'C:\WINNT\system32\COMCTL32.DLL', no matching symbolic information > found. > Loaded symbols for 'C:\WINNT\system32\lz32.dll' > The thread 0x118 has exited with code 183 (0xB7). > The program 'C:\Program Files\DevStudio\MyProjects\MyDlg\Debug\MyDlg.exe' > has exited with code 183 (0xB7). > -- > Damit Senanayake > http://www.*-*-*.com/ > ICQ: 6930718 > Please reply to the group, unless your newsreader doesn't work or you don't > have a reliable connection!
|
Tue, 18 Sep 2001 03:00:00 GMT |
|
 |
David Lownd #8 / 13
|
 Application's main window a dialog?
Quote: >I tested this on NT4 SP4 (but I haven't updated my symbols yet), and the >dialog failed to create, using this code.
I can't see anything in particular wrong with your code, what do you have on your dialog? Try making it as simple as possible with a single OK button and see if that works. Quote: > LPSTR buf = (LPSTR) HeapAlloc(GetProcessHeap(), 0, sizeof(int));
wsprintf(buf, "%d", rc); MessageBox(NULL, buf, "DialogBox returned", MB_OK); HeapFree(GetProcessHeap(), 0, buf); < Zoiks, what are you doing here? Why not use something simpler for your diagnostics? char szBuff[100]; wsprintf( szBuff, "DialogProc returned %d", rc ); Quote: > return GetLastError();
By the time you do this you've made other API calls such as HeapAlloc & MessageBox, which will have changed the value - you need to call GetLastError immediately after the API that failed. Even then, in the case of a complex operation like DialogBox, it might not give you a precise answer. Dave ---- My address is altered to discourage junk mail. Please post responses to the newsgroup thread, there's no need for follow-up email copies.
|
Tue, 18 Sep 2001 03:00:00 GMT |
|
 |
Tomas Restrep #9 / 13
|
 Application's main window a dialog?
Hi Damit, Quote: > The weird thing is that now it works, and I didn't make any changes to the > code at all! The only thing I did was to remove the "No fail create" style > from the dialog box. I suppose you are right, GetLastError() is returning > crap; I have had that happen before with COM HRESULT errors as well...
Go Figure :) Quote: > :Also, Just as a > : comment, consider that the code you are using to display the number can > cause a > : memory overwrite because the buffer allocated is too small. > But surely sizeof(int) should be large enough? I mean, DialogBox is supposed > to return an int.
YEs, but when you allocate a buffer for chars, sizeof(int) == 4*sizeof(char) which means you'll only be able to store a 3-digit number in it's string form (3chars + '\0'), which is definitly not enough. If the error returned was, say, 124454, you'd get a buffer overrun, which can be pretty tricky to find if they don't cause an access violatyion right away. -- Tomas Restrepo
http://members.xoom.com/trestrep/
|
Tue, 18 Sep 2001 03:00:00 GMT |
|
 |
David Lownd #10 / 13
|
 Application's main window a dialog?
Quote: >But surely sizeof(int) should be large enough?
No, sizeof(int) will give you 4 bytes. You're allocating a 4 byte buffer for a string representation of an integer value. For a 32-bit integer that string could be much longer, for example "-2147483647" which would overrun your buffer and cause you quite a few problems. Quote: >Now, onto the next stage. <g>
Then go slowly, and review your understanding of some basic 'C' coding issues first. Dave ---- My address is altered to discourage junk mail. Please post responses to the newsgroup thread, there's no need for follow-up email copies.
|
Tue, 18 Sep 2001 03:00:00 GMT |
|
 |
Damit Senanayak #11 / 13
|
 Application's main window a dialog?
Tomas -- : Is rc == -1 when DialogBox() returns? Because if it isn't, then GetLastError() : is most likely returning {*filter*}and you can't trust it's value. The weird thing is that now it works, and I didn't make any changes to the code at all! The only thing I did was to remove the "No fail create" style from the dialog box. I suppose you are right, GetLastError() is returning crap; I have had that happen before with COM HRESULT errors as well... :Also, Just as a : comment, consider that the code you are using to display the number can cause a : memory overwrite because the buffer allocated is too small. But surely sizeof(int) should be large enough? I mean, DialogBox is supposed to return an int. : Other than that I : can't see any particular problems with your code. Thanks! Now, onto the next stage. <g> -- Damit Senanayake http://www.*-*-*.com/ ICQ: 6930718 Please reply to the group, unless your newsreader doesn't work or you don't have a reliable connection!
|
Wed, 19 Sep 2001 03:00:00 GMT |
|
 |
Damit Senanayak #12 / 13
|
 Application's main window a dialog?
Hi Tomas -- : Go Figure :) Good heavens, this thing is temperamental! Now, it's failing with GetLastError = 1407 ! (What *is* that, anyway?) : YEs, but when you allocate a buffer for chars, sizeof(int) == 4*sizeof(char) : which means you'll only be able to store a 3-digit number in it's string form : (3chars + '\0'), which is definitly not enough. If the error returned was, say, : 124454, you'd get a buffer overrun, which can be pretty tricky to find if they : don't cause an access violatyion right away. I took David's suggestion of using char buf[100]; instead. Thanks though. -- Damit Senanayake http://members.xoom.com/damit ICQ: 6930718 Please reply to the group, unless your newsreader doesn't work or you don't have a reliable connection!
|
Wed, 19 Sep 2001 03:00:00 GMT |
|
 |
Tomas Restrep #13 / 13
|
 Application's main window a dialog?
Hi Damit, Quote: > : Go Figure :) > Good heavens, this thing is temperamental! Now, it's failing with > GetLastError = 1407 ! (What *is* that, anyway?)
1407 == "Cannot Find Window Class" Quote: > I took David's suggestion of using > char buf[100]; > instead.
That's what I call "the sensible solution" :) -- Tomas Restrepo
http://members.xoom.com/trestrep/
|
Wed, 19 Sep 2001 03:00:00 GMT |
|
|
|