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?
Where is the declaration of szString?

You really should adopt the practice of declaring variables where they're used. For example, szString, dwType and dwSize are only used within the if statement, so they should be declared there.

If you want to read registry values, you don't need KEY_ALL_ACCESS. The request for full access will cause RegOpenKeyEx to fail in a more restricted environment. You should only request the access you need.

Why is RegCloseKey not withing the if statement?
Last edited on
HKEY configKey;
DWORD dwType;
DWORD dwSize=200;
char szString[255];

the problem is with these two lines
logindlg.Username_Box.SetWindowTextA(szString);

logindlg.Password_Box.SetWindowTextA(szString);
You need to set the size or else RegQueryValueEx won't know how much data to return. It's possible szString has been overrun. You need to reset it before each call as it's also an output parameter.

I forgot; you aren't checking the type. The query just returns the binary block of data. The type determines how you handle that data. You can only treat it as a string if the type is REG_SZ.
Last edited on
the assertion gives an error where it checks if username_box and password_box are windows...then i guess this validation fails and returns an assertion....nothing to do with registry query or registry type.
Have you looked at the values to confirm they're correct? As your code stands, if the user name is shorter than your password, it won't read all the password.

You still need to check the type and you still need to reset the size before each call and you ought to change that KEY_ALL_ACCESS to something less restrictive.
i have checked that...if i do something like logindlg.Username_Box.SetWindowTextA("123");
it still gives an assertion error....
Well, you've only asked about the registry. As for the login ...

Did you test your login with a fixed userid/passwd that you know works?
my problem is here " I get an assertion error when I try to read registry values and set it into these two boxes from my main dialog code."

when i assign it to the textboxes using setwindowtextA.....

the login is fine as I tested that first....
What exactly is the assertion?

Have you made the recomended changes?
Last edited on
it saws debug assertion in visual studio and asks me to refer to documentation..


something to do with

the assertion gives an error where it checks if username_box and password_box are windows...then i guess this validation fails and returns an assertion
That doesn't mean much to me. You have a few layers of code in there. I presume logindlg is a dialog derived from CDialog and Username_Box is a CEdit. Why is Username_Box public?

You can only use Username_Box while logindlg is active.

Where are you using this code? Is it from another dialog or the main app or something like that?
Last edited on
yes from the main app I am calling this logindlg...
The Username_Box class maps onto a Windows edit control and doesn't do anything of itself. If the Windows edit control does not exist, Username_Box can't actually do anything. I suspect that's the error.

There are a couple of ways of fixing this:
1. You could add a constructor to CLoginDlg to take a userid/passwd. When the dialog starts, it pre-populates the edit control fields for the user. You'd do this in CLoginDlg::InitDialog.
2. You could make the actual login function in CLoginDlg static. So you'd end up with a public static method CLoginDlg::DoLogin. Then you could call that from anywhere in your app without having to instantiate a CLoginDlg class.
i will try to use the initdialog and keep u posted on this...thanks for helping.
LoginDialog::LoginDialog(CWnd* pParent /*=NULL*/)
: CDialog(LoginDialog::IDD, pParent)
{
#ifndef _WIN32_WCE
EnableActiveAccessibility();
#endif
EnableAutomation();
}

is this a constructor?
I dont know if i am doing it correctly but there is still an assertion when I do this

BOOL LoginDialog::OnInitDialog()
{
Username_Box.SetWindowTextA("11");
return true;
}

whereas when I do it in here

void LoginDialog::OnBnClickedButton1()
{
Username_Box.SetWindowTextA("11");
}

it works perfectly fine
The stuff in bold is what you need to add.

You'll have a declaration that looks like:
1
2
3
4
5
6
7
8
9
class LoginDialog : public CDialog
{
public:
    LoginDialog(CWnd* pParent /*=NULL*/);
    LoginDialog(CString strUser, strPass, CWnd* pParent /*=NULL*/);

    //...
private:
    CString m_strUser, m_strPass;


And you'll need to add this constructor.
1
2
3
4
5
6
7
LoginDialog::LoginDialog(CString strUser, strPass, CWnd* pParent /*=NULL*/)
    :   CDialog(pParent),
        m_strUser(strUser),
        m_strPass(strPass)
{
    // You'll need to add any other stuff that appears in the other constructor.
}


And finally:
1
2
3
4
5
6
7
8
BOOL LoginDialog::OnInitDialog()
{
    CDialog::OnInitDialog();
    Username_Box.SetWindowText(m_strUser);
    Password_Box.SetWindowText(m_strPass;
    //...
    return TRUE;
}


And finally, you call it as:
1
2
3
    // ... read user/passwd from the registry into: user, passwd
    LoginDialog logindlg(user, passwd);
    logindlg.DoModal();

Last edited on
you are a genius....just doing

BOOL LoginDialog::OnInitDialog()
{
CDialog::OnInitDialog();
Username_Box.SetWindowText(m_strUser);
Password_Box.SetWindowText(m_strPass;
//...
return TRUE;
}


made everything work....
thanks
Topic archived. No new replies allowed.