home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / wint1_92 / dpmi / wnetbios.pas < prev   
Pascal/Delphi Source File  |  1991-12-23  |  8KB  |  292 lines

  1. WNETBIOS.PAS - Source to a DLL implementing the NetBIOS routines using DPMI
  2. {*********************************************************}
  3. {*                 WNETBIOS.PAS 1.00                     *}
  4. {*        Copyright (c) TurboPower Software 1991.        *}
  5. {*                 All rights reserved.                  *}
  6. {*********************************************************}
  7. {$C Fixed}
  8. {$S-,R-,I-,V-}
  9.  
  10. Library WNetBios;
  11.  
  12. uses
  13.   WinDos, WinProcs, WinTypes, WinDPMI, TNetBIOS;
  14.  
  15. function AllocateWinNCB(var N : WinNCB) : Boolean; Export;
  16.   {-Allocates memory for an NCB and maintains a real and protected mode ptr}
  17. var
  18.   L : LongInt;
  19. begin
  20.   L := GlobalDosAlloc(SizeOf(NCB));
  21.   AllocateWinNCB := L <> 0;
  22.   with N do begin
  23.      NR := Ptr(DoubleWord(L).HiWord, 0);
  24.      NP := Ptr(DoubleWord(L).LoWord, 0);
  25.   end;
  26. end;
  27.  
  28. procedure FreeWinNCB(N : WinNCB); Export;
  29.   {-Frees a WinNCB previously allocated with AllocateWinNCB}
  30. begin
  31.   GlobalDosFree(SegOfs(N.NP).Segm);
  32. end;
  33.  
  34. procedure ClearNCB(var N : NCB);
  35.  
  36. begin
  37.   FillChar(N,SizeOf(NCB),$00);     {fill the NCB with 0}
  38. end;
  39.  
  40. procedure NetBIOSRequest(N : NCBPtr);
  41. var
  42.   Regs : DPMIRegisters;
  43. begin
  44.   FillChar(Regs, SizeOf(Regs), 0);
  45.   with Regs do begin
  46.     drES := SegOfs(N).Segm;
  47.     drBX := SegOfs(N).Ofst;
  48.     drFlags := GetCPUFlags;
  49.   end;
  50.   SimulateRealModeInt($5C, 0, Regs);
  51. end;
  52.  
  53. procedure SendDataGram(var N : WinNCB; SenderNameNum : Byte;
  54.                        ReceiverName : NBNameStr;
  55.                        Wait : Boolean;
  56.                        var PostEvent : WindowsPostType;
  57.                        DGSize : Word;
  58.                        Datagram : Pointer); Export;
  59. {Send a data packet via datagram services}
  60.  
  61. begin
  62.   ClearNCB(N.NP^);
  63.   {build the NCB}
  64.   with N.NP^ do begin
  65.     Command := $20;
  66.     if not Wait then begin
  67.       Command := Command + NoWait;
  68.       PostRoutine := PostEvent.Callback;           {set the post event routine}
  69.       PostEvent.NCBs := @N;
  70.     end;
  71.     Buffer  := DataGram;          {point Buffer at the data to send}
  72.     BufLen  := DGSize;            {set the size of the data}
  73.     NameNum := SenderNameNum;     {the name number to associate with}
  74.     LanaNum := 0;
  75.     Move(ReceiverName[1],CallName,Length(ReceiverName)); {the receiver}
  76.   end;
  77.   NetBiosRequest(N.NR);              {send it off}
  78. end;
  79.  
  80. procedure ReceiveDataGram(var N : WinNCB;
  81.                           ReceiverNameNum : Byte;
  82.                           Wait : Boolean;
  83.                           var PostEvent : WindowsPostType;
  84.                           DGSize : Word;
  85.                           Datagram : Pointer); Export;
  86. {receive a data packet via datagram services. If Wait is TRUE, then this
  87.  routine will wait until a datagram is received before returning. Otherwise,
  88.  it waits for the datagram in the background. The CmdCompleted field of the
  89.  NCB reports when the event is complete if Wait is FALSE.
  90. }
  91. begin
  92.   ClearNCB(N.NP^);
  93.   {build the NCB}
  94.   with N.NP^ do begin
  95.     Command := $21;
  96.     if not Wait then begin
  97.       Command := Command + NoWait;  {return immediately-do not wait for msg}
  98.       PostRoutine := PostEvent.Callback;           {set the post event routine}
  99.       PostEvent.NCBs := @N;
  100.     end;
  101.     Buffer  := DataGram;           {point buffer at data receive buffer}
  102.     BufLen  := DGSize;
  103.     NameNum := ReceiverNameNum;
  104.   end;
  105.   NetBiosRequest(N.NR);
  106. end;
  107.  
  108. function CancelRequest(var N : WinNCB) : Byte; Export;
  109.   {-cancels a pending NetBIOS function request}
  110. var
  111.   CancelNCB        : WinNCB;
  112. begin
  113.   if not AllocateWinNCB(CancelNCB) then begin
  114.     CancelRequest := NBEDPMIError;
  115.     Exit;
  116.   end;
  117.   ClearNCB(CancelNCB.NP^);
  118.   with CancelNCB.NP^ do begin
  119.     Buffer    := N.NR;
  120.     Command   := $35;
  121.     LanaNum   := 0;
  122.   end;
  123.  
  124.   NetBiosRequest(CancelNCB.NR);
  125.  
  126.   with CancelNCB.NP^ do
  127.     CancelRequest := RetCode;
  128.   FreeWinNCB(CancelNCB);
  129. end;
  130.  
  131. function NetBiosAddName(NameToAdd : NBNameStr;
  132.                         var NameNumber : Byte) : Byte; Export;
  133.   {-Adds a NetBIOS unique name to the name table}
  134. var
  135.   NameNCB : WinNCB;
  136. begin
  137.   if not AllocateWinNCB(NameNCB) then begin
  138.     NetBIOSAddName := NBEDPMIError;
  139.     Exit;
  140.   end;
  141.   ClearNCB(NameNCB.NP^);
  142.   {build the NCB}
  143.   with NameNCB.NP^ do begin
  144.     Command := $30;
  145.     Move(NameToAdd[1],Name,Length(NameToAdd));
  146.     LanaNum := 0;
  147.   end;
  148.   NetBiosRequest(NameNCB.NR);
  149.   with NameNCB.NP^ do begin
  150.     NetBiosAddName := RetCode;
  151.     NameNumber := NameNum;
  152.   end;
  153.   FreeWinNCB(NameNCB);
  154. end;
  155.  
  156. function NetBiosDeleteName(NameToDelete : NBNameStr) : Byte; Export;
  157.   {-deletes a name or group name from the NetBIOS name table}
  158. var
  159.   NameNCB : WinNCB;
  160. begin
  161.   if not AllocateWinNCB(NameNCB) then begin
  162.     NetBIOSDeleteName := NBEDPMIError;
  163.     Exit;
  164.   end;
  165.   ClearNCB(NameNCB.NP^);
  166.   {build the NCB}
  167.   with NameNCB.NP^ do begin
  168.     Command := $31;
  169.     Move(NameToDelete[1],Name,Length(NameToDelete));
  170.     LanaNum := 0;
  171.   end;
  172.   NetBiosRequest(NameNCB.NR);
  173.   with NameNCB.NP^ do
  174.     NetBiosDeleteName := RetCode;
  175.   FreeWinNCB(NameNCB);
  176. end;
  177.  
  178. function NetBIOSIntVecInstalled : Boolean;
  179. {Determine whether an int 5Ch vector is installed (that does not point to
  180.  the BIOS).
  181. }
  182. var
  183.   Vec              : Pointer;
  184.   S                : Word;
  185. begin
  186.   GetRealModeIntVector(NetBiosInt5C,Vec);  {get the int 5Ch vector}
  187.   S := SegOfs(Vec).Segm;                   {isolate the segment portion}
  188.   if (S = $0000) or (S = $F000) then
  189.     NetBIOSIntVecInstalled := FALSE
  190.   else
  191.     NetBIOSIntVecInstalled := TRUE; {pointed somewhere other than NIL or BIOS}
  192. end;
  193.  
  194. function DoBogusNetBIOSCall : Boolean;
  195. {issues an invalid NetBIOS call.   If such a call is made, and the installed
  196.  int 5Ch handler returns the NetBIOS invalid command error code, then NetBIOS
  197.  or a NetBIOS emulator is installed.
  198. }
  199. const
  200.   ErrorInvalidCommand = $03;
  201.   NBInvalidCommand    = $7F;
  202.   NBEInvalidCommand   = $03;
  203. var
  204.   PresenceNCB      : WinNCB;
  205. begin
  206.   if not AllocateWinNCB(PresenceNCB) then begin
  207.     DoBogusNetBIOSCall := False;
  208.     Exit;
  209.   end;
  210.   ClearNCB(PresenceNCB.NP^);
  211.   PresenceNCB.NP^.Command := NBInvalidCommand;    {make an impossible request}
  212.   NetBiosRequest(PresenceNCB.NR);
  213.   DoBogusNetBIOSCall := PresenceNCB.NP^.RetCode = NBEInvalidCommand;
  214.   FreeWinNCB(PresenceNCB);
  215. end;
  216.  
  217. function NetBIOSInstalled : Boolean; Export;
  218. {returns true if NetBIOS (or a NetBIOS emulator) is installed}
  219.  
  220. begin
  221.   if NetBIOSIntVecInstalled then
  222.     NetBIOSInstalled := DoBogusNetBIOSCall
  223.   else
  224.     NetBIOSInstalled := FALSE;             {no vector, no NetBIOS}
  225. end;
  226.  
  227.  
  228. procedure CallbackShell; Near; Assembler;
  229. asm
  230.   push    ax
  231.   push    bx
  232.   push    cx
  233.   push    dx
  234.   push    si
  235.   push    di
  236.   push    es
  237.   push    ds
  238.   push    bp
  239.   mov     ax,[si]
  240.   mov     dx,[si+2]
  241.   mov     es:[di].WindowsPostType.Regs.drIP,ax
  242.   mov     es:[di].WindowsPostType.Regs.drCS,dx
  243.   push    word ptr es:[di].WindowsPostType.Regs.drAX
  244.   push    word ptr es:[di].WindowsPostType.NCBs+2
  245.   push    word ptr es:[di].WindowsPostType.NCBs
  246.   mov     ax,es:[di].WindowsPostType.DataSegm
  247.   mov     ds,ax
  248.   call    dword ptr es:[di].WindowsPostType.Post
  249.   pop     bp
  250.   pop     ds
  251.   pop     es
  252.   pop     di
  253.   pop     si
  254.   pop     dx
  255.   pop     cx
  256.   pop     bx
  257.   pop     ax
  258.   iret
  259. end;
  260.  
  261. function GetWindowsPostRoutine(ProtectedProc : NetBiosPostRoutine;
  262.                                DataSegment : Word;
  263.                                var WinPost : WindowsPostType) : Boolean; Export;
  264. begin
  265.   with WinPost do begin
  266.     Post := ProtectedProc;
  267.     DataSegm := DataSegment;
  268.     AllocateRealModeCallBackAddr(@CallbackShell, DPMIRegisters(Regs), Callback);
  269.     GetWindowsPostRoutine := LongInt(Callback) <> 0;
  270.   end;
  271. end;
  272.  
  273. procedure FreeWindowsPostRoutine(var WinPost : WindowsPostType); Export;
  274. begin
  275.   FreeRealModeCallbackAddr(WinPost.Callback);
  276. end;
  277.  
  278. exports
  279.   GetWindowsPostRoutine   index 1,
  280.   FreeWindowsPostRoutine  index 2,
  281.   SendDataGram            index 3,
  282.   ReceiveDataGram         index 4,
  283.   NetBIOSInstalled        index 5,
  284.   CancelRequest           index 6,
  285.   NetBiosAddName          index 7,
  286.   NetBiosDeleteName       index 8,
  287.   AllocateWinNCB          index 9,
  288.   FreeWinNCB              index 10;
  289.  
  290. begin
  291. end.
  292.