Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CString erased without reason ?

CString erased without reason ?

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RadioOpa
    wrote on last edited by
    #1

    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

    R 1 Reply Last reply
    0
    • R RadioOpa

      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

      R Offline
      R Offline
      Ryan Binns
      wrote on last edited by
      #2

      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 telling GetComputerName() there is actually "dwHostLength" bytes. That's just asking for trouble. Similarly for the user name. Also, when you call GetBuffer(), you have to call ReleaseBuffer() afterwards. Put in the call to ReleaseBuffer() straight after the call to GetComputerName() (and GetUserName()).

      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"

      R 1 Reply Last reply
      0
      • R Ryan Binns

        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 telling GetComputerName() there is actually "dwHostLength" bytes. That's just asking for trouble. Similarly for the user name. Also, when you call GetBuffer(), you have to call ReleaseBuffer() afterwards. Put in the call to ReleaseBuffer() straight after the call to GetComputerName() (and GetUserName()).

        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"

        R Offline
        R Offline
        RadioOpa
        wrote on last edited by
        #3

        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.

        R 1 Reply Last reply
        0
        • R RadioOpa

          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.

          R Offline
          R Offline
          Ryan Binns
          wrote on last edited by
          #4

          Glad to help. BTW, you could just use dwHostLength and dwUserLength in the calls to GetBuffer() 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"

          R 1 Reply Last reply
          0
          • R Ryan Binns

            Glad to help. BTW, you could just use dwHostLength and dwUserLength in the calls to GetBuffer() 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"

            R Offline
            R Offline
            RadioOpa
            wrote on last edited by
            #5

            You´re absolutely right ! Thanks again.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups