Solved

Programmatically getting a list of countries from Windows into a TStringList

Posted on 2002-03-08
6
364 Views
Last Modified: 2010-04-05
Hi,

I would like a Delphi function that would read a list of countries from the Windows settings (all of the available countries) and put them into a TStringList.

e.g. function GetCountriesFromWindows: TStringList;

Do you have the code that would do this?

Thanks in Advance,

Richard
0
Comment
Question by:Richard2000
6 Comments
 
LVL 8

Accepted Solution

by:
TOndrej earned 50 total points
ID: 6851680
function GetLocaleDataW(ID: LCID; Flag: DWORD): string;
var
  Buffer: array[0..1023] of WideChar;
begin
  Buffer[0] := #0;
  GetLocaleInfoW(ID, Flag, Buffer, SizeOf(Buffer) div 2);
  Result := Buffer;
end;

function GetLocaleDataA(ID: LCID; Flag: DWORD): string;
var
  Buffer: array[0..1023] of Char;
begin
  Buffer[0] := #0;
  SetString(Result, Buffer, GetLocaleInfoA(ID, Flag, Buffer, SizeOf(Buffer)) - 1);
end;

var
  _List: TStringList;

function EnumLocalesProc(LocaleID: PChar): Integer; stdcall;
var
  ID: Cardinal;
  CountryName: string;
  GetLocaleDataProc: function (ID: LCID; Flag: DWORD): string;
begin
  Result := 1;
  ID := StrToInt('$' + LocaleID);
  if Win32Platform = VER_PLATFORM_WIN32_NT then
    GetLocaleDataProc := @GetLocaleDataW
  else
    GetLocaleDataProc := @GetLocaleDataA;
  CountryName := GetLocaleDataProc(ID, LOCALE_SENGCOUNTRY);
  _List.Add(CountryName);
end;

function GetCountriesFromWindows: TStringList;
begin
  Result := TStringList.Create;
  try
    Result.Sorted := True;
    Result.Duplicates := dupIgnore;
    _List := Result;
    if not EnumSystemLocales(@EnumLocalesProc, LCID_SUPPORTED) then
      RaiseLastOSError;
  except
    Result.Free;
    raise;
  end;
end;

// example usage:

procedure TForm1.Button1Click(Sender: TObject);
var
  Countries: TStringList;
begin
  Countries := GetCountriesFromWindows;
  try
    ListBox1.Items.Assign(Countries);
  finally
    Countries.Free;
  end;
end;

HTH
TOndrej
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 6852084
Listening ;-)
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 6852345
listening
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Richard2000
ID: 6854751
Hi TOndrej,

Many thanks for the code, here are the points!  I've tried it on my setup (Windows '98 SE) and it correctly retrieved the list of countries.  The only problem I had was that I got an undeclared identifier error with "RaiseLastOSError".  Do I need to add something to the uses clause so that the compiler recognises it correctly?

Richard
0
 
LVL 8

Expert Comment

by:TOndrej
ID: 6854804
Thanks for the points.
RaiseLastOSError is a new procedure in Delphi 6 (introduced due to cross-platform code for Windows and Linux).
If you're using earlier version of Delphi, simply use RaiseLastWin32Error instead.
In either case, you'll need to include SysUtils in your uses clause.
0
 

Author Comment

by:Richard2000
ID: 6858262
Hi,

Many thanks for your comment.  I'm using Delphi 4 and RaiseLastWin32Error works.

Richard
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now