我正在嘗試查看 IP 地址并在訊息框中顯示這兩個錯誤
錯誤看起來像這樣
[dcc32 錯誤] Unit1.pas(38): E2010 不兼容的型別:'PAnsiChar' 和 'PWideChar' => Error1
和
[dcc32 錯誤] Unit1.pas(40): E2010 不兼容的型別:'string' 和 'array[1..1024] of Byte' => Error2
我可以在 C/C 中做到這一點,但我想在 Delphi 中嘗試一些新的東西
我的源代碼看起來像這樣
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Wininet, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
hInet: HINTERNET;
hFile: HINTERNET;
buffer: array[1..1024] of byte;
bytesRead: DWORD;
url : String;
begin
url := 'https://icanhazip.com';
hInet := InternetOpen(PChar(application.title),INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0);
hFile := InternetOpenUrlA(hInet, PChar(url), nil, 0, 0, 0); //Error 1
InternetReadFile(hFile,@buffer,SizeOf(buffer),bytesRead); // Error 2
showmessage(buffer);
end;
end.
請問我做錯了什么?
編輯
好的,InternetOpenUrl作業了,但是,它仍然給我在顯示訊息(緩沖區)上的錯誤
代碼現在看起來像這樣
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Wininet, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
hInet: HINTERNET;
hFile: HINTERNET;
buffer: array[1..1024] of byte;
bytesRead: DWORD;
url : String;
begin
url := 'https://icanhazip.com';
hInet := InternetOpen(PChar(application.title),INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0);
hFile := InternetOpenUrl(hInet, PChar(url), nil, 0, 0, 0);
InternetReadFile(hFile,@buffer,SizeOf(buffer),bytesRead);
showmessage(buffer);
end;
end.
編輯2
我的代碼現在看起來像這樣
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Wininet, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
hInet: HINTERNET;
hFile: HINTERNET;
buffer: String;
bytesRead: DWORD;
url : String;
szMessage : String;
i : integer;
begin
//url := 'https://icanhazip.com';
hInet := InternetOpen(PChar(application.title),INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0);
hFile := InternetOpenUrl(hInet, PChar('https://icanhazip.com'), nil, 0, 0, 0);
InternetReadFile(hFile,@buffer,SizeOf(buffer),bytesRead);
//showmessage(buffer);
Label1.Caption := buffer;
end;
end.
uj5u.com熱心網友回復:
發生第一個錯誤是因為您呼叫了函式的 AnsiChar 版本。
使用InternetOpenUrl而不是InternetOpenUrlA.
第二個發生在您期望的下面的行中。ShowMessage需要 astring而不是array of byte.
編輯: 要從要傳遞給 ShowMessage 的位元組陣列中獲取字串,您需要知道發送位元組的編碼方式。假設是 UTF-8,轉換可以簡單地完成:
ShowMessage(TEncoding.UTF8.GetString(Slice(buffer, bytesRead)))
編輯 2: 好的,對于 XE8,你必須做更多的作業。
procedure TForm1.Button1Click(Sender: TObject);
var
hInet: HINTERNET;
hFile: HINTERNET;
buffer: TBytes;
bytesRead: DWORD;
url : String;
begin
url := 'https://icanhazip.com';
hInet := InternetOpen(PChar(application.title),INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0);
hFile := InternetOpenUrl(hInet, PChar(url), nil, 0, 0, 0);
SetLength(buffer, 1024);
InternetReadFile(hFile,@buffer[0],Length(buffer),bytesRead);
showmessage(TEncoding.UTF8.GetString(buffer, 0, bytesRead));
end;
uj5u.com熱心網友回復:
在 C/C 中,您通常使用以 NUL 結尾的字串 - 并自己分配存盤來保存該字串。這就是您在代碼中使用 BYTE 陣列所做的事情。
在 Delphi 中,astring是本機型別,而不僅僅是指向 NUL 終止字符資料的指標。
從您提供的錯誤訊息中,錯誤發生在對 ShowMessage 的呼叫上,而不是在對 InternetReadFile 的呼叫上。
您需要將字串變數傳遞給 ShowMessage。
你可以試試這個:
var
szMessage: String;
i: Integer;
SetLength(szMessage, bytesread);
for i =1 to bytesread do
szMessage[i]:=buffer[i-1];
ShowMessage(szMessage);
有一些方法可以更緊湊地執行此操作,但作為最近從 C 轉向主要使用 Delphi 的人,我認為以這種方式查看操作有助于理解兩種語言之間的差異。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446163.html
上一篇:是否有將Delphi11.0與11.1分開的編譯器定義?
下一篇:Delphi動態陣列變數復用
