• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 638
  • Last Modified:

Unicode strings and Binary Registry entires.

Hello,

I would like to be able to edit some registry entries. The entries are stored as binary. They are actually unicode strings

i.e. 77 00
for w

I would like to read in the value convert to ansi string make change convert back to unicode and write it back to the registry. I am not sure of the best way or easiest way to make the conversion...  the strings would be of variable length, null terminated 00 00.

Thanks for any help.
Kent
0
KentDRuddick
Asked:
KentDRuddick
2 Solutions
 
robert_marquardtCommented:
Try the Jedi Code Library (http://jcl.sf.net). I have written the JclRegistry functions and they accept REG_BINARY for reading Unicode strings.
For writing you explicitly specify the type.
The transformation is simple. You read a WideString, assign it to a string, change it, assign it to a WideString and write it to the registry.
0
 
mikelittlewoodCommented:
I think you need to take a look at the unicode library and free source code

http://unicode.soft-gems.net/Tools.php

if you want to read something from the registry first though here is how you would put something into a local variable (which I guess you could then convert from unicode)

uses
  registry;

procedure TForm1.ReadRegistryKeys;
var
  FIniFile: TRegIniFile;
  sUser: String;
begin
  FIniFile := TRegIniFile.Create;
  try
    // set the root key to the entry point within the registry
    FIniFile.RootKey:=HKey_Local_Machine;
    // say you had a user key in the database somewhere under an application called "MyApp"
    sUser := FIniFile.ReadString('Software\MyApp, 'Username', '');
  finally
    // free the instance of the
    FIniFile.Free;
  end;
end;
0
 
Mohammed NasmanSoftware DeveloperCommented:
try two functions below to convert from/to unicode strings


Converts Unicode string to Ansi string using specified code page.
  @param   ws       Unicode string.
  @param   codePage Code page to be used in conversion.
  @returns Converted ansi string.
}

function WideStringToString(const ws: WideString; codePage: Word): AnsiString;
var
  l: integer;
begin
  if ws = ' then
    Result := '
  else
  begin
    l := WideCharToMultiByte(codePage,
      WC_COMPOSITECHECK or WC_DISCARDNS or WC_SEPCHARS or WC_DEFAULTCHAR,
      @ws[1], - 1, nil, 0, nil, nil);
    SetLength(Result, l - 1);
    if l > 1 then
      WideCharToMultiByte(codePage,
        WC_COMPOSITECHECK or WC_DISCARDNS or WC_SEPCHARS or WC_DEFAULTCHAR,
        @ws[1], - 1, @Result[1], l - 1, nil, nil);
  end;
end; { WideStringToString }


{:Converts Ansi string to Unicode string using specified code page.
  @param   s        Ansi string.
  @param   codePage Code page to be used in conversion.
  @returns Converted wide string.
}
function StringToWideString(const s: AnsiString; codePage: Word): WideString;
var
  l: integer;
begin
  if s = ' then
    Result := '
  else
  begin
    l := MultiByteToWideChar(codePage, MB_PRECOMPOSED, PChar(@s[1]), - 1, nil, 0);
    SetLength(Result, l - 1);
    if l > 1 then
      MultiByteToWideChar(CodePage, MB_PRECOMPOSED, PChar(@s[1]),
        - 1, PWideChar(@Result[1]), l - 1);
  end;
end; { StringToWideString }


http://www.swissdelphicenter.ch/torry/showcode.php?id=1692
0
 
KentDRuddickAuthor Commented:
Thank you.

Both answers can get me where I am going so I split the points. I felt the first two were best because they also addressed the registry.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now