Question

Saving Typr Array to Registry

Asked by: bubbles

I am in the process of writing a program that needs an indiviudal user to be able to setup their own profile within the program, and I need the program to save this to the registry. I want the program to be able to handle unlimited users, and store information such as their name, e-mail address and password.

I think I need to define a type that has the variables for each piece of info I want to store, but I am unsure of how to do this and how to save each one to a seperate registry key. Can someone please provide some code

Thanks.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2002-08-26 at 16:47:12ID20342333
Tags

array

,

saving

Topic

Delphi Programming

Participating Experts
1
Points
400
Comments
6

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. The Registry
    2 things here. Answer either one. 1) How do I write a program to configure the registry, so that whenever a file of a certain extension is clicked, Windows will fire up my application and my application will open the file? 2) Add a new item to the "right-click pop-up&q...
  2. Registry
    I asked this before and I guess I sort of jumped the gun with the answer. I am having a problem writing to the registry of an NT system. I can write to the registry using savesetting and retrieve using getsetting, but I need to be able to choose the HKEY root I want I've u...
  3. registry
    hi all, could somone please tell me where i can get my hands on some source code that i can use as a registry editor? What i really mean by this is, a combobox with HKEY_LOCAL... in it and a list box providing all the keys and when a key is clicked it is then displayed...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Slick812Posted on 2002-08-26 at 22:50:13ID: 7244272

hello bubbles, it's not so difficult to write binary data to the registry, but it seems like it would be better and faster to write your user data to a file and read the info from the file. Here is some code that will write and read a UserData Type to and from the Registry


type
  TUserData = Record
    Number: Integer;
    FirstName: Array[0..14] of Char;
    MiddleIni: Char;
    LastName: Array[0..16] of Char;
    Account: Word;
    ARect: TRect;
    ShoeSize: Single;
    end;



procedure TForm1.sbut_Save2RegClick(Sender: TObject);
var
Reg1: TRegistry;
UserData1: TUserData;
begin
UserData1.Number := 876121;
UserData1.FirstName := 'Mikey';
UserData1.MiddleIni := 'G';
UserData1.LastName := 'MacFerson, Levy';
UserData1.Account := 321;
UserData1.ARect := Rect(43, 127, 234, 382);
UserData1.ShoeSize := 11.5;
Reg1 := TRegistry.Create;
try
  Reg1.RootKey := HKEY_CURRENT_USER;
  if Reg1.OpenKey('\Software\YourAppName', True)
    then begin
      Reg1.WriteBinaryData('user1',UserData1, SizeOf(UserData1));;
      Reg1.CloseKey;
    end else ShowMessage('No GO');
  finally
    Reg1.Free;
  end;
end;

procedure TForm1.sbut_ReadRegClick(Sender: TObject);
var
Reg1: TRegistry;
UserData1: TUserData;
begin
Reg1 := TRegistry.Create;
try
  Reg1.RootKey := HKEY_CURRENT_USER;
  if Reg1.OpenKey('\Software\YourAppName', False)
    then begin
      Reg1.ReadBinaryData('user1',UserData1, SizeOf(UserData1));;
      Reg1.CloseKey;
    end else ShowMessage('No GO');
  finally
    Reg1.Free;
  end;
if UserData1.Number = 876121 then
ShowMessage('Name is '+UserData1.FirstName+' '+UserData1.MiddleIni+'. '+UserData1.LastName+' ShoeSize is '+FloatToStr(UserData1.ShoeSize));
end;


- - - - - - - - - - - - - - - - - - - - - - - -
hope this helps you, ask questions if you need more info.

 

by: bubblesPosted on 2002-08-28 at 01:23:16ID: 7245919

Thanks for your comment - I wasn't sure about whether I had been clear enough in my question so this is a great start.

First question here. Say I have 10 users - what is the best way of managing them in the program so I can select any one of them from a list box? I kind of imagined some kind of array of the type that has an index so I can ask for record 1,2,3,4 or whatever. I have a long background in VB so I understand perfectly how to do it in that environment, but I am trying to understand how it works in Delphi.

Secondly, I can see your point about saving this info to a file. Maybe if you had some code illustrating that it would help. I am willing to raise the points on this question.

