新建空白工程,粘贴代码即可。procedure updatez不带参数时,可正常运行。unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure updatez(text:string);
begin
  showmessage(text);
end;procedure TForm1.FormCreate(Sender: TObject);
var
hthread:thandle;
threadid:dword;
Param: Integer;
begin
hthread:=createthread(nil,0,@updatez('hello'),@Param,0,threadid);
end;end.

解决方案 »

  1.   

    帮助里说的很清楚,只能用没有参数的函数lpStartAddressThe starting address of the new thread. This is typically the address of a function declared with the WINAPI calling convention that accepts a single 32-bit pointer as an argument and returns a 32-bit exit code. Its prototype is:DWORD WINAPI ThreadFunc( LPVOID );
      

  2.   

    hthread:=createthread(nil,0,@updatez, @Param,0,threadid);参数是在 @Param 这里传进去的
      

  3.   

    是这么传吗? 还是没传进去。unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure updatez(text:string);
    begin
      showmessage(text);
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
    hthread:thandle;
    threadid:dword;
    Param: string;
    begin
    Param := 'hello';
    hthread:=createthread(nil,0,@updatez,@Param,0,threadid);
    end;end.
      

  4.   

    是在createthread里传进的,但不知道为什么取不到
    这是MSDN上的一段:#include <windows.h>
    #include <conio.h>DWORD WINAPI ThreadFunc( LPVOID lpParam ) 

        char szMsg[80];    wsprintf( szMsg, "Parameter = %d.", *(DWORD*)lpParam ); 
        MessageBox( NULL, szMsg, "ThreadFunc", MB_OK );    return 0; 

     
    VOID main( VOID ) 

        DWORD dwThreadId, dwThrdParam = 1; 
        HANDLE hThread; 
        char szMsg[80];    hThread = CreateThread( 
            NULL,                        // default security attributes 
            0,                           // use default stack size  
            ThreadFunc,                  // thread function 
            &dwThrdParam,                // argument to thread function 
            0,                           // use default creation flags 
            &dwThreadId);                // returns the thread identifier 
     
       // Check the return value for success. 
     
       if (hThread == NULL) 
       {
          wsprintf( szMsg, "CreateThread failed." ); 
          MessageBox( NULL, szMsg, "main", MB_OK );
       }
       else 
       {
          _getch();
          CloseHandle( hThread );
       }
    }
      

  5.   

    我把上面代码翻译成了Delphi,但是发现得不到lpParam的值
    哪位老大进来看看function ThreadFunc(lpParam: Pointer): Integer;
    var
       pi : ^Dword;
       szMsg :array [0..80] of char;
    begin
      pi := lpParam;//pi 为nil  wsprintf(szMsg,pChar(Format('Parameter = %d.',[pi^])));
      MessageBox(0,PChar(floatToStr(pi^)),'ThreadFunc',MB_OK);
      Result :=0;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      dwThreadId,dwThrdParam : DWORD;
      hThread : THandle;
      szMsg : array [0..80] of char;begin
        dwThrdParam:=2;
        hThread := CreateThread(0,0,@ThreadFunc,@dwThrdParam,0,dwThreadId);
        if hThread = 0 then
        begin
          wsprintf( szMsg, 'CreateThread failed.' );
          MessageBox( 0, szMsg, 'main;', MB_OK );
        end
        else
        begin
          CloseHandle( hThread );
       end;end;
      

  6.   

    有几个地方得改
    1 msdn里的 _getch();
       这里实际是等按用户按键盘。 没有了这一步,线程未执行则此点击过程已经结束,dwThrdParam失效,结果难以预料;还有,没执行就关闭线程的Handle,不知道会造成什么影响
    2 szMsg直接用string 类型变量代替就行了,要不看着就麻烦3.还有一个关键地方,呵呵
      等我试验完再说,要不万一不对多丢面子
      

  7.   

    avenir说得第一点还有些道理,
    是有可能会执行不到线程函数,加上sleep之类的
    线程没执行就关闭线程的Handle对线程没有什么问题,
    只要后面不用此Handle就可以
    至于说string,这完全是看个人喜好
      

  8.   

    至于说局部变量、堆栈云云,只是提醒一下各位,
    我们传递的是@dwThrdParam,即使dwThrdParam被失效,
    @dwThrdParam这个地址也是存在的,也不会被置空
    现在的问题是,不管用什么变量,在线程函数里,lpParam: Pointer是nil在CloseHandle( hThread );前面加上sleep(10000)
    不知道还会不会招来唾沫
      

  9.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;  function updatez(lp: Pointer): DWord;var
      Form1: TForm1;
      Param: PChar = 'fffffffffffff';
    implementation{$R *.dfm}
    function updatez(lp: Pointer): DWord;
    var
      s: string;
    begin
      s := PChar(lp);
      showmessage(s);
      result := 0;
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      hthread:thandle;
      threadid:dword;
    begin
      hthread := BeginThread(nil, 0, @updatez, Param, 0, threadid);
      sleep(1000);
    end;end.很变态的方法。
      

  10.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    function ThreadFunc(lpParam: Pointer): Integer;
    var
       pi : ^Dword;
       szMsg :array [0..80] of char;
    begin
      pi := lpParam;//pi 为nil  wsprintf(szMsg,pChar(Format('Parameter = %d.',[pi^])));
      MessageBox(0,PChar(floatToStr(pi^)),'ThreadFunc',MB_OK);
      Result :=0;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      dwThreadId,dwThrdParam : DWORD;
      hThread : THandle;
      szMsg : array [0..80] of char;
    begin
        dwThrdParam:=2;
        hThread := BeginThread(nil, 0, @ThreadFunc,@dwThrdParam, 0, dwThreadId);
        if hThread = 0 then
        begin
          wsprintf( szMsg, 'CreateThread failed.' );
          MessageBox( 0, szMsg, 'main;', MB_OK );
        end
        else
        begin
          sleep(1000);
          CloseHandle( hThread );
       end;
    end;
    end.
      

  11.   

    看来还是得用BeginThread,直接用CreateThread很麻烦
    参数和进程函数的传递经过了其他的处理,
    有兴趣的可以看看system.pas里的ThreadWrapper函数偶是一看见汇编就头大
    -----------------------------------------------------------
    BeginThread的实现方法:function BeginThread(SecurityAttributes: Pointer; StackSize: LongWord;
      ThreadFunc: TThreadFunc; Parameter: Pointer; CreationFlags: LongWord;
      var ThreadId: LongWord): Integer;
    var
      P: PThreadRec;
    begin
      New(P);
      P.Func := ThreadFunc;
      P.Parameter := Parameter;
      IsMultiThread := TRUE;
      Result := CreateThread(SecurityAttributes, StackSize, @ThreadWrapper, P,
        CreationFlags, ThreadID);
    end;
      

  12.   

    先用 hexenzhou(甲骨文) 的方法了。谢谢。过两天一定给分。