GetCurrentDirectory 缓冲区未返回正确的值
我对 GetCurrentDirectory()
有疑问,我不太明白为什么。我不明白的是它适用于 XP,但不适用于 7(或至少在我的计算机上)。这是我的代码:
char dir_name[1024]; // as a global variable
int get_files() {
// ...
DWORD dwRet;
dwRet = GetCurrentDirectory(MAX_PATH, dir_name);
printf("%s\n",dir_name);
printf("%d\n",dwRet);
//...
}
此代码将返回:
printf("%s\n",dir_name);
-> return "c"
printf("%d\n",dwRet);
-> 42
(这是应该返回的字符串的正确长度)
我不明白为什么 dir_name 只采用值“c”。
I have an issue with GetCurrentDirectory()
, and i don't really understand why. The thing i don't understand is that it works for XP but not for Seven (or at least on my computer). There is my code:
char dir_name[1024]; // as a global variable
int get_files() {
// ...
DWORD dwRet;
dwRet = GetCurrentDirectory(MAX_PATH, dir_name);
printf("%s\n",dir_name);
printf("%d\n",dwRet);
//...
}
This code will return:
printf("%s\n",dir_name);
-> return "c"
printf("%d\n",dwRet);
-> 42
(which is the right length of the string that should be returned)
I don't understand why dir_name only takes the value "c".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想,结果就是 Windows 7 中的 Unicode!该函数的每个 ascii 字符后面都有零。您正在通过 printf 打印它。您应该在程序中使用宽字符函数。就像
wprintf
一样。尝试下面的代码:(在 Visual Studio 2008 + Windows 7 中测试)
I think, the result is Unicode in Windows Seven! and after each ascii character of this function there is zero. And you are printing it by
printf
. You should use wide-char functions in your program. Likewprintf
.Try below code: (Tested in Visual Studio 2008 + Windows 7)
我不确定,但是在win7下
GetCurrentDirectory()
会返回2字节字符吗?在这种情况下,您将在返回的 char 数组的每一秒字节中得到一个 0。
因此,您应该使用
printf()
函数的宽字符感知版本,例如wprintf()
。另外我想知道编译器是否不会警告您有关类型的错误。
Im not sure, but could it be
GetCurrentDirectory()
returns 2-byte chars under win7?In such case you'll be getting a 0 in each second bytes of the char array returned.
So you should use a wide-char aware version of the
printf()
function such aswprintf()
.Also I wonder whether the compiler wouldn't have warned you about something being wrong regarding types.
你使用什么编译器?在 Visual C++ 2005 下,GetCurrentDirectory 是一个宏,如果定义了 UNICODE 宏,则解析为 GetCurrentDirectoryW,否则解析为 GetCurrentDirectoryA。你有没有定义过 UNICODE?
what compiler are you using? Under Visual C++ 2005, GetCurrentDirectory is a macro that resolves to GetCurrentDirectoryW if UNICODE macro is defined and to GetCurrentDirectoryA otherwise. Do you have UNICODE defined by any chance?