Delphi中的Maxmind geoip查询

asg*_*012 2 delphi geoip

我正在寻找geoip数据库(城市,国家,组织)查询一堆IP地址.我查看了http://www.maxmind.com/download/geoip/api/pascal/Sample.pas并对其进行了修改:

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;
Run Code Online (Sandbox Code Playgroud)

但我对超过50'000个查询没有任何结果.我知道在使用csv时必须操作地址,但我有二进制db版本.我错过了什么?

谢谢!

TLa*_*ama 5

您遇到了众所周知的ANSI/Unicode不匹配问题.您正在使用Unicode版本的Delphi(版本2009+),并且the unit在发布Unicode版本的Delphi之前已经过时了.

在2009年以下的Delphi(非Unicode)中,类似stringPChar映射到这些类型的ANSI版本,而从Delphi 2009到Unicode版本.

质量替代:

要修复此GeoIP.pas单元,首先,替换所有出现的:

 PChar  -> PAnsiChar
 string -> AnsiString
Run Code Online (Sandbox Code Playgroud)

2.小​​改变:

完成替换后,将第AnsiString93行上的类型更改回string类型:

 92  public
 93    constructor Create(const FileName: AnsiString); // <- string
 94  ...
Run Code Online (Sandbox Code Playgroud)

第138行也是如此:

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