【发布时间】:2015-05-16 00:36:54
【问题描述】:
尝试将string 转换为const char* 给出“表达式必须具有类类型”
在底线。在没有运气的情况下尝试了一些转换的变体。有任何想法吗?
string GetMachineID()
{
// LPCTSTR szHD = "C:\\"; // ERROR
string ss;
ss = "Err_StringIsNull";
UCHAR szFileSys[255],
szVolNameBuff[255];
DWORD dwSerial;
DWORD dwMFL;
DWORD dwSysFlags;
int error = 0;
bool success = GetVolumeInformation(LPCTSTR("C:\\"), (LPTSTR)szVolNameBuff,
255, &dwSerial,
&dwMFL, &dwSysFlags,
(LPTSTR)szFileSys,
255);
if (!success)
{
ss = "Err_Not_Elevated";
}
stringstream errorStream;
errorStream << dwSerial;
return string(errorStream.str().c_str());
}
const char *cstr = GetMachineID.c_str();
【问题讨论】:
-
不要将明确的
char与TCHARs 混为一谈;要么满TCHAR要么去wchar_t。 -
(例如
LPCTSTR("C:\\")在 Unicode 构建中调用未定义的行为)