Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Programmatically getting a list of countries from Windows into a TStringList

Posted on 2002-03-08
6
Medium Priority
?
388 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 200 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
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!

 

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

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!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

598 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