GetCurrentDirectory缓冲区不返回正确的值 [英] GetCurrentDirectory buffer doesn't return the right value
问题描述
我有一个问题与 GetCurrentDirectory()
,我真的不明白为什么。我不明白的是,它适用于XP,但不是为七(或至少在我的电脑)。有我的代码:
char dir_name [1024] //作为全局变量
int get_files(){
// ...
DWORD dwRet;
dwRet = GetCurrentDirectory(MAX_PATH,dir_name);
printf(%s\\\
,dir_name);
printf(%d\\\
,dwRet);
// ...
}
此代码将返回: p>
printf(%s\\\
- >
,dir_name); returnc
printf(%d\\\
>
,dwRet); 42
(这是应该返回的字符串的正确长度)
dir_name只接受值c。
我认为,结果是Windows 7中的Unicode!并且在这个函数的每个ascii字符后面有零。并且您正在打印 printf
。您应该在程序中使用宽字符函数。
尝试下面的代码:(在Visual Studio 2008 + Windows 7中测试)
$ b
$ b #include< stdio.h>
#include< windows.h>
#include< wchar.h>
WCHAR dir_name [1024]; //作为全局变量
int get_files()
{
// ...
DWORD dwRet;
dwRet = GetCurrentDirectory(MAX_PATH,dir_name);
wprintf(L%s\\\
,dir_name);
printf(%d\\\
,dwRet);
// ...
return 0;
}
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".
解决方案 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. Like wprintf
.
Try below code: (Tested in Visual Studio 2008 + Windows 7)
#include <stdio.h>
#include <windows.h>
#include <wchar.h>
WCHAR dir_name[1024]; // as a global variable
int get_files()
{
// ...
DWORD dwRet;
dwRet = GetCurrentDirectory(MAX_PATH, dir_name);
wprintf(L"%s\n", dir_name);
printf("%d\n", dwRet);
//...
return 0;
}
这篇关于GetCurrentDirectory缓冲区不返回正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!