C++ _Tchar units szTestString[] 如何给字符串赋值
C++ _Tchar units szTestString[] How to assign a string value
请教如何赋值!
我不知道如何将字符串转换为 TCHAR 以写入 reg_sz 注册表
string par = "1234.32.23";
_TCHAR szTestString[] = _T(pra);
完整代码
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);
您不能使用 _T()
宏在运行时转换数据。它仅适用于 compile-time.
处的 char/string 文字
最好的解决方案是只使用 std::wstring
。你可以把它的数据直接传给RegSetValueExW()
,根本不需要复制到_TCHAR[]
缓冲区,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);
}
否则,如果您出于某种原因需要继续使用std::string
,但又想使用RegSetValueExW()
,则您必须将char
数据转换为wchar_t
] 在运行时使用 MultiByteToWideChar()
或等价物,例如:
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);
}
否则,只需使用 RegSetValueExA()
代替,让它在内部为您从 char
转换为 wchar_t
,例如:
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);
}
请教如何赋值! 我不知道如何将字符串转换为 TCHAR 以写入 reg_sz 注册表
string par = "1234.32.23";
_TCHAR szTestString[] = _T(pra);
完整代码
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);
您不能使用 _T()
宏在运行时转换数据。它仅适用于 compile-time.
最好的解决方案是只使用 std::wstring
。你可以把它的数据直接传给RegSetValueExW()
,根本不需要复制到_TCHAR[]
缓冲区,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);
}
否则,如果您出于某种原因需要继续使用std::string
,但又想使用RegSetValueExW()
,则您必须将char
数据转换为wchar_t
] 在运行时使用 MultiByteToWideChar()
或等价物,例如:
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);
}
否则,只需使用 RegSetValueExA()
代替,让它在内部为您从 char
转换为 wchar_t
,例如:
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);
}