c++ - Reading And Writing Windows Registry in C++ (How to convert string to wchar(?))
问题描述
This is a part of my assignment. I know how to open/read registry keys and create values, but i have few questions. My code:
This is how i write new string value into registry:
void lCreateKeyOne(HKEY hKey, LPCWSTR lSubKey)
{
WCHAR wcValue[] = TEXT"testvalue";
LONG lNewValue = RegSetValueEx (hKey,
L"MytoolsTestKey",
NULL,
REG_SZ,
(LPBYTE)wcValue,
sizeof(wcValue));
}
It works, but i want to generate random string and write it into registry key. This is how i generate random string:
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
int stringLength = sizeof(alphanum) - 1;
char genRandom()
{
return alphanum[rand() % stringLength];
}
srand(time(0));
string Str;
for (unsigned int i = 0; i < 20; ++i)
{
Str += genRandom();
}
- How to write it as registry key ?
- how to convert string Str to WCHAR wcValue[] ?
- I tried to use char instead of wchar and it writes chinese characters https://docs.microsoft.com/en-us/windows/desktop/api/winreg/nf-winreg-regsetvalueexa
解决方案
static wchar_t const * const alphanum{
L"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz" };
constexpr auto stringLength{ wcslen(alphanum) };
wchar_t genRandom()
{
return alphanum[std::rand() % stringLength];
}
// ...
std::srand(static_cast<unsigned>(std::time(nullptr)));
std::wstring Str;
for (std::size_t i{}; i < 20; ++i){
Str += genRandom();
}
推荐阅读
- mysql - mysql查询中的DISTINCT和AVG
- python - 具有多个分隔符和非罗马字符的 Python 字符串拆分
- c# - asp.net '无法绑定多部分标识符“abc@gmail.com”。'
- go - Hyperledger Fabric 链码 - 无法使用 InvokeChaincode 从另一个链码读取数据。状态 200 但有效负载为空
- php - PHP 将数据插入 Apache 日志
- osgi - 服务消费与白板模式
- node.js - 使用 VueSocketio 将多个 Sockets (socket.io) 绑定到单个 Vuex 存储
- c++ - 在这种情况下,如何传递成员函数而不是函数?
- delphi - 为什么 try... except end; 没有捕获到异常?
- javascript - 如何使用 JS 更改 HTML 标签“for”