Josh Line
Josh Line

Reputation: 635

Delphi - Using NtCreateKey with HKCU

and thanks for taking the time to look over my issue.

I am trying to create a key inside HKCU using the native NtCreateKey API.

I have a source that helps me to understand how the API works, but I cannot seem to make it work for HKCU, only HKLM...is this possible?

Source Code I have already:

const
   KeyNameBuffer: AnsiString = '\Registry\Machine\SOFTWARE'; //Assuming I need to change this.....
   NewKeyNameBuffer: AnsiString = 'Parent Key To Create';
   HiddenKeyNameBuffer: AnsiString = 'Main Key To Create';
   HiddenValueNameBuffer: AnsiString = 'Value Key To Create';

 procedure TForm1.btnDemoClick(Sender: TObject);
 var
    KeyName, ValueName: UNICODE_STRING;
    SoftwareKeyHandle, SysKeyHandle, HiddenKeyHandle: THandle;
    Status: ULONG;
    ObjectAttributes: OBJECT_ATTRIBUTES;
    Disposition: ULONG;
    Buffer: array of WideChar;
 begin

    //
    // Open the Software key
    //
   SetLength(Buffer, Length(KeyNameBuffer));
   MultiByteToWideChar(CP_UTF8, 0, @KeyNameBuffer[1], Length(KeyNameBuffer),
       PWideChar(Buffer), Length(Buffer));
   KeyName.Buffer := @Buffer[0];
    KeyName.Length := Length(KeyNameBuffer) * SizeOf(WideChar);
    InitializeObjectAttributes(ObjectAttributes, @KeyName, OBJ_CASE_INSENSITIVE,
       0, nil);
    Status := NtCreateKey(SoftwareKeyHandle, KEY_ALL_ACCESS, ObjectAttributes, 0,
       nil, REG_OPTION_NON_VOLATILE, Disposition);
    if not NT_SUCCESS(Status) then
      raise Exception.Create('Error: Couldn''t open HKLM\Software');
 end;

All of my Type Definitions and API Declarations have been made above that code, I just don't see it being necessarily to post it all.

I am using the Delphi 7 IDE.

Any help on solving this issue would be greatly appreciated.

Thanks.

Upvotes: 2

Views: 643

Answers (1)

David Heffernan
David Heffernan

Reputation: 613302

I believe that you should be using RegCreateKeyEx for this.

However, the answer to your question is that the appropriate key name for HKCU is:

\Registry\User\<Users_SID>

Naturally you have to provide the SID for the current user. This is one of the many services that the Win32 API provides for you in RegCreateKeyEx.

Upvotes: 5

Related Questions