这种情况可能是i 变量定义在createthread(nil, 0, @test, @i, 0, ID ); 这句代码的过程里了比如procedure proc;var i: integer;begin i:=100; //由于i是局部变量,所以在下边这一句结束后,局部变量的存在期就结束了 //换句话说,传过程后就产生野指针了 createthread(nil, 0, @test, @i, 0, ID ); end;正确的做法就是把 i 变量定义成全部变量如下: unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
i: Integer;implementation{$R dfm}function test (var i:integer): boolean; stdcall;
begin
form1edit1text := inttostr (i);
result:=true;
end;procedure TForm1Button1Click(Sender: TObject);
var
id: cardinal;
begin
i:=12000;
createthread(nil,0,@test,@i,0,id);
end;end
另外还有一点, 使用线程回调函数时一定要加上stdcall,因为默认参数压栈的方式与这个API不兼容会导致错误如果只有一个参数或没有参数,就没有问题在DELPHI使用回调函数时记得在定义处加上stdcall就不会出乱子了
procedure TForm1Button1Click(Sender: TObject);
begin
with adoStoredproc1 do begin
close;
ParametersClear;
ProcedureName := 'charu;1';
ParametersCreateParameter('@stno',ftString,pdInput,25,edit1text);
ParametersCreateParameter('@stname',ftString,pdInput,25,edit2text);
ParametersCreateParameter('@stsex',ftString,pdInput,25,edit3text);
ParametersCreateParameter('@stage',ftfloat,pdInput,0,strtofloat(edit4text));
ParametersCreateParameter('@stdept',ftString,pdInput,25,edit5text);
ExecProc;
end;
end;
end
以上就是关于关于delphi中的 createthread() 函数传递参数的小问题全部的内容,包括:关于delphi中的 createthread() 函数传递参数的小问题、delphi 存储过程 传递输入参数、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)