//主单元
procedure cap_ip1Cap(ip, proto, sourceIP, destIP, SourcePort,
      DestPort: String; header: PChar; header_size: Integer; data: PChar;
      data_size: Integer);procedure zhixing();
begin
  cap_ip1         := Tcap_ip.Create(nil);
  cap_ip1.OnCap   := cap_ip1Cap;///这里编译不过,不知道怎么写了.
end;//引用单元 cap_ip
type
  TOnCap = procedure(ip,proto,sourceIP,destIP,SourcePort,DestPort: string;
                       header:pchar;header_size:integer;data:pchar;data_size:integer) of object;
end;问题来了如果我写到窗体里面就可以编译通过,放到单元里面调用就通不过,为什么呢.不知道谁能帮我改下,只要能在单元里调用就行了,因为程序是没窗口的,直接抓数据放到文本里面存放的。

解决方案 »

  1.   

    type 
      TOnCap = procedure(ip,proto,sourceIP,destIP,SourcePort,DestPort: string; 
                          header:pchar;header_size:integer;data:pchar;data_size:integer) of object;// 注意这个xx of Object; 表明这是一个类方法,都知道,对于类方法的调用,跟普通的方法(静态方法,当然类当中也支持静态方法)调用有一个最大的区别,就是类方法会push一个self指针。而指明为一个of Object的方法,就是要告诉编译器,这一个方法在被调用时候需要额外的传送一个self指针。如果你要放在一个单元里面也可以。写成一个类函数,不过可以定义为class procedure,即静态方法
    type
      TGlobal_Functional = class
        public
          class procedure cap_ip1Cap(ip, proto, sourceIP, destIP, SourcePort, 
          DestPort: String; header: PChar; header_size: Integer; data: PChar; 
          data_size: Integer); 
      end;
    procedure zhixing(); 
    begin 
      cap_ip1        := Tcap_ip.Create(nil); 
      cap_ip1.OnCap  := TGlobal_Functional.cap_ip1Cap;///这里编译不过,不知道怎么写了. 
    end; 
      

  2.   

    编译通不过
    class procedure cap_ip1Cap(ip, proto, sourceIP, destIP, SourcePort, 
          DestPort: String; header: PChar; header_size: Integer; data: PChar; 
          data_size: Integer); 
    这里错误 external declaration:'TGlobal_Functional.cap_ip1Cap'
      

  3.   

    unit cap_main;interface
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      cap_ip, StdCtrls, ExtCtrls;    function check_filter(data: PChar;data_size: Integer):boolean;
       function MidStr(Const Str: String; From, Size: Word): String;
      procedure xzcx();
      Procedure WriteTxt(sFileName,sText:String);type
      Tmy_data=record
        buf:array of char;
    end;
    type
    TGlobal_Functional = class
        public
          class procedure cap_ip1Cap(ip, proto, sourceIP, destIP, SourcePort,
          DestPort: String; header: PChar; header_size: Integer; data: PChar;
          data_size: Integer);
    end;var
      buf_list:array of Tmy_data;
      cap_ip1:Tcap_ip;implementation
    function MidStr(Const Str: String; From, Size: Word): String;
    begin
    MidStr := Copy(Str, From, Size)
    end;Procedure WriteTxt(sFileName,sText:String); //写txt文件
    Var 
    F:TextFile; 
    Begin
    AssignFile(F,sFileName); 
    IF FileExists(sFileName) Then 
    Append(F) 
    Else 
    ReWrite(F);WriteLn(F,sText); 
    CloseFile(F); 
    End; 
    function check_filter(data: PChar;data_size: Integer):boolean;
    var
        i:integer;
        temp_str:string;
    begin
     result:=true;
      setlength(buf_list[0].buf,data_size);
      copymemory(buf_list[0].buf,data,data_size);
      for i:=0 to data_size-1 do
        temp_str:=temp_str+buf_list[0].buf[i];
        temp_str:=AnsiLowerCase(temp_str);
         if pos(AnsiLowerCase(webglks),temp_str)>0 then
          begin
           result:=false; exit;
          end else
          begin
           result:=true; exit;
        end;
     result:=false;
    end;function cap_ip1Cap(ip, proto, sourceIP, destIP, SourcePort,
      DestPort: String; header: PChar; header_size: Integer; data: PChar;
      data_size: Integer): String;
    //省
    end;procedure xzcx();
    begin
     setlength(buf_list,10);
      cap_ip1:=Tcap_ip.Create(nil);
      cap_ip1.OnCap:=TGlobal_Functional.cap_ip1Cap;
    cap_ip1.StartCap;
    end;
    end.
    这是我的代码.
      

  4.   

    waxfdx :
    你发的源程序中还差一些东西。
    如webglks的定义,cap_ip1Cap被你省略了。
    麻烦你补上,或发邮件给我:[email protected]谢谢!!