CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2001
    Posts
    1,029

    How to use std::string with GetCurrentDirectory()?

    Hello all,

    I have the following code that works in Debug mode in my program, but not in Release mode:

    Code:
    CString dir;
    GetCurrentDirectory(1000, dir.GetBufferSetLength(1000));
    string path = string(CT2CA(dir)) + "\\tempdir\\test1.jpg";
    In Release mode the CT2CA() call returns garbage.

    Any idea why or how best to fix this to work with std::string??

    Thanks!
    Last edited by lab1; March 13th, 2011 at 12:33 PM.

  2. #2
    Join Date
    Apr 2001
    Posts
    1,029

    Re: How to use std::string with GetCurrentDirectory()?

    I also tried this code:

    Code:
    int mpath =1000;
    std::string str(mpath+1, 0);
    GetCurrentDirectory(mpath, &str[0]);
    but I get this error:

    error C2664: 'GetCurrentDirectoryW' : cannot convert parameter 2 from 'char *' to 'LPWSTR'


    Any idea how to make this work with std::string??

    Thanks!

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: How to use std::string with GetCurrentDirectory()?

    Your release build is set to unicode instead of multibyte
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    Join Date
    Apr 2001
    Posts
    1,029

    Re: How to use std::string with GetCurrentDirectory()?

    hmm I changed it, but now my program just failed with a box that says "Failed to create empty document"

    any idea why?


    Thanks
    Last edited by lab1; March 13th, 2011 at 01:26 PM.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: How to use std::string with GetCurrentDirectory()?

    Quote Originally Posted by lab1 View Post
    hmm I changed it, but now my program just failed with a box that says "Failed to create empty document"

    any idea why?


    Thanks
    Something wrong with your code most likely. Try debugging it.

  6. #6
    Join Date
    Apr 2007
    Posts
    162

    Re: How to use std::string with GetCurrentDirectory()?


  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to use std::string with GetCurrentDirectory()?

    Quote Originally Posted by lab1 View Post
    I also tried this code:

    Code:
    int mpath =1000;
    std::string str(mpath+1, 0);
    GetCurrentDirectory(mpath, &str[0]);
    Even though this will be allowed eventually by ANSI, I don't believe that Visual C++ makes any guarantees on whether that will cause undefined behaviour or not. The std::string class was never guaranteed to be writable, as an implementation could use a reference counted string, and code like the above breaks the reference counting mechanism.

    Instead, the following is guaranteed to work.
    Code:
    int mpath =1000;
    std::vector<char> vec(mpath+1, 0);
    GetCurrentDirectory(mpath, &vec[0]);
    std::string str = &vec[0];
    Regards,

    Paul McKenzie

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to use std::string with GetCurrentDirectory()?

    Why not just use CString and forget about it?

    CString is designed for the Win32 apis that have ANSI and UNICODE versions.

    If you use CStrings in your app and pass these to the Win32 api's (by being sure to leave off the A and W suffix), you can build for ANSI or UNICODE by simply changing a build setting.


    Sure you can use std::string or std::wstring by typedefining it to a TString but it isn't as convenient to pass it to the Win32 api's that accept string buffers (like GetCurrentDirectory as you have found out).

    Lastly, unless you need to support running on Win9x platform, you should build your app for UNICODE. The reason being is all NT (Win2K, XP, Win7, etc) platforms use UNICODE internally so it's a performance hit to build your app for ANSI since it will convert every string to UNICODE anyway..

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