帮忙看看这个用delphi编写局域网发送信息的问题,感谢 ( 积分: 100 )

  • 主题发起人 主题发起人 quietsky
  • 开始时间 开始时间
Q

quietsky

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手,小弟刚学delphi,有个小程序搞不懂,编译报错请,高手指点.
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
NET_API_STATUS = LongInt;
function netmessagebuffersend(servername:pwidechar;toname:pwidechar;fromname:pwidechar;buf:pwidechar;var buflen:integer):integer;stdcall; external 'netapi32.dll' name 'netmessagebuffersend';
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Label3: TLabel;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

Form1=TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
edit1.Clear;
edit2.Clear;
edit3.Clear;
end;

procedure TForm1.Button2Click(Sender: TObject);
const
max_length=1025;
var
string1,string2,string3:string;
pstring1,pstring2:array[0..28] of widechar;
pstring3:array[0..1024] of widechar;
i,j,length:integer;
begin
string1:=edit1.Text;
string2:=edit2.Text;
string3:=edit3.Text;
for i:=0 to 28 do
begin
pstring1:=#0;
stringtowidechar(string1,pstring1,length);
pstring2:=#0;
stringtowidechar(string2,pstring2,length);
pstring3:=#0;
stringtowidechar(string3,pstring3,length);
end;

j:=netmessagebuffersend(nil,pstring2,pstring1,pstring3,max_length);

end;

end.

报错如下:
[Error] Unit1.pas(64): Types of actual and formal var parameters must be identical
[Fatal Error] Project2.dpr(5): Could not compile used unit 'Unit1.pas'

谢谢!
 
各位高手,小弟刚学delphi,有个小程序搞不懂,编译报错请,高手指点.
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
NET_API_STATUS = LongInt;
function netmessagebuffersend(servername:pwidechar;toname:pwidechar;fromname:pwidechar;buf:pwidechar;var buflen:integer):integer;stdcall; external 'netapi32.dll' name 'netmessagebuffersend';
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Label3: TLabel;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

Form1=TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
edit1.Clear;
edit2.Clear;
edit3.Clear;
end;

procedure TForm1.Button2Click(Sender: TObject);
const
max_length=1025;
var
string1,string2,string3:string;
pstring1,pstring2:array[0..28] of widechar;
pstring3:array[0..1024] of widechar;
i,j,length:integer;
begin
string1:=edit1.Text;
string2:=edit2.Text;
string3:=edit3.Text;
for i:=0 to 28 do
begin
pstring1:=#0;
stringtowidechar(string1,pstring1,length);
pstring2:=#0;
stringtowidechar(string2,pstring2,length);
pstring3:=#0;
stringtowidechar(string3,pstring3,length);
end;

j:=netmessagebuffersend(nil,pstring2,pstring1,pstring3,max_length);

end;

end.

报错如下:
[Error] Unit1.pas(64): Types of actual and formal var parameters must be identical
[Fatal Error] Project2.dpr(5): Could not compile used unit 'Unit1.pas'

谢谢!
 
晕,类型不匹配
 
类型不匹配
 
我查了帮助也知道是类型不匹配,可是我就不知道该怎么改了,请高手们指点
 
pwidechar只是个指针类型,本身不占有内存,你的类型声明的不对:
pstring1,pstring2:array[0..28] of widechar;
^^^^^^^^^^^
pstring3:array[0..1024] of widechar;
^^^^^^^^^^^^^

还有在for中使用的循环变量必须为局部变量,不能使用全局变量。
 
1、for 循環中的i未聲明類型
2、不知道你是想聲明指針不是聲明數組,兩者混為一談了。
 
非常感谢各位兄弟的关照.经过TYZhang的点化,明白很多
我是想用那个api函数,netmessagebuffersend,所以就需要进行一个转换,讲string类型转换成api函数需要的指针类型pwidechar,问题就是我不知道该怎样转换
 
当我声明数组为pstring1,pstring2:array[0..28] of widechar;时,
这一行j:=netmessagebuffersend(nil,@pstring2,@pstring1,@pstring3,max_length);
就是形参和实参不一致,该怎么改啊
[Error] Unit1.pas(61): Types of actual and formal var parameters must be identical
 
其实array of widechar 就是pwidechar,可以直接使用,不要用@取地址,这有个例子:
procedure tt(p:pwidechar);
begin
showmessage(p);
end;

procedure TForm1.Button3Click(Sender: TObject);
var b:array[0..100] of widechar;
s:string;
begin
s:='abcde';
fillchar(b,sizeof(b),0);
stringtowidechar(s,b,100);
tt(b);
end;
 
非常感谢TYZhang和各位朋友,谢谢!
 
后退
顶部