c++ - 在调试中无法从 'const char *'转换为 'LPCTSTR'

标签 c++ compiler-errors

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

错误发生在线:

LPCTSTR lpFileName = strFilenameIni.c_str();

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

我在编译器设置中搜索了可能导致偏差但找不到的东西。
我只是无法在 Debug模式下进行编译。
我正在使用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

最佳答案

更改字符集为注释后,似乎仍然存在一些问题。
无论如何,您应该始终使用以下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;

关于c++ - 在调试中无法从 'const char *'转换为 'LPCTSTR',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24605615/

相关文章:

C++ 快速将 int 数组内容转储到文本文件中

php - “PHP Fatal error: Constant expression contains invalid operations”,何时初始化静态内联?

ios - Swift 链接错误 : type metadata accessor for Module. 类

ios - Arc 语义问题 : No known class method for selector (method name here)

c++ - 避免在 cpp 中使用空标记

C++ 原始类型安全

c++ - 进一步了解 i++ 和 i=i+1

c++ - 识别 Windows 中连接的驱动器

java - 在可序列化编程中收到错误 “cannot find symbol”

java - 简单的Java代码无法编译- “Create class '中的 'java.lang'对象'”