无法将TCHAR *添加到const char

时间:2017-09-06 19:35:19

标签: c++

您好我正在尝试让我的应用程序在启动时运行,并且这样做是为了在我的客户端PC上工作首先我需要获取他们的PC用户名,但是当我试图使这个工作时我得到了这个错误:

E2140 expression must have integral or unscoped enum type

以下是代码:

HKEY hKey;
const char* czStartName = "MY application";
TCHAR pcusername[UNLEN + 1];
DWORD pcusername_len = UNLEN + 1;
GetUserName((TCHAR*)pcusername, &pcusername_len);
const char* czExePath = "\"C:\\Users\\" + pcusername + "\\Desktop\\Myapplication.exe\" /background";

如何将TCHAR *转换为Const Char?

1 个答案:

答案 0 :(得分:1)

正如其他人在评论中所说,你不能使用加法运算符在C中连接字符串。您可以执行以下示例中的操作:

#include <string.h>

char buf[4096];
snprintf(buf, sizeof(buf), "\"C:\\Users\\%s\\Desktop\\Myapplication.exe\" /background", username);

const char* czExePath = buf;