Like Tree1Likes
  • 1 Post By oogabooga

GetCurrentDirectory printing only the first character of the path

This is a discussion on GetCurrentDirectory printing only the first character of the path within the Windows Programming forums, part of the Platform Specific Boards category; Why does the following snippet only print out `C` and not the entire path which is way longer?? Code: LPTSTR ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    384

    GetCurrentDirectory printing only the first character of the path

    Why does the following snippet only print out `C` and not the entire path which is way longer??

    Code:
    LPTSTR lpBuffer = (LPTSTR)malloc(256*2);
    GetCurrentDirectory(256, lpBuffer);
    MessageBoxA(NULL, (LPCSTR)lpBuffer, (LPCSTR)"Path", MB_OK);

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,198
    Note: your post is concerned with Windows programming, not C programming.

    Probably because of something else in your program, or in your choice of compiler, or in compiler settings, or in linker settings that you have omitted. There is an almost infinite number of ways those things might affect behaviour of that snippet, and nobody other than you can even guess what they might be.

    When asking a question in a forum, the onus is on YOU to provide relevant context, and to post your question in a forum where it is relevant. You have not done that.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Presumably a wide-character problem.

    If you're in unicode mode your GetCurrentDirectory will resolve to GetCurrentDirectoryW and return a wide-character string. These strings will (most likely in your situation) have a zero-byte every second byte.

    But then you're printing the string with MessageBoxA, which expects a char string. It interprets the first zero-byte as the end of the string.

    You need to get these things straight. You're declaring lpBuffer as a LPTSTR (which will resolve to LPWSTR in unicode mode and LPSTR otherwise) but then casting it to LPCSTR (not only a char string, but constant for some reason).

    MessageBox (or explicitly MessageBoxW) will display the wide string properly.

    EDIT: Read these
    MSDN: Conventions for Function Prototypes
    MSDN: Using Generic Data Types
    Last edited by oogabooga; 01-25-2014 at 12:38 AM.
    Salem likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Thanks for bringing it to my notice. I need some more research on conventions and data types. char's,TCHAR's, WCHAR's, _TCHAR's and all other jumble of data types are particularly confusing and dissuading. Also, what methods do you suggest for string concatenation?? I use strcat and strcat_s but for whatever reasons they change a fairly intelligible English string into a not so amusing Japanese one.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    I would forget about trying to use TCHAR's - they won't be useful to you. Instead, change your compiler settings to either 1) Turn off Unicode and only use char-based types/functions - like strcat() etc. 2) Turn on Unicode and only use wchar_t-based types/functions. Whether or not you explicitly use the trailing A/W is up to you. I tend to use them when posting code so that it just works regardless of the Unicode settings.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing from an opened file character by character
    By SneakySnake in forum C Programming
    Replies: 1
    Last Post: 12-07-2012, 12:05 PM
  2. Printing out self path
    By juice in forum C++ Programming
    Replies: 4
    Last Post: 03-15-2012, 06:38 PM
  3. Help Please - Printing the path for links
    By lewiso in forum Linux Programming
    Replies: 9
    Last Post: 03-04-2008, 03:44 PM
  4. GetCurrentDirectory + '\\'
    By rotis23 in forum Windows Programming
    Replies: 4
    Last Post: 09-08-2003, 07:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21