Solved

Programmatically getting a list of countries from Windows into a TStringList

Posted on 2002-03-08
6
372 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

679 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