1

Hello I'm trying to make my application to run on startup, and to do this to work on my clients PC firstly I needed to get their PC username, but when I'm trying to make this working I'm getting this error :

E2140 expression must have integral or unscoped enum type

Here's the code:

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";

How Can I convert TCHAR* to Const Char?

A.Krasniqi
  • 165
  • 12
  • 5
    You are adding pointers together. Adding pointers to strings together does not concatenate those strings. – François Andrieux Sep 06 '17 at 19:36
  • @FrançoisAndrieux how to fix it? Could you answer with the code? – A.Krasniqi Sep 06 '17 at 19:38
  • Use a `std::string` if you want to concatenate using the `+` operator, then you can pass it to any C functions using the `str.c_str()` method. Otherwise you need to use `strcat()`. – Dan Sep 06 '17 at 19:40
  • [`std::string` likely helps here.](http://en.cppreference.com/w/cpp/string/basic_string) – user4581301 Sep 06 '17 at 19:40
  • Possible duplicate of [const char\* concatenation](https://stackoverflow.com/questions/1995053/const-char-concatenation) – Dan Sep 06 '17 at 19:41
  • One possible solution: `const TCHAR* czStartName = _T("MY application"); std::wstring ws = std::wstring(czStartName) + pcusername;` use `ws.c_str()` to get concatanated string – marcinj Sep 06 '17 at 19:41
  • @marcinj I got many errors : `"identifier "_T" is undefined"` , `no instance of constructor "std::basic_string<_Elem, _Traits, _Alloc>::basic_string [with _Elem=wchar_t, _Traits=std::char_traits, _Alloc=std::allocator]" matches the argument list` , `no operator "+" matches these operands` – A.Krasniqi Sep 06 '17 at 19:49
  • 1
    @A.Krasniqi that is not an answer to copy paste, just an idea what to do. – marcinj Sep 06 '17 at 19:51
  • `import ` ... – Dan Sep 06 '17 at 19:55
  • Could anyone answer the answer with code? I tried your comments advice, but noone worked! – A.Krasniqi Sep 06 '17 at 20:05
  • @Dan C++ places heavy duties on imports in spite of generally being quite inclusive. – user4581301 Sep 06 '17 at 20:09
  • 1
    Use [`SHGetKnownFolderPath`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762188(v=vs.85).aspx) to reliably get the path to a user's desktop folder. – molbdnilo Sep 06 '17 at 20:47
  • @molbdnilo you probably just [solved X when the question asks Y](http://xyproblem.info/). Probably should be the *real* answer to this question. – Dan Sep 06 '17 at 20:53
  • @Dan, good point. The error message referrd to one thing while the OP asks about another. – Brad S. Sep 06 '17 at 20:58

1 Answers1

1

As others have said in the comments, you cannot concatenate strings in C using the addition operator. You can do something like in this example:

#include <string.h>

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

const char* czExePath = buf;
Dan
  • 4,488
  • 5
  • 48
  • 75
Brad S.
  • 327
  • 1
  • 9
  • Worked! Thank you – A.Krasniqi Sep 06 '17 at 21:33
  • 2
    Depending on C:\\Users\\username being that user's profile directory is a bad idea. It won't always be true. – Joshua Sep 07 '17 at 00:46
  • @Joshua, yes. of course. This little example is imperfect but it directly addresses the compiler error in the question. There are several other issues for sure. – Brad S. Sep 07 '17 at 02:07
  • @Joshua , in which cases it will be not true? – A.Krasniqi Sep 07 '17 at 13:12
  • @A.Krasniqi: It's still *possible* the user directory isn't c:\users; and if c:\users\username directory already exists before the user has logged in the first time that user will be given another directory. – Joshua Sep 07 '17 at 15:17
  • 1
    @A.Krasniqi: Always use SHGetSpecialFolderPath function for this job https://msdn.microsoft.com/en-us/library/windows/desktop/bb762204(v=vs.85).aspx – Joshua Sep 07 '17 at 15:18