Of course your code crashes. Let me tell you what is happening in it.
>         char *m_pTextUser = "";
>         char *m_pTextPass = "";
>   
Here you initialize two strings to the value "". This means that both 
strings already point to a string. This string is null-terminated, and 
because the string is empty, the string is only one byte long and that 
byte is zero.

>         int nLena = m_pUser->GetTextLength() + 1;
>         int nLenb = m_pPassword->GetTextLength() + 1;
>   
Here you get the length of the username and the password and add one.

>         m_pUser->SetText("terrorcell");
>   
Here you copy the text "terrorcell" into the m_pUser entity, which then, 
probably makes a local copy of "terrorcell".

>         m_pUser->GetText(m_pTextUser, nLena);
>         m_pPassword->GetText(m_pTextPass, nLenb);
>   
And here is your crash. Remember that the strings that both m_pTextUser 
and m_pTextPass point to, they are both 1 byte long. However, by calling 
this command with nLena, is telling the command that the string is nLena 
bytes long, while it only is 1 byte long. Now, the command tries to copy 
the string into m_pTextUser, and copies more bytes into the string than 
it can handle. The moment the program realizes that, the code crashes.

Instead you should either get a pointer to the username and the 
password, or allocate a new string and copy the contents into that. So 
what you should do instead is.

        m_pUser->SetText("terrorcell");

        // Allocate new strings
        int nLena = m_pUser->GetTextLength() + 1;
        int nLenb = m_pPassword->GetTextLength() + 1;
        char *m_pTextUser = new char[nLena];
        char *m_pTextPass = new char[nLenb];

        // Copy the strings
        m_pUser->GetText(m_pTextUser, nLena);
        m_pPassword->GetText(m_pTextPass, nLenb);

        // Display the strings.
        Msg(m_pTextUser);
        Msg(m_pTextPass);

        // Do not forget to deallocate your new strings
        // when you are done using them!
        delete[] m_pTextUser;
        delete[] m_pTextPass


That said, you should go get a C++ book and read about how pointers, 
C-style strings and other basic C++ stuff works. It doesn't look like 
you know what you are doing.

_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives, please 
visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders

Reply via email to