Solved

Programmatically getting a list of countries from Windows into a TStringList

Posted on 2002-03-08
6
361 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
Comment Utility
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
Comment Utility
Listening ;-)
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
listening
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:Richard2000
Comment Utility
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
Comment Utility
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
Comment Utility
Hi,

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

Richard
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

7 Experts available now in Live!

Get 1:1 Help Now