向 CreateThread 传递参数
我在调用 CreateThread 时将类引用作为参数传递给 ThreadProc 时遇到问题。这是一个示例程序,演示了我遇到的问题:
program test;
{$APPTYPE CONSOLE}
uses
SysUtils, Windows, Dialogs;
type
TBlah = class
public
fe: Integer;
end;
function ThreadProc(param: Pointer) : DWORD;
begin
ShowMessage(IntToStr(TBlah(param).fe));
Result := 0;
end;
var
tID: DWORD;
handle: THandle;
b: TBlah;
begin
b := TBlah.Create;
b.fe := 54;
handle := CreateThread(nil, 0, @ThreadProc, Pointer(b), 0, tID);
WaitForSingleObject(handle, INFINITE);
end.
对 ShowMessage
的调用会弹出一个消息框,其中包含类似 245729105
的内容,而不是 54< /code> 正如我所料。
这可能只是对 Delphi 工作原理的一个基本误解,所以有人可以告诉我如何让它正常工作吗?
I am having a problem passing a class reference as the parameter to the ThreadProc in a call to CreateThread. Here is a sample program that demonstrates the problem I am having:
program test;
{$APPTYPE CONSOLE}
uses
SysUtils, Windows, Dialogs;
type
TBlah = class
public
fe: Integer;
end;
function ThreadProc(param: Pointer) : DWORD;
begin
ShowMessage(IntToStr(TBlah(param).fe));
Result := 0;
end;
var
tID: DWORD;
handle: THandle;
b: TBlah;
begin
b := TBlah.Create;
b.fe := 54;
handle := CreateThread(nil, 0, @ThreadProc, Pointer(b), 0, tID);
WaitForSingleObject(handle, INFINITE);
end.
The call to ShowMessage
pops up a message box that has something like 245729105
in it, not 54
like I expect.
This is probably just a basic misunderstanding of how Delphi works, so could someone please tell me how to get this working properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里的问题是你的线程函数有错误的调用约定。您需要使用
stdcall
约定来声明它:话虽如此,它更惯用的做法是使用
TThread
后代来为您处理 OOP 到 C 函数返回 OOP 的转换。看起来像这样:顺便说一句,有人知道为什么 Windows.pas 将 TFNThreadStartRoutine 声明为 TFarProc 而不是正确类型化的函数指针吗?
The problem here is that your thread function has the wrong calling convention. You need to declare it with the
stdcall
convention:Having said that, it would be more idiomatic to just use a
TThread
descendant which handles the OOP to C function back to OOP transitioning for you. That would look like this:Incidentally, does anyone know why Windows.pas declares
TFNThreadStartRoutine
asTFarProc
rather than a proper typed function pointer?您忘记了
stdcall
指令。You're forgetting the
stdcall
directive.并且不要使用
Pointer
转换:b
已经是一个Pointer
(一个Class
,它是一个 <代码>对象 <代码>指针)And don't use
Pointer
cast in:b
is already aPointer
(aClass
, which is anObject
Pointer
)