Assertion error

I have another dialog called Login Dialog.
Basically it has two text boxes. One is Username_Box and another is Password_Box.
this is basically just to login to my main dialog.

however I get an assertion error when I try to read registry values and set it into these two boxes from my main dialog code.
1
2
3
4
5
6
7
8
9
10
11
12
13
CWnd* TheMainWindow = AfxGetMainWnd();
        CDialog::OnCancel();
        LoginDialog logindlg;
        TheMainWindow = &logindlg;
        if(RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\ConfApp\\", 0, KEY_ALL_ACCESS, &configKey)==ERROR_SUCCESS)
        {
            RegQueryValueEx(configKey,"Username",NULL,&dwType,(BYTE *)szString,&dwSize);
            logindlg.Username_Box.SetWindowTextA(szString);
            RegQueryValueEx(configKey,"Password",NULL,&dwType,(BYTE *)szString,&dwSize);
            logindlg.Password_Box.SetWindowTextA(szString);
               }
        RegCloseKey(configKey);
        logindlg.DoModal();


does anyone know how to solve this assertion problem?

how does one go about solving assertion problems and what causes assertion problems?
hello carlsum1986,

i guess that 'RegQueryValueEx' writes the data into 'szString'. so what's 'szString'? has it enough capacity to hold the data? contains 'dwSize' the correct number of bytes for 'szString'?
szString has enough capacity.

HKEY configKey;
DWORD dwType;
DWORD dwSize=200;
char szString[255];

i am pretty sure it is the setwindowtextA function
i am pretty sure it is the setwindowtextA function
that's a windows function, right? so very likely not.
you expect, that szString ends with 0. Does that happen?
szString does not end with 0 it's value is usually usernames and passwords.
even if i do logindlg.Username_Box.SetWindowTextA("it doesnt work");
well, I'm sure that SetWindowTextA expects a '\0' and the end. how else will it recognize the end?
otherwise are there any threads involved?
even with a \0 it gives me an assert error.
well then, what does that assertion say? no threads involved?
debug assertion failed...

it seems to fail on the part of the code where it checks if the dialog is a window....
the last thing that comes to my mind is that 'Username_Box' is not correctly initialized at that time.

Maybe you have to find a method like OnAfterInitialized (or so) where you can do your thing
what do u mean not correctly initialized?
find a method for onafterinitialized?
it seems to fail on the part of the code where it checks if the dialog is a window....
this sounds as if the window (logindlg? Username_Box?) doesn't exists when you call SetWindowTextA. So you have to call it later. but i can't tell you where or when. Is it MFC? then having the class is not enough...
yes it is MFC.....what do u mean by having the classs is not enough?even if i call it later like after logindlg.domodal()...it still hits the assertion error.
man....

looking at the help of VC++ it says

1
2
3
4
5
6
7
8
9
10
11
BOOL CSimpleDlg::OnInitDialog()
{
   CDialog::OnInitDialog();

   // TODO: Add extra initialization here
   m_cMyEdit.SetWindowText(_T("My Name")); // Initialize control values
   m_cMyList.ShowWindow(SW_HIDE);      // Show or hide a control, etc.

   return TRUE;  // return TRUE unless you set the focus to a control
   // EXCEPTION: OCX Property Pages should return FALSE
}


You can't do something like this:
1
2
3
CSimpleDlg dlg;

dlg.m_cMyEdit.SetWindowText(_T("My Name"));
and expect that it works

You have to provide the dialog with your strings, create the dialog and SetWindowText at the appropriate time.

Well, better consider using something else but MFC (like .Net, wxWidget, Qt ...)
i cant use something else besides MFC cause it a requirement.....
i dont understand what u mean by


You can't do something like this:
CSimpleDlg dlg;

dlg.m_cMyEdit.SetWindowText(_T("My Name"));

and expect that it works


I was shown on one guide on the web that to replace the current dialog in the main window i just do

CWnd* TheMainWindow = AfxGetMainWnd();
CDialog::OnCancel();
LoginDialog logindlg;
TheMainWindow = &logindlg;
well that excerpt doesn't tell enough.
What about CDialog::OnCancel();? Doesn't it end your dialog and hence destroy all your windows (including Username_Box)?

You should post it in Windows section of this board
LoginDialog::OnCancel();

yes this destroy the main window...

will there be ppl who can help me on the windows section rather than here?
this is general C++ programming section and your problem is a MFC related. Looking for a Windows/MFC-forum wouldn't be the worst thing you can do (www.codeproject.com seems not so bad for instance).
thanks for trying to help me anyway
Topic archived. No new replies allowed.