{ Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters. }
function UrlDecode(const AStr: AnsiString): AnsiString; var Sp, Rp, Cp: PAnsiChar; s: AnsiString; begin SetLength(Result, Length(AStr)); Sp := PAnsiChar(AStr); Rp := PAnsiChar(Result); Cp := Sp; while Sp^ <> #0 do begin case Sp^ of '+': Rp^ := ' '; '%': begin Inc(Sp); if Sp^ = '%' then Rp^ := '%' else begin Cp := Sp; Inc(Sp); if (Cp^ <> #0) and (Sp^ <> #0) then begin s := AnsiChar('$') + Cp^ + Sp^; Rp^ := AnsiChar(StrToInt(string(s))); end; end; Cp := Cp; end; else Rp^ := Sp^; end; Inc(Rp); Inc(Sp); end; SetLength(Result, Rp - PAnsiChar(Result)); end;
function NetHTTPClient(const aURL: PAnsiChar; UTF8: Boolean): PAnsiChar; var vHttp: TNetHTTPClient; vUTF8, vGBK: TStringStream; begin Result := ''; vHttp := TNetHTTPClient.Create(nil); vUTF8 := TStringStream.Create('', TEncoding.GetEncoding(CP_UTF8)); vGBK := TStringStream.Create('', TEncoding.GetEncoding(936)); try with vHttp do begin if UTF8 then vUTF8.Clear else vGBK.Clear; ConnectionTimeout := 2000; // 2秒 ResponseTimeout := 10000; // 10秒 AcceptCharSet := IfThen(UTF8, 'utf-8', 'gbk'); AcceptEncoding := IfThen(UTF8, '65001', '936'); AcceptLanguage := 'zh-CN'; ContentType := 'text/html'; UserAgent := 'Embarcadero URI Client/1.0'; try if UTF8 then begin //D2007传参数的话,这里aURL为nil,好像没有接受到值 //改成固定的网址,D2007里调用能显示正确的响应字符 Get('http://www.taobao.com', vUTF8); Result := PAnsiChar(AnsiString(TNetEncoding.URL.UrlDecode(vUTF8.DataString))); end else begin Get(string(AnsiString(aURL)), vGBK); Result := PAnsiChar(UrlDecode(AnsiString(vGBK.DataString))); end;
except on E: Exception do Result := PAnsiChar(AnsiString(E.Message)) end; end; finally vUTF8.Free; vGBK.Free; vHttp.Free; end;
type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
implementation
function NetHTTPClient(const aURL: PAnsiChar; UTF8: Boolean): PAnsiChar; stdcall; external 'NetHTTP.dll';
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage( NetHTTPClient(PAnsiChar('http://www.taobao.com/'), True)); end;
end.
d2007里执行后,显示响应字符后,程序就失去响应。 调试环境下报: ---------- Debugger Fault Notification ---------- Project C:\Users\lenovo\Documents\Embarcadero\Studio\Projects\NetHTTPDLL\Win32\Debug\Project1.exe raised too many consecutive exceptions: 'access violation at 0x00000000: read of address 0x00000000'. Process Stopped. Use Step or Run to continue. ---------- OK Help ----------
----------------------------------------------
我打的是酱油,而不是别的什么油。 我灌的是口水,而不是别的什么水。 我聊的折腾不是那个不折腾的折腾。 我说的阿娇不是那个邓玉娇的阿娇。 3个代表,6个为什么,9个肠胃炎。 D性强的领导干部都不喜欢热比娅。 我特别要讲的是,屁民网黄色论坛是我经常上网必选的 网站之一
DLL 里的英文,应该也是可以用PChar的意思吧。 To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters.
----------------------------------------------
我打的是酱油,而不是别的什么油。 我灌的是口水,而不是别的什么水。 我聊的折腾不是那个不折腾的折腾。 我说的阿娇不是那个邓玉娇的阿娇。 3个代表,6个为什么,9个肠胃炎。 D性强的领导干部都不喜欢热比娅。 我特别要讲的是,屁民网黄色论坛是我经常上网必选的 网站之一