Thanks for your help.

 

by: Slick812Posted on 2002-08-28 at 10:56:51ID: 7247083

OK, here's some more code to use arrays in Delphi. I use a Dynamic array, AryUser, which means that the array has a variable length, so you need to set the length of the array


TUserData = Record
    Number: Integer;
    FirstName: String[14];
{I changed the names to Strings, which are easier for
beginers to use}
    MiddleIni: Char;
    LastName: String[16];
    Account: Word;
    ARect: TRect;
    ShoeSize: Single;
    end;


{declare your Array without any size [0..42]}
private
    { Private declarations }
    AryUser: Array of TUserData;





procedure TForm1.button_AddUserClick(Sender: TObject);
var
AryLen: Byte;
begin
{this procedure adds users to the AryUser Array}
if Length(AryUser) > 30 then
  begin
  {limit the amount of users, you can use any amoumt but some
  cutoff should be used}
  ShowMessage('the Max of 30 users has been reached, NO MORE USERS!');
  Exit;
  end;
AryLen := Length(AryUser);
{when you use Dymanic arrays you have to set the length,
it is important to have enough room in your array, or you will
get exeptions for tring to access array member that do not exist}
SetLength(AryUser, AryLen +1);

AryUser[High(AryUser)].Number := 123450+ AryLen+1;
{use the Delphi function High(AryUser) to get the limit of the Array
I use the AryLen below to save processor time}
AryUser[AryLen].FirstName := 'User '+IntToStr(AryLen+1);
AryUser[AryLen].MiddleIni := '0';
AryUser[AryLen].LastName := IntToStr(AryLen+1)+' LastName';
AryUser[AryLen].Account := 300+AryLen+1;
AryUser[AryLen].ARect := Rect(43, 127, 234, 382);
AryUser[AryLen].ShoeSize := 4.5+AryLen;

{with AryUser[AryLen] do
  begin
  // the with - - - - do can be helpful
  Number := 123450+ AryLen+1;
  FirstName := 'User '+IntToStr(AryLen+1);
  MiddleIni := '0';
  LastName := IntToStr(AryLen+1)+' LastName';
  Account := 300+AryLen+1;
  ARect := Rect(43, 127, 234, 382);
  ShoeSize := 4.5+AryLen;
  end;}
Label2.Caption := AryUser[AryLen].FirstName;
end;


procedure TForm1.sbut_Save2RegClick(Sender: TObject);
var
Reg1: TRegistry;
UserData1: TUserData;
i: Integer;
begin
{UserData1.Number := 876121;
UserData1.FirstName := 'Mikey';
UserData1.MiddleIni := 'G';
UserData1.LastName := 'MacFerson, Levy';
UserData1.Account := 321;
UserData1.ARect := Rect(43, 127, 234, 382);
UserData1.ShoeSize := 11.5;}
if Length(AryUser) < 1 then Exit;
Reg1 := TRegistry.Create;
try
  Reg1.RootKey := HKEY_CURRENT_USER;
  if Reg1.OpenKey('\Software\Tbear2u', True) then
    begin
    Reg1.WriteInteger('ArrayHigh', High(AryUser));
    {you need to put the array number ArrayHigh in the registry}
    for i := 0 to High(AryUser) do
    Reg1.WriteBinaryData('user'+IntToStr(i),AryUser[i], SizeOf(TUserData));
      //Reg1.WriteBinaryData('user1',UserData1, SizeOf(UserData1));
      Reg1.CloseKey;
    end else ShowMessage('No GO');
  finally
    Reg1.Free;
  end;
Label2.Caption := 'Number is '+IntToStr(AryUser[0].Number);
end;

