【问题标题】:Using GetCurrentDirectory with wcstombs将 GetCurrentDirectory 与 wcstombs 一起使用
【发布时间】:2018-05-02 16:35:38
【问题描述】:

我从collegaue 那里得到了一段前段时间写的代码,令人困惑。 代码是:

TCHAR Curr_dir[100];  
char* input_file;  
DWORD a = GetCurrentDirectory(100, Curr_dir);  
size_t i= wcstombs(&input_file[i], Curr_dir, 100);

问题是Curr_dir 不是wcstombs 需要的类型。 有没有其他函数可以做wcstombs 对这种类型的变量所做的事情? 或者有什么方法可以转换?

【问题讨论】:

  • Errors using TCHAR,cannot convert to wchar_t 的可能重复项 - 另外,这似乎是一个 C 问题。
  • @kabanus 虽然,Win32 API 是一个 C API,它也是为 C++ 准备/使用的。提问者可能编写了一个 C++ 程序。因此,这次让他走吧……(顺便说一句,感谢您的链接 - 我“喜欢”接受的答案。)

标签: c windows winapi tchar


【解决方案1】:

看起来您的代码曾经支持多字节和 UNICODE,但在停止为多字节编译后衰减(不再支持 Windows 95)。

仅当您定义了_UNICODE 时,这段代码才有意义。在这种情况下,它在预处理器之后像这样结束:

wchar_t Curr_dir[100];  
char* input_file;  
unsigned long a = GetCurrentDirectoryW(100, Curr_dir);  
size_t i= wcstombs(&input_file[i], Curr_dir, 100);

在这种情况下,GetCurrentDirectoryWCurr_dir 中返回一个 UINCODE(宽字符串),并且由于某种原因,它被转换为多字节字符串。并且字符的类型匹配。

但如果_UNICODE 没有定义它,代码将更改为以下内容:

char Curr_dir[100];  
char* input_file;  
unsigned long a = GetCurrentDirectoryA(100, Curr_dir);  
size_t i= wcstombs(&input_file[i], Curr_dir, 100);

GetCurrentDirectoryA 现在切换到ANSI version of the API,再调用wcstombs 也没有意义了。

通常 MDSN 文档有一个部分,其中包含一个表,其中包含 Generic-Text Routine Mappings 下的字符串函数的所有版本(例如 strcmp-wcscmp-mbscmp)没有“_t”版本的 @ 987654324@ 所以你需要使用#ifdef _UNICODE

但即使 Win32 API 的多字节版本可能仍受支持,继续使用它们也没有任何意义。

更多:https://www.codeproject.com/articles/76252/what-are-tchar-wchar-lpstr-lpwstr-lpctstr-etc

【讨论】:

    猜你喜欢
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    相关资源
    最近更新 更多