CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #1
    Join Date
    Dec 2008
    Posts
    12

    strcpy_s & strncpy_s with char pointers

    I have been tasked fixing deprecations and need some help with some rather newbie problems. Listed below are two examples of some warnings that I need to address.

    I am seeking some examples on the proper use of strcpy_s & strncpy_s or a link to some "How To:". One thing that I am trying to figure out is, if I am copying into a char *, how do I determine the 2nd parameter, "numberOfElements : Size of the destination string". At first I was doing sizeof(someCharPointer) but then I realized that would not give me what I want. Please forgive the newbie questions, I don't know if this was the correct forum to ask. Thank you for any assistance.


    strcpy example
    bool RegKey::EnumerateValues(LPTSTR pszValueName, LPBYTE lpValue, DWORD& dwValueSize, DWORD& dwValueType, const DWORD dwIndex)
    {
    TCHAR tmpName[2048] = {'\0'};

    DWORD dwTmpNameSize = sizeof(tmpName);

    LONG lRetValue = RegEnumValue(hTheKey_, dwIndex, tmpName, &dwTmpNameSize, NULL, &dwValueType, lpValue, &dwValueSize);
    if(lRetValue == ERROR_NO_MORE_ITEMS)
    {
    _tcscpy(pszValueName, tmpName);
    iLastErrorCode_ = ERROR_NO_MORE_ITEMS;
    return false;
    }

    _tcscpy(pszValueName, tmpName); //To be changed to _tcscpy_s()

    return true;
    }

    strncpy example
    bool RegKey::GetValue(LPCTSTR pszValueName, LPTSTR pszValue, DWORD& dwValueLength)
    {
    TCHAR* tmpBuffer = new TCHAR[dwValueLength + 1];
    memset(tmpBuffer, 0, dwValueLength * sizeof(TCHAR));

    _tcsncpy(pszValue, tmpBuffer, dwValueLength); //To Be changed to _tcsncpy_s()
    delete [] tmpBuffer;
    tmpBuffer = NULL;

    iLastErrorCode_ = ERROR_SUCCESS;
    return true;
    }
    Last edited by Draznar; December 16th, 2008 at 11:18 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured