Thread: GetCurrentDirectory printing only the first character of the path

  1. #1
    Registered User
    Join Date
    Aug 2011

    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??

    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
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    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 01:38 AM.
    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
    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
    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.


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, 01: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, 04:44 PM
  4. GetCurrentDirectory + '\\'
    By rotis23 in forum Windows Programming
    Replies: 4
    Last Post: 09-08-2003, 07:30 AM