procedure TForm1.sbut_ReadRegClick(Sender: TObject);
var
Reg1: TRegistry;
UserData1: TUserData;
i, High: Integer;
begin
Reg1 := TRegistry.Create;
try
  Reg1.RootKey := HKEY_CURRENT_USER;
  if Reg1.OpenKey('\Software\Tbear2u', False) then
    begin
    if not (Reg1.ValueExists('ArrayHigh') and Reg1.ValueExists('user0')) then
      begin
      {test for the values otherwize you'll throw an exeption}
      ShowMessage('Registry Info does not exist');
      Exit;
      end;
    High := Reg1.ReadInteger('ArrayHigh');
    {get the number of users in High}
    SetLength(AryUser, High+1);
    for i := 0 to High do
    Reg1.ReadBinaryData('user'+IntToStr(i),AryUser[i], SizeOf(TUserData));;
    Reg1.CloseKey;
    end else ShowMessage('No GO');
  finally
    Reg1.Free;
  end;
Label2.Caption := 'Last name is '+AryUser[High].LastName;
{if UserData1.Number = 876121 then
ShowMessage('Name is '+UserData1.FirstName+' '+UserData1.MiddleIni+'. '+UserData1.LastName+' ShoeSize is '+FloatToStr(UserData1.ShoeSize));}
end;


procedure TForm1.button_SaveFileClick(Sender: TObject);
var
UserFile: File of TUserData;
i: Integer;
begin
{Delphi has a very useful method using File of Type
with a File of Type, the file is automaticly segmented
into the individual Records of the Type}
if Length(AryUser) < 1 then Exit;
SaveDialog1.Filter := 'UDF|*.udf|AllFiles|*.*';
SaveDialog1.FilterIndex := 1;
if SaveDialog1.Execute then
  begin
  AssignFile(UserFile, SaveDialog1.FileName);
  {these are standard Delphi File handling routines}
  Rewrite(UserFile);
  for i := 0 to High(AryUser) do
  Write(UserFile, AryUser[i]);
  {you do not need to specify any byte length for the read
  it will use the size of the record of the File Type}
  CloseFile(UserFile);
  end;
end;

procedure TForm1.button_LoadUsersClick(Sender: TObject);
var
UserFile: File of TUserData;
i, Len: Integer;
begin
OpenDialog1.Filter := 'UDF|*.udf';
OpenDialog1.FilterIndex := 0;
if OpenDialog1.Execute then
  begin
  AssignFile(UserFile, OpenDialog1.FileName);
  Reset(UserFile);
  Len := FileSize(UserFile);
  if Len < 1 then Exit;
  SetLength(AryUser, Len);
  for i := 0 to Len-1 do
  Read(UserFile, AryUser[i]);
  CloseFile(UserFile);
  Label2.Caption:= AryUser[Len-1].LastName;
  end;
end;

- - - - - - - - - - - - - - - -
ask if you need more

 

by: bubblesPosted on 2002-08-30 at 19:54:23ID: 7252828

Hi,

I have tried your solution just today and it is working well - your code is really helpful and I have read it and I am now applying it to my program. One problem though - how can I delete a record from the array once it is created. For example, say I wanted to delete user number three from AryUser. How can I do this, or do I have to re-write the whole array?

I would appreciate your help on this. Also, if I want to use the array in a number of forms, do I have to declare it publicly in say Form1 and then reference it by Form1.AryUser in every other form, or is there a way to make it global to everything?

Thanks.

 

by: Slick812Posted on 2002-08-30 at 21:24:40ID: 7252904

when you use arrays the memory blocks for each member is not removable except if you rewrite the array to remove the record and the shorten the length of the array. . . like this


procedure TfrmAlbum.RemoveCell(ItemNum: Word);
var
Alength, i: Integer;
begin
Alength := Length(ThisAlb); // Array ThisAlb

if (Alength < 1) or (ItemNum > Alength-1) then Exit;
{check to make sure you are in range of array}
Dec(Alength);
{Dec(ALength) will give you the High of the array}
if ItemNum <> Alength then
  for i := ItemNum to Alength-1 do
    ThisAlb[i] := ThisAlb[i+1];

SetLength(ThisAlb, Alength);
end;

- - - - - - - - - - - - - - - - - - - - - - -  

Using a Public Variable in Form1 is a good way to do it and then add the Form1.pas to the uses clause of the other forms.

 

by: bubblesPosted on 2002-08-31 at 00:25:24ID: 7253088

Thanks, you have been most helpful. I have not tried your suggestion yet but I am sure it will work. Thanks again.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...