tchar into a string

hello again.
I have a new question for you Guys :

how can I Convert what is in my buffer (TCHAR) and copy it into my string

This could sound stupid to you but i'm not very at ease with tchars and stuff.

 

Thanks!

 

Edit : here a code sample of what I approximatly need to do :P

string strPath;

TCHAR buffer[MAX_PATH+1];

DWORD iNombreChars = sizeof(buffer);

iNombreChars = GetCurrentDirectory(iNombreChars, buffer);

for(int iCpt = 0; iCpt < (int)iNombreChars; iCpt++)

{

strPath[iCpt] = buffer[iCpt];

}




Answer this question

tchar into a string

  • myNameIsRon

    yay, thanks, it worked perfectly fine, i add a subfolder, and now i can get \Accounts\<AccountID> from Anywhere on the disk!

    Thnaks a bundle

  • danadanny

    TCHAR is a macro. It contains either a short * or a char *, depending on if Unicode is defined. I believe that std::string also maps to Unicode or ANSI, so this should always work

    TCHAR buffer[MAX_PATH+1];

    DWORD iNombreChars = sizeof(buffer);

    iNombreChars = GetCurrentDirectory(iNombreChars, buffer);

    string strPath(buffer);

    You may need

    string strPath(&buffer[0]);

    but I don't think so.



  • Amit Bhandari

    cgraus wrote:

    OK, sorry. I thought that string had a constructor that took a buffer pointer. This works:

    TCHAR buffer[MAX_PATH+1];

    DWORD iNombreChars = GetCurrentDirectory(MAX_PATH, buffer);

    string strPath;

    strPath.assign(&buffer[0], &buffer[iNombreChars]);

    FWIW, this works because a pointer is an acceptable random access iterator.

    Or simply like this:

    std::string str(MAX_PATH+1, 0);
    GetCurrentDirectory(MAX_PATH, &str[0]);



  • luca morelli

    thanks for helping

    tho im getting this error after doing your steps :

    Error 1 error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it)' : cannot convert parameter 1 from 'TCHAR *__w64 ' to 'std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it' c:\projets\banquet\banquet\banquesecuretrav.cpp 110

    TCHAR buffer[MAX_PATH+1];
    DWORD iNombreChars = sizeof(buffer);

    iNombreChars = GetCurrentDirectory(iNombreChars, buffer);

    string strPath(&buffer[0]); //Also tried without & and [0]


  • Ravi Verma_Ambala

    Quote (from cgraus):

    I believe that std::string also maps to Unicode or ANSI, so this should always work I believe that std::string also maps to Unicode or ANSI, so this should always work

    As far as I understand it, std::string is always basic_string<char> and std::wstring is basic_string<wchar_t>. This means that you need to use std::string if TCHAR is a char (ANSI build) and std::wstring if TCHAR is a short (Unicode build).

    Quote (from cgraus):

    string strPath(buffer);

    You may need

    string strPath(&buffer[0]);

    but I don't think so.

    There is a constructor for basic_string that takes a pointer to the buffer to copy (and an allocator) - Either of the string constructors should work if the type of string matches the current build setting - if ANSI then strPath should be std::string, if Unicode then strPath should be std::wstring.

    Best regards,

    John


  • DaveFinley

    OK, sorry. I thought that string had a constructor that took a buffer pointer. This works:

    TCHAR buffer[MAX_PATH+1];

    DWORD iNombreChars = GetCurrentDirectory(MAX_PATH, buffer);

    string strPath;

    strPath.assign(&buffer[0], &buffer[iNombreChars]);

    FWIW, this works because a pointer is an acceptable random access iterator.



  • RajPratapReddy

    cgraus wrote:

    TCHAR buffer[MAX_PATH+1];
    DWORD iNombreChars = sizeof(buffer);
    iNombreChars = GetCurrentDirectory(iNombreChars, buffer);

    The usage of sizeof here is wrong. GetCurrentDirectory wants the sizeof chars that are allowed, not the buffers size.

    DWORD iNombreChars = sizeof(buffer)/sizeof(TCHAR);

    Would be correct.



  • tchar into a string