Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8796489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:38:06+00:00 2026-06-13T23:38:06+00:00

Note: I am using Delphi XE2 I am trying to add a registry key.

  • 0

Note: I am using Delphi XE2

I am trying to add a registry key. Here is my current code:

function AddRegKey(Key:HKEY;Keyname,Value:String):Boolean;
var
  phkResult:HKEY;
begin
  if RegOpenKeyEx(Key, PWideChar('SOFTWARE\Microsoft\Windows\CurrentVersion\Run'), 0, KEY_SET_VALUE, phkResult) = ERROR_SUCCESS then
    begin
      Result := (RegSetValueEx(phkResult, PWideChar(KeyName), 0, REG_SZ, PWideChar(Value), Length(Value)) = ERROR_SUCCESS);
      RegCloseKey(phkResult);
    end
  else Result:=False;
end;

begin
    If AddRegKey(HKEY_CURRENT_USER,'TestTest','123456789') Then
        MessageBox(0,'Success','Test',0);
end.

Which is resulting in the following:

enter image description here

It is only adding the first four characters

But when I change the “Value” parameter to:

123456789123456789

It then adds:

123456789

So it seems as if it is only adding half of the given value for some reason. How do I go about solving this?

Am I passing the wrong data type for the *lpData parameter in RegSetValueEx?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:38:07+00:00Added an answer on June 13, 2026 at 11:38 pm

    Read the documentation. The last parameter of RegSetValueEx() is a byte count, but you are passing it a character count instead (and not even the right count, either – you have to include the null terminator). SizeOf(WideChar) is 2, so you are telling RegSetValueEx() to write only half of the String data. You need to fix that, eg:

    function AddRegKey(Key:HKEY; Keyname, Value: String): Boolean;
    var
      phkResult: HKEY;
    begin
      Result := False;
      if RegOpenKeyEx(Key, PChar('SOFTWARE\Microsoft\Windows\CurrentVersion\Run'), 0, KEY_SET_VALUE, phkResult) = ERROR_SUCCESS then
      begin
        Result := RegSetValueEx(phkResult, PChar(KeyName), 0, REG_SZ, PChar(Value), (Length(Value) + 1) * SizeOf(Char)) = ERROR_SUCCESS;
        RegCloseKey(phkResult);
      end;
    end;
    

    With that said, You might consider using the TRegistry class, which handles these details for you:

    uses
      ..., Registry;
    
    function AddRegKey(Key: HKEY; Keyname, Value: String): Boolean;
    var
      Reg: TRegistry;
    begin
      Result := False;
      try
        Reg := TRegistry.Create(KEY_SET_VALUE);
        try
          Reg.RootKey := Key;
          if Reg.OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Run', True) then
          try
            Reg.WriteString(KeyName, Value);
            Result := True;
          finally
            Reg.CloseKey;
          end;
        finally
          Reg.Free;
        end;
      except
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

( Note : I'm using Scala 2.7.7 here, not 2.8). I'm doing something pretty
I'm working with Embarcadero Delphi XE2 and Firebird DB, using TIBDatabase , TIBTransaction and
I am using Orpheus control v4.08 along with Delphi XE2 and I am facing
I am having problem with IdTCPclient.connected function from Indy in Delphi. I am using
I'm trying to get Delphi to Round like Excel but I can't. Here is
Note: Using Monotouch. I have a directory called Images where I have all my
I found the following claim in the documentation for Net::OpenSSH : Note that using
Note: I'm using Windows file servers and .NET If I were to create a
Note: I'm using Google Chrome Currently I have this test page http://www.evecakes.com/doodles/canvas_size_issue.htm It should
[NOTE: I'm using ASP.NET MVC2 RC2.] I have URLs like this: /customers/123/orders/456/items/index /customers/123/orders/456/items/789/edit My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.