简体   繁体   English

C ++将数组写入注册表中的二进制条目

[英]C++ write an array to a binary entry in the registry

I need to write an array into a BINARY registry key. 我需要将一个数组写入BINARY注册表项。

The array contains a series of characters... 数组包含一系列字符...

char ArrayUserName[100] 字符ArrayUserName [100]

it is loaded with the value of ZK8ORQ1 它加载了ZK8ORQ1的值

I need to write that value to a binary value (REG_BINARY) in the registry. 我需要将该值写入注册表中的二进制值(REG_BINARY)。

void WriteRegistryValues()
{
DWORD  dwDisposition;
FILE *fileb; 
fileb = fopen("C:\\logs\\cplog2.txt", "a+");

fprintf(fileb,"Field ID %d: [%s]\n", 2, ArrayUserName);
fprintf(fileb,"Field ID %d: [%s]\n", 3, ArrayPassword);

HKEY hKey;
LPCTSTR sk = TEXT("SOFTWARE\\XXXX\\YYYY\\GINA");
LONG openRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sk, 0, KEY_ALL_ACCESS , &hKey);


if (openRes==ERROR_SUCCESS) {
    fprintf(fileb, "Success opening key.\n");
} else {
    fprintf(fileb, "Error opening key.\n");
}

LPCTSTR value = TEXT("Domain");
LONG setRes = RegSetValueEx(hKey, value, 0, REG_BINARY, (CONST BYTE*)ArrayUserName, 100);


if (setRes == ERROR_SUCCESS) {
    fprintf(fileb, "Success writing to Registry.\n");
} else {
    fprintf(fileb, "Error writing to Registry.\n");
}

LONG closeOut = RegCloseKey(hKey);

if (closeOut == ERROR_SUCCESS) {
    fprintf(fileb, "Success closing key.\n");
} else {
    fprintf(fileb, "Error closing key.\n");
}

fclose(fileb);

In the text file, i see the values of the username and the password 在文本文件中,我看到用户名和密码的值

as

Field ID 2: [zk8orq1] 栏位ID 2:[zk8orq1]

Field ID 3: [P@ssword2] 栏位ID 3:[P @ ssword2]

Success opening key. 成功打开密钥。

Error writing to Registry. 写入注册表时出错。

Success closing key. 成功关闭键。

What am i missing? 我想念什么?

thank you 谢谢

One thing I can see is that you want to use UCHAR byteRegArray , instead of char , 我可以看到的一件事是,您要使用UCHAR byteRegArray而不是char

 UCHAR byteRegArray[] = {0x01, 0x00....... };
 RegSetValueEx(hKey, "sflaksdfl;asjdf", 0, REG_BINARY, (BYTE *)byteRegArray, sizeof(byteRegArray));

you also need to check what the error message is, not just compare to ERROR_SUCCESS, I believe there are more specific ERROR code. 您还需要检查错误消息是什么,而不仅仅是与ERROR_SUCCESS进行比较,我相信还有更具体的错误代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM