在Debug中无法从'const char *'转换为'LPCTSTR'

时间:2014-07-07 07:58:47

标签: c++ compiler-errors

当我在调试模式下编译代码时,我得到error C2440 "cannot convert from 'const char *' to 'LPCTSTR'"
当我在发布模式下编译代码时,它编译没有错误,我甚至没有得到警告。

错误发生在行:

LPCTSTR lpFileName = strFilenameIni.c_str();

我使用LPCTSTR,因为我使用GetPrivateProfileString从ini文件中读取值,我需要将文件位置转换为LPCTSTR,以便GetPrivateProfileString接受它。

我在编译器设置中搜索了可能导致偏差的内容,但找不到任何内容 我只是无法在调试模式下编译它 我正在使用Visual Studio 2005.

感谢任何帮助。

有问题的代码:

std::string strFilenameIni = "";  //filename of ini file

strFilenameIni = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(textBox_ini_load->Text);

//init ini-parser strings
LPCTSTR lpFileName = strFilenameIni.c_str(); //<- this throws error in debug
LPCTSTR lpSection = "CHECKSUM";
LPCTSTR lpKey = "CRC";

TCHAR TCHAR_inBuf[11]; //buffer for CRC-number
GetPrivateProfileString(lpSection,lpKey,"",TCHAR_inBuf,11,lpFileName); //read CRC
unsigned long ulCRC_IN = strtoul(TCHAR_inBuf,NULL,16); //convert to unsigned long

1 个答案:

答案 0 :(得分:1)

在将字符集更改为注释后,似乎仍然存在一些问题 无论如何,你应该一直使用以下API集之一;不要混淆它们。

对于单字节字符:

char,    LPCSTR,  std::string,  GetPrivateProfileStringA, strtoul,  "literal"

对于宽(2字节)字符:

wchar_t, LPCWSTR, std::wstring, GetPrivateProfileStringW, wcstoul,  L"literal"

对于取决于_UNICODE的情况:

TCHAR,   LPCTSTR, **,           GetPrivateProfileString,  _tcstoul, _T("literal")

**没有标准的预定义方式,您最好使用如下:

typedef std::basic_string<TCHAR> tstring;
相关问题