Advertisement
TLama

Untitled

Aug 14th, 2014
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.85 KB | None | 0 0
  1. type
  2.   TEnumData = record
  3.     GeoCode: string;
  4.     GeoName: string;
  5.     Success: Boolean;
  6.   end;
  7.  
  8.   GEOID = type LONG;
  9.   GEOTYPE = type DWORD;
  10.   GEOCLASS = type DWORD;
  11.  
  12.   SYSGEOTYPE = (
  13.     GEO_NATION = $0001,
  14.     GEO_LATITUDE = $0002,
  15.     GEO_LONGITUDE = $0003,
  16.     GEO_ISO2 = $0004,
  17.     GEO_ISO3 = $0005,
  18.     GEO_RFC1766 = $0006,
  19.     GEO_LCID = $0007,
  20.     GEO_FRIENDLYNAME= $0008,
  21.     GEO_OFFICIALNAME= $0009,
  22.     GEO_TIMEZONES = $000A,
  23.     GEO_OFFICIALLANGUAGES = $000B,
  24.     GEO_ISO_UN_NUMBER = $000C,
  25.     GEO_PARENT = $000D
  26.   );
  27.  
  28.   SYSGEOCLASS = (
  29.     GEOCLASS_NATION = 16,
  30.     GEOCLASS_REGION = 14,
  31.     GEOCLASS_ALL = 0
  32.   );
  33.  
  34.   GEO_ENUMPROC = function(GeoId: GEOID): BOOL; stdcall;
  35.  
  36.   function EnumSystemGeoID(GeoClass: GEOCLASS;
  37.     ParentGeoId: GEOID; lpGeoEnumProc: GEO_ENUMPROC): BOOL; stdcall;
  38.     external kernel32 name 'EnumSystemGeoID';
  39.   function GetGeoInfo(Location: GEOID; GeoType: GEOTYPE;
  40.     lpGeoData: LPTSTR; cchData: Integer; LangId: LANGID): Integer; stdcall;
  41.     external kernel32 name {$IFDEF UNICODE}'GetGeoInfoW'{$ELSE}'GetGeoInfoA'{$ENDIF};
  42.  
  43. implementation
  44.  
  45. var
  46.   // I have used this global variable due to a lack of user data parameter for the callback function
  47.   EnumData: TEnumData;
  48.  
  49. function TryGetGeoInfo(GeoId: GEOID; GeoType: GEOTYPE; out Value: string): Boolean;
  50. var
  51.   Buffer: string;
  52.   BufferLen: Integer;
  53. begin
  54.   Result := False;
  55.   BufferLen := GetGeoInfo(GeoId, GeoType, LPTSTR(Buffer), 0, 0);
  56.   if BufferLen <> 0 then
  57.   begin
  58.     SetLength(Buffer, BufferLen);
  59.     Result := GetGeoInfo(GeoId, GeoType, LPTSTR(Buffer), BufferLen, 0) <> 0;
  60.     if Result then
  61.       Value := Trim(Buffer);
  62.   end;
  63. end;
  64.  
  65. function EnumGeoInfoProc(GeoId: GEOID): BOOL; stdcall;
  66. var
  67.   S: string;
  68. begin
  69.   Result := TryGetGeoInfo(GeoId, GEOTYPE(GEO_ISO2), S);
  70.   if Result and (S = EnumData.GeoCode) then
  71.   begin
  72.     // stop the enumeration since we've found the country by its ISO code
  73.     Result := False;
  74.     // return the success flag and try to return the friendly name of the country to the
  75.     // EnumData.GeoName record field
  76.     EnumData.Success := TryGetGeoInfo(GeoId, GEOTYPE(GEO_FRIENDLYNAME), EnumData.GeoName);
  77.   end;
  78. end;
  79.  
  80. function TryGetCountryNameByISO2(const Code: string; out Name: string): Boolean;
  81. begin
  82.   // here is the brainless part using global record variable (because the function used
  83.   // here with its callback does not support passing user data); no, you cannot tune it
  84.   // up by making the callback function nested
  85.   EnumData.GeoCode := Code;
  86.   EnumData.Success := False;
  87.  
  88.   if not EnumSystemGeoID(GEOCLASS(GEOCLASS_NATION), 0, EnumGeoInfoProc) then
  89.     RaiseLastOSError;
  90.  
  91.   Result := EnumData.Success;
  92.   if Result then
  93.     Name := EnumData.GeoName;
  94. end;
  95.  
  96. // and a possible usage
  97. var
  98.   S: string;
  99. begin
  100.   if TryGetCountryNameByISO2('DE', S) then
  101.     ShowMessage(S);
  102. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement