Can't write char array to Windows Registry key
NickName:Ballers Ask DateTime:2020-08-05T04:32:33

Can't write char array to Windows Registry key

I am trying to write to the Windows Registry using the Win32 API, but for some reason when I try writing bytes to a Registry key with the REG_BINARY data type, the key value does not change.

Here is code that attempts to write to the Registry key:

struct angles
{
    float pitch;
    float yaw;
    float roll;
};
    
int main()
{
    angles ang;
    ang.pitch = 69.25;
    ang.yaw = 420.21;
    ang.roll = 100.0;
    unsigned char b[sizeof(ang)];
    
    std::cout << "Size of structure: " << sizeof(ang) << "\n\n";
    
    memcpy(b, &ang, sizeof(ang));
    
    for (int i = 0; i < sizeof(b); i++)
    {
        printf("Byte %d == %02X ", i, b[i]);
        std::cout << "\n\n";
    }
    
    HKEY handle;
    if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_WRITE, &handle) == ERROR_SUCCESS)
    {
        if (RegSetValueEx(handle, L"test", 0, REG_BINARY, (BYTE*)&b, sizeof(b)) == ERROR_SUCCESS)
        {
            std::cout << "Wrote to key test.\n\n";
        }
        else
        {
            std::cout << "Couldnt write key\n";
        }
    }
    else
        std::cout << "couldn't open key\n";
    
    while (!GetAsyncKeyState(VK_NUMPAD0))
    {
    
    }
    
    return 0;
}

If you compile the code, you'll see that I am copying the bytes of the angles struct into the char array b.

When calling RegOpenKeyEx(), it succeeds. And when calling RegSetValueEx(), it succeeds. But, the value in the Registry does not change.

So, I'm not sure what to do at this point. I am running the application as admin as well.

Copyright Notice:Content Author:「Ballers」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/63254655/cant-write-char-array-to-windows-registry-key

More about “Can't write char array to Windows Registry key” related questions

Can Can for Rails Association

I used can-can gem for managing the roles in my application. The models are as follows class User &lt; ActiveRecord::Base has_many :projects has_and_belongs_to_many :teams end class Pr...

Show Detail

Can can with devise, admin and user

I use cancan and devise, I can update delete and show but I can't create profile. why I can't create new profile ("ActiveModel::ForbiddenAttributesError") class Ability include CanCan::Ability ...

Show Detail

rails 3 can can ability

I am using can can gem to restriction user based on role there is no model for product class ProductsController &lt; ApplicationController authorize_resource :class =&gt; false def index ...

Show Detail

GET request can be bookmarked and POST can not . Can anybody explain on this?

I am studying the HTTP methods. I read that GET request can be bookmarked and POST request can not be bookmarked. Can anybody explain this with an example? Thanks

Show Detail

Can not tnsping but can sqlplus connect

I have a question hopefully someone could explain it to me. I have an Oracle 11g installed properly on the server. From a workstation, I have installed the oracle client which tnsname.ora pointed t...

Show Detail

Can Can with Role Model restriction

I'm Trying to achieve that the user can see only HIS applications, and not the "Application Index" containing all the applications from all Users. My Header: &lt;%= link_to "My Application ",

Show Detail

CAN busoff with CAN_High and CAN_Low shorted

It is known fact that short circuiting CAN_High and CAN_Low on a CAN bus leads to a bus off condition. With respect to the physical layer, how does this condition lead to bus off condition?

Show Detail

What is CAN Active, CAN passive and Sleep state in CAN Network manager?

I am trying to understand the CAN network management in vehicle. During my research, I got to know that CAN network management(CANNM) will make some Mode state to decide the CAN transmission. Those...

Show Detail

Where is CAN ID in CAN Message Frame

I am new to the CAN-BUS protocol. So was going through the CAN Bus Specifications and related documents. I have always used the CAN ID and Frame at the application level. CANID like 0x1a1 CAN Fram...

Show Detail

Service can do ,but Asynctask can not?

I have read about difference between Service,Intent Service &amp; Asynctask ,but I have not figured out any situation where only service works but Asynctask fails. I have noticed some points: Asy...

Show Detail