问题描述
我使用的是 Win32 API,我需要复制用 argv
捕获的参数,问题是代码必须同时符合 ASCII 和 UNICODE,这是 Windows 中 C/C++ 的问题。
除此之外,我必须尽可能使用 C++ 而不是 C,所以我使用 std::copy
来复制 LPTSTR
(或 TCHAR*
)类型的参数,我可以使用 { {1}} 但正如我所说,它必须尽可能使用 C++。
注意:我不能使用 _tcscpy_s
或 std::wstring
,因为它们必须传递给 std::string
并且参数必须是 CreateProcess()
类型,因此它可以将其转换为TCHAR*
或 LPTSTR
取决于编码。
这是一个控制台可执行的最小可重现示例:
LPWSTR
#include <windows.h>
#include <tchar.h>
#include <corecrt_io.h>
#include <fcntl.h>
#include <iostream>
#ifdef UNICODE
#define tcout wcout
#define tcin wcin
#else
#define tcout cout
#define tcin cin
#endif
我的问题是:
代码是否安全,和/或是否有更好的替代方案(最好使用 C++)?
(不仅是复制部分,还有整个想法)
解决方法
您应该使用std::basic_string
:
using tstring = std::basic_string<TCHAR>;
它自己处理所有的复制。每当您需要与某些 C API 交谈时,将 str.c_str()
用于 const 指针,将 str.data()
(C++17 之后)或 &str[0]
(C++17 之前)用于非常量指针。
#include <windows.h>
#include <tchar.h>
#include <corecrt_io.h>
#include <fcntl.h>
#include <iostream>
#include <string>
using tstring = std::basic_string<TCHAR>;
#ifdef UNICODE
static auto& tcout = std::wcout;
static auto& tcin = std::wcin;
#else
static auto& tcout = std::cout;
static auto& tcin = std::cin;
#endif
int _tmain(int argc,LPTSTR argv[])
{
tstring fileName;
if (argc > 1)
{
fileName = argv[1];
}
else
{
tcout << _T("Program name: ");
tcin >> fileName;
}
tcout << fileName;
return 0;
}