RegSetValueEx and CHAR
RegSetValueEx and CHAR
考虑以下代码
addHash("hash");
bool addHash(char* hash) {
HKEY hKey = 0;
int code = RegOpenKey(HKEY_CURRENT_USER, subkey, &hKey);
const int length = strlen(hash)+1;
WCHAR whash[100];
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, hash, strlen(hash), whash, 100);
LONG setRes = RegSetValueEx(hKey, L"hash", 0, REG_SZ, (LPBYTE)whash, strlen(hash)+1);
return true;
}
在代码被编译和执行之后,"ha"被放入注册表。有人能告诉我问题出在哪里吗?
提前谢谢!
最后一个参数是倒数第二个参数所指向的字节数,而不是字符数。
因此,whash
的前五个字节(strlen(hash) + 1
)将存储在注册表中。更改为:
LONG setRes = RegSetValueEx(hKey,
L"hash",
0,
REG_SZ,
(LPBYTE)whash,
(wcslen(whash) + 1) * sizeof(WCHAR));
您可能还需要初始化whash
(我不认为MultiByteToWideChar()
会为您添加空终止符):
WCHAR whash[100] = { 0 };
我想这就是你想要做的:
#include <tchar.h>
#include <Windows.h>
using namespace std;
bool addHash(wstring hash) {
const wchar_t* wHash = hash.c_str();
LONG ret = RegSetKeyValue(HKEY_CURRENT_USER, _T("Software\aa\test"), _T("hash"), REG_SZ, wHash, hash.length() * sizeof(wchar_t));
return (ret == ERROR_SUCCESS);
}
int main()
{
addHash(_T("A42B2094EDC43"));
return 0;
}
希望这能有所帮助;)
相关文章:
- C++ const char with .begin() and .end()
- 指针混乱char* and int*
- Concatenate char[] and uint32_t in C++
- getline() and char array
- Unicode const char* to JString using JNI and C++
- Char* pointers and char[]
- std::string and const char *
- jni String to *char and java.lang.UnsatisfiedLinkError:
- Evaluate an int, char and int
- RegSetValueEx and CHAR
- While loop and char
- Interchanging const char* and std::string
- Char size and const void*
- While loop and cin char, int
- C++ 连接字符串会导致"invalid operands of types ‘const char*’ and ‘const char"
- const char * and char *
- char * and char[]
- char* and cin in C++
- string and const char* and .c_str()?
- char and String in c++