CString erased without reason ?
-
csUserName and csComputerName contain valid entries. As soon as the instance of a Recordset Class is created, both are erased. Interesting: The CString Object csSqlStatement is not erased. code sniplet: CString csComputerName; // Speichert den Hostnamen des PCs CString csUserName; // Speichert den UserNamen ( Windows) CString csTimeNow; // Speichert die Uhrzeit CString csConnectionString; // Verbindungs Daten // Hole aktuelle Zeit COleDateTime coTimeNow(COleDateTime::GetCurrentTime()); csTimeNow.Format( "%04d.%02d.%02d %02d:%02d:%02d", coTimeNow.GetYear(), coTimeNow.GetMonth(), coTimeNow.GetDay(), coTimeNow.GetHour(), coTimeNow.GetMinute(), coTimeNow.GetSecond()); // Hole Hostnamen DWORD dwHostLength = MAX_COMPUTERNAME_LENGTH + 1; GetComputerName( csComputerName.GetBuffer( 0), &dwHostLength); // Hole UserNamen DWORD dwUserLength = UNLEN + 1; GetUserName( csUserName.GetBuffer( 0), &dwUserLength); // Instanz zur Datenbank aufbauen csConnectionString = "DSN=ODR_CRM;UID=ODRGMBH;PWD=origin"; m_pDB = new CDatabase(); if( m_pDB->OpenEx(csConnectionString, CDatabase::noOdbcDialog)) { // We´re connected to the database } else { // we have a problem connecting to the database } // !!!!! // ! Next statement will erase csComputerName and csUserName ! // Access to Class Protokoll Table m_pProtocol = new CProtokoll( m_pDB); m_pProtocol->Open(); // ready to use
-
csUserName and csComputerName contain valid entries. As soon as the instance of a Recordset Class is created, both are erased. Interesting: The CString Object csSqlStatement is not erased. code sniplet: CString csComputerName; // Speichert den Hostnamen des PCs CString csUserName; // Speichert den UserNamen ( Windows) CString csTimeNow; // Speichert die Uhrzeit CString csConnectionString; // Verbindungs Daten // Hole aktuelle Zeit COleDateTime coTimeNow(COleDateTime::GetCurrentTime()); csTimeNow.Format( "%04d.%02d.%02d %02d:%02d:%02d", coTimeNow.GetYear(), coTimeNow.GetMonth(), coTimeNow.GetDay(), coTimeNow.GetHour(), coTimeNow.GetMinute(), coTimeNow.GetSecond()); // Hole Hostnamen DWORD dwHostLength = MAX_COMPUTERNAME_LENGTH + 1; GetComputerName( csComputerName.GetBuffer( 0), &dwHostLength); // Hole UserNamen DWORD dwUserLength = UNLEN + 1; GetUserName( csUserName.GetBuffer( 0), &dwUserLength); // Instanz zur Datenbank aufbauen csConnectionString = "DSN=ODR_CRM;UID=ODRGMBH;PWD=origin"; m_pDB = new CDatabase(); if( m_pDB->OpenEx(csConnectionString, CDatabase::noOdbcDialog)) { // We´re connected to the database } else { // we have a problem connecting to the database } // !!!!! // ! Next statement will erase csComputerName and csUserName ! // Access to Class Protokoll Table m_pProtocol = new CProtokoll( m_pDB); m_pProtocol->Open(); // ready to use
You have to pass a length to
CString::GetBuffer()
. By passing 0, you're allocating a buffer of 0 bytes to receive the computer name in, but tellingGetComputerName()
there is actually "dwHostLength" bytes. That's just asking for trouble. Similarly for the user name. Also, when you callGetBuffer()
, you have to callReleaseBuffer()
afterwards. Put in the call toReleaseBuffer()
straight after the call toGetComputerName()
(andGetUserName()
).Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
You have to pass a length to
CString::GetBuffer()
. By passing 0, you're allocating a buffer of 0 bytes to receive the computer name in, but tellingGetComputerName()
there is actually "dwHostLength" bytes. That's just asking for trouble. Similarly for the user name. Also, when you callGetBuffer()
, you have to callReleaseBuffer()
afterwards. Put in the call toReleaseBuffer()
straight after the call toGetComputerName()
(andGetUserName()
).Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
Thanks a lot ! Changed it into: // Hole Hostnamen DWORD dwHostLength = MAX_COMPUTERNAME_LENGTH + 1; GetComputerName( csComputerName.GetBuffer( MAX_COMPUTERNAME_LENGTH + 1), &dwHostLength); csComputerName.ReleaseBuffer(); // Hole UserNamen DWORD dwUserLength = UNLEN + 1; GetUserName( csUserName.GetBuffer( UNLEN + 1), &dwUserLength); csUserName.ReleaseBuffer(); and now it works.
-
Thanks a lot ! Changed it into: // Hole Hostnamen DWORD dwHostLength = MAX_COMPUTERNAME_LENGTH + 1; GetComputerName( csComputerName.GetBuffer( MAX_COMPUTERNAME_LENGTH + 1), &dwHostLength); csComputerName.ReleaseBuffer(); // Hole UserNamen DWORD dwUserLength = UNLEN + 1; GetUserName( csUserName.GetBuffer( UNLEN + 1), &dwUserLength); csUserName.ReleaseBuffer(); and now it works.
Glad to help. BTW, you could just use
dwHostLength
anddwUserLength
in the calls toGetBuffer()
instead of computing the length twice. It's a bit safer if you change the calculation later and forget to change both places...Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Glad to help. BTW, you could just use
dwHostLength
anddwUserLength
in the calls toGetBuffer()
instead of computing the length twice. It's a bit safer if you change the calculation later and forget to change both places...Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"