Solved

Programmatically getting a list of countries from Windows into a TStringList

Posted on 2002-03-08
6
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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

Technology Partners: 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!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

729 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