在SYSTEM目录下有一个Netbios.dll,用快速查看将
其打开,在导出表部分显示如下:
  导出表:
  序数 入口 名称
  0000 00001a37 NetbiosAddthd
  0001 000019eb NetbiosDelete
  0002 00001a96 NetbiosDelthd
  0003 000019b1 NetbiosInitialize
  0004 0000186b PostRoutineCaller
  0005 0000102e _Netbios
  注意到那个0005号_Netbios
定义
一个NCB(Netbios控制块)记录,将NCB数据结构封装在里面;声明一
个后处理例程以
及消息处理过程,以完成广播数据的接收和发送。
/////////Netbios单元///////////
  unit netbios;
  interface
  uses windows,messages,Forms,SysUtils;
  type
  {$X+}{$A+}
   //声明一个NCB记录指针。
   PNCB=^NCB;
  //声明一个后处理例程的过程类型。
   POST=procedure(var ncbR:PNCB);
   NCB=record
   ncb_command:UCHAR;
   ncb_retcode:UCHAR;
   ncb_lsn:UCHAR;
   ncb_num:UCHAR;
   ncb_buffer:PCHAR;
   ncb_length:WORD;
   ncb_callname:array [1..16] of UCHAR;
   ncb_name:array [1..16] of UCHAR;
   ncb_rto:UCHAR;
   ncb_sto:UCHAR;
   ncb_post:POST;
   ncb_lana_num:UCHAR;
   ncb_cmd_cplt:UCHAR;
   ncb_reserve:array [1..10] of UCHAR;
   ncb_event:HANDLE;
   end;
  function NetbiosSR(ncbX:PNCB):UCHAR;pascal;
  //初始化NCB。
  procedure InitNCB(var ncbY:PNCB);
  //后处理例程,注意使用远指针。
  procedure postrout(var ncbR:PNCB);stdcall;far;
  var
   char_buffer:array[0..511]of UCHAR;
   int_buffer:array[1..512]of Byte;
  implementation
   //调用系统的Netbios。dll中的Netbios函数标号是6。Delphi
搜索外部文件的
顺序是当前目录→系统目录→其他目录,别忘了保证存在
Netbios.dll。
   function NetbiosSR(ncbX:PNCB):UCHAR;external 
‘netbios' index 6;
   procedure InitNCB(var ncbY:PNCB);
   var
   x:integer;
   begin
   ncbY.ncb_command:=0;
   ncbY.ncb_retcode:=0;
   ncbY.ncb_lsn:=0;
   ncbY.ncb_num:=0;
   ncbY.ncb_length:=512; //数据缓冲长度,最大512B。
   for x:=1 to 16 do
   begin
   ncbY.ncb_callname[x]:=0;
   ncbY.ncb_name[x]:=0;
   end;
   ncbY.ncb_rto:=0;
   ncbY.ncb_sto:=0;
   ncbY.ncb_lana_num:=0;
   ncbY.ncb_cmd_cplt:=0;
   for x:=1 to 10 do
   ncbY.ncb_reserve[x]:=0;
   ncbY.ncb_event:=0;
   end;
  
procedure postrout(var ncbR:PNCB);
  begin 
  sendMessage(wnd_BROADCAST,WM_TIMER,0,0);
   end;
  end.
  ////////窗口单元//////////
  unit broadcast;
  interface
  uses
   Windows,Messages,SysUtils,Classes,Graphics,
Controls,Forms,Dialogs,
   netbios;
  type
   Tmain=class(TForm)
   private
   {Private declarations}
   //消息处理过程,注意消息宏要与后处理中的一致。
   procedure post_main(var Message:TMessage);message 
WM_TIMER;
   public
   {Public declarations}
   end;
  var
   main: Tmain;
   ncbname:UCHAR;
   ncbRock:PNCB;
   post_add:POST;
  implementation
  {$R *.DFM}{$A-}{$I-}
  /////////主窗口建立过程/////////
  procedure Tmain.FormCreate(Sender: TObject);
  var
   ret:UCHAR;
   i,x,y:integer;
   p:single;
  begin
   new(ncbRock);
   randomize();i:=0;
   FillChar(char_buffer,sizeof(char_
  buffer),0);
   post_add:=@postrout;
   //取后处理例程的地址。
   ncbRock.ncb_buffer:=@char_buf
  fer; //取数据缓冲区的地址。
   InitNCB(ncbRock);
   ret:=9;
   ncbname:=random(100);
   ncbRock.ncb_name[1]:=ncbna
  me;
   ncbRock.ncb_command:=$30;
   //加名,ret为0加名成功。
   while ((i<10)and(ret<>0)) do
   begin
   ret:=netbiosSR(ncbRock);
   i:=i+1;
   end;
   if ret<>0 then
   begin
   for i:=1 to 20 do
   messagebeep(-1);
   MessageDlg(‘网络通信无法实现!您需要关闭程序重新运行.
',mtWarning,
   [mbOk],0);
   end
   else if ret=0 then
   begin
   ncbRock.ncb_post:=post_add;
   ncbRock.ncb_command:=$a3; //异步接收方式字。
   ncbRock.ncb_event:=0;
   ncbRock.ncb_length:=512;
   ret:=netbiosSR(ncbRock);
   end;
  end;
  ///////////广播消息处理过程/////
  procedure Tmain.post_main(var Message:TMessage);
  var
  x:integer;
  ret:UCHAR;
  begin
   //取出数据缓冲区的内容
   for x:=0 to 511 do
   int_buffer[x+1]:=char_buffer[x];
   ////以下可以进行数据处理////
   //重新打开异步接受。
   ncbRock.ncb_post:=post_add;
   ncbRock.ncb_command:=$a3;
   ncbRock.ncb_event:=0;
   ncbRock.ncb_length:=512;
   ret:=netbiosSR(ncbRock);
  end;
  end.