asg2012
asg2012

Reputation: 335

Maxmind geoip query in delphi

I'm looking to query the geoip database (city, country, org) for a bunch of IP addresses. I looked at http://www.maxmind.com/download/geoip/api/pascal/Sample.pas and modified it:

function LookupCountry(IPAddr: string) : string;
var
   GeoIP: TGeoIP;
   GeoIPCountry: TGeoIPCountry;
begin
  GeoIP := TGeoIP.Create('C:\Users\Albert\Documents\RAD Studio\Projects\Parser\geoip\GeoIP.dat');
  try
    if GeoIP.GetCountry(IPAddr, GeoIPCountry) = GEOIP_SUCCESS then
    begin
      Result := GeoIPCountry.CountryName;
    end
    else
    begin
      Result := IPAddr;
    end;
  finally
    GeoIP.Free;
  end;
end;

but I get no results on over 50'000 queries. I know that the address has to be manipulated when working with csv, but I have the binary db version. What am I missing?

Thanks!

Upvotes: 4

Views: 1625

Answers (1)

TLama
TLama

Reputation: 76713

You've encountered well known ANSI/Unicode mismatch problem. You're using Unicode version of Delphi (version 2009+) and the unit that is dated before the Unicode version of Delphi was released.

In Delphi below 2009 (non Unicode) were types like string or PChar mapped to the ANSI versions of these types whilst since Delphi 2009 to Unicode ones.

1. mass replace:

To fix this GeoIP.pas unit, as first, replace all occurences of:

 PChar  -> PAnsiChar
 string -> AnsiString

2. minor change:

After you're done with the replace, change the AnsiString type on line 93 back to the string type:

 92  public
 93    constructor Create(const FileName: AnsiString); // <- string
 94  ...

And the same do also on line 138:

138  constructor TGeoIP.Create(const FileName: AnsiString); // <- string
139  begin
140    inherited Create;

Upvotes: 6

Related Questions