0

Please help me how to assign a value! I don't know how to translate from String to TCHAR for writing to the reg_sz registry

string par = "1234.32.23";
_TCHAR szTestString[] = _T(pra);

Full Code

string par = "1234.32.23";   
HKEY ProxyServerKey;
RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &ProxyServerKey, NULL);
_TCHAR szTestString[] = _T(par);
RegSetValueExW(ProxyServerKey, L"ProxyServer", 0, REG_SZ, (BYTE*)szTestString, sizeof(szTestString));
RegCloseKey(ProxyServerKey);
SendNotifyMessageW(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0);
danrom11
  • 27
  • 6
  • Do you really need a `_TCHAR[]` (that's `char[]`)? I'd imagine the function you want to call just needs a `const char*`, then you could use `par.c_str()` instead. Can you show some more code? Or name the function you want to call. You could `strcpy` the string into the array, but there is probably an easier way. – Lukas-T Aug 11 '20 at 19:23
  • @churill I added complete code – danrom11 Aug 11 '20 at 19:29
  • Does this answer your question? [std::string to char\*](https://stackoverflow.com/questions/7352099/stdstring-to-char) – Roy Avidan Aug 11 '20 at 20:26
  • Mixing `TCHAR` with the `W` versions of functions with `std::string` shows a misunderstanding of what one or the other actually does. (It also looks like `_T` is used without understanding what it does.) I'd highly recommend going back and learning about how Windows handles strings and then using wide strings unconditionally when interacting with the APIs. – chris Aug 11 '20 at 20:28
  • 1
    You can just use the `A` version of that API: https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regcreatekeyexa – Vlad Feinstein Aug 11 '20 at 20:28

1 Answers1

2

You can't use the _T() macro to convert data at runtime. It only works on char/string literals at compile-time.

The best solution is to just use a std::wstring instead. You can pass its data directly to RegSetValueExW(), there is no need to copy it into a _TCHAR[] buffer at all, eg:

wstring par = L"1234.32.23";   

HKEY ProxyServerKey;
if (RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &ProxyServerKey, NULL) == ERROR_SUCCESS)
{
    RegSetValueExW(ProxyServerKey, L"ProxyServer", 0, REG_SZ, (const BYTE*)par.c_str(), (par.size()+1) * sizeof(wchar_t));
    RegCloseKey(ProxyServerKey);
    SendNotifyMessageW(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0);
}

Otherwise, if you need to keep using std::string for whatever reason, but want to use RegSetValueExW(), then you will have to convert the char data to wchar_t at runtime using MultiByteToWideChar() or equivalent, eg:

string par = "1234.32.23";   

wstring wpar;
int len = MultiByteToWideChar(CP_ACP, 0, par.c_str(), par.size(), NULL, 0);
wpar.resize(len);
MultiByteToWideChar(CP_ACP, 0, par.c_str(), par.size(), &wpar[0], len);

HKEY ProxyServerKey;
if (RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &ProxyServerKey, NULL) == ERROR_SUCCESS)
{
    RegSetValueExW(ProxyServerKey, L"ProxyServer", 0, REG_SZ, (const BYTE*)wpar.c_str(), (wpar.size()+1) * sizeof(wchar_t));
    RegCloseKey(ProxyServerKey);
    SendNotifyMessageW(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0);
}

Otherwise, just use RegSetValueExA() instead, and let it convert from char to wchar_t internally for you, eg:

string par = "1234.32.23";   

HKEY ProxyServerKey;
if (RegCreateKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &ProxyServerKey, NULL) == ERROR_SUCCESS)
{
    RegSetValueExA(ProxyServerKey, "ProxyServer", 0, REG_SZ, (const BYTE*)par.c_str(), par.size()+1);
    RegCloseKey(ProxyServerKey);
    SendNotifyMessageW(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0);
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770