c++stringtchar

C++ _Tchar units szTestString[] How to assign a string value


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

Solution

  • 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);
    }