GetCurrentDirectory 缓冲区未返回正确的值

发布于 2024-12-14 10:41:39 字数 523 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

私藏温柔 2024-12-21 10:41:39

我想,结果就是 Windows 7 中的 Unicode!该函数的每个 ascii 字符后面都有零。您正在通过 printf 打印它。您应该在程序中使用宽字符函数。就像wprintf一样。

尝试下面的代码:(在 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;
}

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;
}
夏尔 2024-12-21 10:41:39

我不确定,但是在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 as wprintf().

Also I wonder whether the compiler wouldn't have warned you about something being wrong regarding types.

口干舌燥 2024-12-21 10:41:39

你使用什么编译器?在 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文