如何在回调函数中使用局部变量?(50分)

  • 主题发起人 主题发起人 doll_paul
  • 开始时间 开始时间
D

doll_paul

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在线程单元!注意,是在线程单元中!!!

例如:

function EnumWndProc(AhWnd: HWnd; AlParam: pointer): Boolean; stdcall;
begin
integer(alparam^) := 1000;
end;

procedure wwww.Execute;
begin
FreeOnTerminate := True;
temp := 1;
EnumWindows(@EnumWndProc, Longint(@temp));
//我要temp变为1000
end;

以上代码有时候会成功,有时候则会出错。另外,小弟按以上思路把TEMP从整型换为记录类
后,会出错地址错,那位老兄可以给出记录类的DEMO,谢谢!!!!
 
> procedure wwww.Execute;
> begin
> FreeOnTerminate := True;
> temp := 1;
> EnumWindows(@EnumWndProc, Longint(@temp));
> //我要temp变为1000
> end;
这里的temp是全局变量吗?
 
是没高手,还是分少,或是。。。。。。
 
是你的问题没有完整,连变量定义都没有,谁知道你的temp是从哪来的?
:)
 
Sorry,我都说了,是局部变量啊!

private
temp: integer;
 
> private
> temp: integer;
------------------------------
不知道我的叫法对不对?
这是类的私有变量,是不是可以叫做局部变量?
 
to wlmmlw ,all,暂且不说我的说法是否正确,我现在只是要在线程中访问类的变量,无论
声明在private,还是publice,在回调函数中,都无法访问!

当然,我如果使用外部声明可以,但这样程序会出问题!

请大家帮帮忙!
 
如果不在线程中可以吗?
 
TO:doll_paul:
在回条函数里可以访问啊
EnumWindows(@EnumWndProc, Longint(@temp));
你可以这样
EnumWindows(@EnumWndProc, Longint(WWWW));
把一个类当参数传进去就可以用了,在函数里这样
with Twwww(LPARAM) do
begin
// 修改具体的值和读数据都行
end;
 
to 张无忌兄,好像没在QQ上见你了~

没错,按我上面的办法是可以修改与读取,但有时候会出错不知道为什么。

但我现在是需要使用多个局部变量,因此我传过去一个记录类型的指针,但我试了几种办法
不是地址非法,就是传入后,经修改,只要从回调函数出去,所有记录的元素值,又回到进
入回调函数前的值了!!!!
 
你写个类进去就可以了,我测试过没有问题的
 
unit Unit2;

interface

uses
Classes, Windows, SysUtils, Messages, Dialogs;

type
wwww = class(TThread)
private
Send_Type: String;
ff: integer;
{ Private declarations }
protected
procedure Execute; override;
public
constructor Create(Task_Type: String);
end;

implementation

uses Unit1;

{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,

Synchronize(UpdateCaption);

and UpdateCaption could look like,

procedure wwww.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }

{ wwww }

constructor wwww.Create(Task_Type: String);
begin
Send_Type := Task_type;
inherited Create(False);
end;

function EnumWndProc(AhWnd: HWnd; AlParam: pointer): Boolean; stdcall;
begin
integer(alparam^) := 3000;
end;

procedure wwww.Execute;
var
i:integer;
temp : ^integer;

begin
FreeOnTerminate := True;

i :=100;
temp := @i;
EnumWindows(@EnumWndProc, Longint(@temp));
showmessage(inttostr(temp^));
end;


end.
 
多人接受答案了。
 
后退
顶部