在运行时,如何告诉我是否在WinXP +? win32 [英] at runtime, how can I tell if I'm on WinXP+? win32
问题描述
我正在做一些win32字符串API调用,并假设字符串出来作为宽字符串,这是有效的XP和更新。我如何断言这个?这是一个运行时检查还是编译时检查?
I'm making some win32 string API calls and am assuming that the strings come out as wide strings, which is valid on XP and newer. How can I assert this? Is this a runtime check or a compile-time check?
我做错了吗?这里有一个例子:
Am I doing it wrong? Here's an example:
typedef std::basic_string<TCHAR> TString;
inline TString queryRegStringValue(HKEY key, const TString& subkey,
const TString defaultValue = TEXT(""))
{
std::vector<char> out_bytes(256);
DWORD num_bytes = out_bytes.size();
DWORD out_type;
long retval = RegQueryValueEx(key, subkey.c_str(), 0, &out_type,
reinterpret_cast<BYTE*>(&out_bytes[0]), &num_bytes); //comes out as a platform string. wide on XP
if (retval != 0)
return defaultValue;
if (num_bytes > 0)
{
assert(out_type == REG_SZ);
BOOST_STATIC_ASSERT(sizeof(TCHAR)==2); //what if someone runs my code on an older system?
return TString(reinterpret_cast<wchar_t*>(&out_bytes[0]), num_bytes/2); //assumes windows XP (wide string)
}
return TEXT("");
}
推荐答案
。 Windows已经是过去17年的一个本地Unicode操作系统,早在XP发布之前。
It is a non-issue. Windows has been a native Unicode operating system for the past 17 years, long before XP was released. David Cutler's brain child, NT 3.1, was Unicode from day one.
在不太可能的情况下,你的程序落在Window 9x机器上,还有一个API层,可以将您的UTF-16字符串转换为8位字符。使用TCHAR进行新代码开发没有任何意义。
In the unlikely case that your program lands on a Window 9x machine, there's still an API layer that can translate your UTF-16 strings to 8-bit chars. There's no point left in using TCHAR for new code development.
这篇关于在运行时,如何告诉我是否在WinXP +? win32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!