|
-
March 13th, 2011, 12:21 PM
#1
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.
-
March 13th, 2011, 12:32 PM
#2
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!
-
March 13th, 2011, 12:43 PM
#3
Re: How to use std::string with GetCurrentDirectory()?
Your release build is set to unicode instead of multibyte
-
March 13th, 2011, 01:10 PM
#4
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.
-
March 13th, 2011, 03:10 PM
#5
Re: How to use std::string with GetCurrentDirectory()?
 Originally Posted by lab1
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.
-
March 13th, 2011, 05:53 PM
#6
Re: How to use std::string with GetCurrentDirectory()?
-
March 13th, 2011, 08:34 PM
#7
Re: How to use std::string with GetCurrentDirectory()?
 Originally Posted by lab1
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
-
March 13th, 2011, 10:35 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|