Thread: lstrcpy( ) help please

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    lstrcpy( ) help please

    Having a little problem with copying a string from one array to another..

    Here is me' code.. problem code is highlighted:

    Code:
    void EditBoxFileParser(HDC hdc, PAINTSTRUCT* ps, HFONT hFont, HWND hwnd, HWND hEdit)
    {
         
         int iCount; 
         WORD iLength, iOffset;        
         TCHAR **Lines;
         
         //Get Number of Lines in Edit Field
         iCount = SendMessage(hEdit, EM_GETLINECOUNT, 0, 0);
         
         //If Editbox is empty, exit function
         if(!iCount)
         
              return;
         
         Lines = new TCHAR*[iCount];
         
         //Populate 2D array, Lines[LineIndex][LineText]
         for(int i=0; i<iCount; i++)
         {    iOffset = SendMessage(hEdit, EM_LINEINDEX, i, 0);
              iLength = (WORD)SendMessage(hEdit, EM_LINELENGTH, iOffset, 0);
              Lines[i] = new TCHAR[iLength+sizeof(WORD)+1];
              CopyMemory(Lines[i], &iLength, sizeof(WORD));          
              SendMessage(hEdit, EM_GETLINE, i, (LPARAM)Lines[i]);
              Lines[i][iLength] = '\0';
         }   
         
         //Visually Verify Lines[][]
         for(int i=0, x=300, y=275; i<iCount; i++)
              
                 TextOut(hdc, x, y+=20, Lines[i], lstrlen(Lines[i]));   
         
         
        //Parse User Entered Text
        TCHAR **Name = new TCHAR*[iCount];
        TCHAR **FiberCount1 = new TCHAR*[iCount];
        TCHAR **FiberCount2 = new TCHAR*[iCount];;
        int size, word_index;
    	bool cont = true;
        
        for(int i=0, j=0; i<iCount; i++, j=0)
        {   
            
            word_index = 0;
    		cont = true;
            
            do{        
             
                  //Extract Fiber Name
                  while(Lines[i][j]!=',')
                  {
                       j++;
                  }
             
                  Lines[i][j]='\0';
                  Name[i] = new TCHAR[j-word_index+1];
                  lstrcpy(Name[i], Lines[i][word_index]);
                  Name[i][j-word_index] = '\0';
             
                  word_index = j;
    			  word_index++;
             
                  //Extract Beginning Fiber Count
                  while(Lines[i][j]!='-')
                  {
                       j++;
                  }
             
                  Lines[i][j] = '\0';
                  FiberCount1[i] = new TCHAR[j-word_index+1];
                  lstrcpy(FiberCount1[i], Lines[i][word_index]);
                  FiberCount1[i][j-word_index] = '\0';
             
                  word_index = j;
    			  word_index++;
             
                  //Extract Ending Fiber Count
                  while(Lines[i][j]!=',' || Lines[i][j]!='\0')  
                  {
                       j++; 
                  }
    
    			  if(Lines[i][j]=='\0')
    
    				  cont = false:
             
                  Lines[i][j] = '\0';
                  FiberCount2[i] = new TCHAR[j-word_index+1];
                  lstrcpy(FiberCount2[i], Lines[i][word_index]);
                  FiberCount2[i][j-word_index] = '\0';
             
              }while(Lines[i][j]!='\0' && cont == true);     
                     
        }   
        
        //Visually Verify Lines[][]
         for(int i=0, x=400, y=275; i<iCount; i++)
              
                 TextOut(hdc, x, y+=20, Name[i], lstrlen(Lines[i]));  
            
    }
    to me.. this seems like a simple operation.. pass in the cstring to be copied.. pass in a pointer to a source buffer.. null termination takes care of the rest.. but why do I get these compiler errors..??!


    Dev-cpp

    373 F:\Dev-Cpp\cablesplicer.cpp invalid conversion from `char' to `const CHAR*'
    373 F:\Dev-Cpp\cablesplicer.cpp initializing argument 2 of `CHAR* lstrcpyA(CHAR*, const CHAR*)'


    ...
    ...


    Hopefully it is just something simple I am overlooking.. any suggestions == good;
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Lines[i][word_index] is just a char. Try Lines[i] + word_index, which uses pointer arithmetic to get a pointer to the spot you want.

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    very cool.. my dev-cpp loves you
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Exclamation lstrcpy( ) security issue..?!?!

    wow.. I was just looking at MSDN.. and seen this ...

    Security Alert Using this function incorrectly can compromise the security of your application. The first argument, lpString1, must be large enough to hold lpString2 and the closing '\0', otherwise a buffer overrun may occur. Buffer overruns may lead to a denial of service attack against the application if an access violation occurs. In the worst case, a buffer overrun may allow an attacker to inject executable code into your process, especially if lpString1 is a stack-based buffer. Consider using one of the following alternatives: StringCbCopy, StringCbCopyEx, StringCbCopyN, StringCbCopyNEx, StringCchCopy, StringCchCopyEx, StringCchCopyN, or StringCchCopyNEx. You should review Security Considerations: Windows User Interface before continuing.

    i mean.. how often do you come accross something like this.. ?? where a simple api function is declared 'dangerous'....
    Last edited by The Brain; 11-04-2005 at 05:01 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memcpy working strangely in msvc 2005
    By *DEAD* in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2007, 09:50 AM
  2. Efitor Tutorial
    By algi in forum Windows Programming
    Replies: 2
    Last Post: 04-02-2005, 04:02 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM