home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 October / Chip_2000-10_cd1.bin / zkuste / Delphi / navody / multithread / mchpipeinterface2.pas < prev    next >
Pascal/Delphi Source File  |  1999-05-10  |  6KB  |  184 lines

  1. { 10-05-1999 10:36:26 PM > [martin on MARTIN] checked out /Reformatting
  2.    according to Delphi guidelines. }
  3. { 14-04-1999 11:59:06 PM > [martin on MARTIN] update: Changing dynamic
  4.    methods to virtual. (0.1) /  }
  5. { 14-04-1999 11:52:47 PM > [martin on MARTIN] checked out /Changing dynamic
  6.    methods to virtual. }
  7. { 06-04-1999 1:46:38 AM > [martin on MARTIN] check in: (0.0) Initial Version
  8.    / None }
  9. unit MCHPipeInterface2;
  10.  
  11. {Martin Harvey 23/9/1998}
  12. {Another interface unit, but one that allows for dynamic loading of the DLL}
  13.  
  14. interface
  15.  
  16. uses MCHPipeTypes,SysUtils;
  17.  
  18. function LoadPipeDLL:boolean;
  19. function UnloadPipeDLL:boolean;
  20.  
  21. {Load and unload functions automatically call initialise and finalise DLL procs}
  22.  
  23. {All the functions below will also raise EDLLNotLoaded}
  24.  
  25. function ConnectServer(var hHandle:TMCHHandle):TMCHError;
  26. {Returns error if server already connected}
  27. function ConnectClient(var hHandle:TMCHHandle):TMCHError;
  28. {Returns error if client already connected}
  29. function WriteData(hHandle:TMCHHandle;var Buf;Count:integer):TMCHError;
  30. {Returns error if client or server not connected (or disconnects during block)
  31.  Blocks if buffer full}
  32. function ReadData(hHandle:TMCHHandle;var Buf;Count:integer):TMCHError;
  33. {Returns error if client or server not connected (or disconnects during block)
  34.  Blocks if buffer empty}
  35. function PeekData(hHandle:TMCHHandle;var BytesReady:integer):TMCHError;
  36. {Returns error if client or server not connected, never blocks}
  37. function WaitForPeer(hHandle:TMCHHandle):TMCHError;
  38. {Lets a thread wait for the peer to connect}
  39. function DisconnectServer(hHandle:TMCHHandle):TMCHError;
  40. {Returns error if server not connected, or bad handle}
  41. function DisconnectClient(hHandle:TMCHHandle):TMCHError;
  42. {Returns error if client not connected or bad handle}
  43.  
  44. function GetDLLLoaded:boolean;
  45.  
  46. implementation
  47.  
  48. uses Windows;
  49.  
  50. type
  51.   ConnectProc = function(var hHandle:TMCHHandle):TMCHError stdcall;
  52.   DisconnectProc = function(hHandle:TMCHHandle):TMCHError;stdcall;
  53.   DataProc = function(hHandle:TMCHHandle;var Buf;Count:integer):TMCHError stdcall;
  54.   PeekProc = function(hHandle:TMCHHandle;var BytesReady:integer):TMCHError stdcall;
  55.   ProcNoParams = procedure stdcall;
  56.  
  57. var
  58.   InitProc,FinalProc:ProcNoParams;
  59.   ConnectServerProc,ConnectClientProc:ConnectProc;
  60.   DisconnectServerProc,DisconnectClientProc:DisconnectProc;
  61.   WaitForPeerProc:DisconnectProc;
  62.   WriteDataProc,ReadDataProc:DataProc;
  63.   PeekDataProc:PeekProc;
  64.   DLLLoaded:boolean;
  65.   DLLInstanceHandle:THandle;
  66.  
  67. function GetDLLLoaded:boolean;
  68. begin
  69.   result := DLLLoaded;
  70. end;
  71.  
  72. function LoadPipeDLL:boolean;
  73. begin
  74.   result := false;
  75.   DLLInstanceHandle := LoadLibrary(PipeDLLName);
  76.   if DLLInstanceHandle <> 0 then
  77.   begin
  78.     InitProc := GetProcAddress(DLLInstanceHandle, 'Initialise');
  79.     FinalProc := GetProcAddress(DLLInstanceHandle, 'Finalise');
  80.     ConnectServerProc := GetProcAddress(DLLInstanceHandle, 'ConnectServer');
  81.     ConnectClientProc := GetProcAddress(DLLInstanceHandle, 'ConnectClient');
  82.     DisconnectServerProc := GetProcAddress(DLLInstanceHandle, 'DisconnectServer');
  83.     DisconnectClientProc := GetProcAddress(DLLInstanceHandle, 'DisconnectClient');
  84.     WaitForPeerProc := GetProcAddress(DLLInstanceHandle, 'WaitForPeer');
  85.     WriteDataProc := GetProcAddress(DLLInstanceHandle, 'WriteData');
  86.     ReadDataProc := GetProcAddress(DLLInstanceHandle, 'ReadData');
  87.     PeekDataProc := GetProcAddress(DLLInstanceHandle, 'PeekData');
  88.     result := true;
  89.     InitProc;
  90.   end;
  91.   DLLLoaded := result;
  92. end;
  93.  
  94. function UnLoadPipeDLL:boolean;
  95. begin
  96.   result := false;
  97.   if DLLLoaded then
  98.   begin
  99.     FinalProc;
  100.     result := FreeLibrary(DLLInstanceHandle);
  101.     DLLLoaded := false;
  102.   end;
  103. end;
  104.  
  105. function ConnectServer(var hHandle:TMCHHandle):TMCHError;
  106. {Returns error if server already connected}
  107. begin
  108.   if DLLLoaded then
  109.     result := ConnectServerProc(hHandle)
  110.   else
  111.     result := meDLLNotLoaded;
  112. end;
  113.  
  114. function ConnectClient(var hHandle:TMCHHandle):TMCHError;
  115. {Returns error if client already connected}
  116. begin
  117.   if DLLLoaded then
  118.     result := ConnectClientProc(hHandle)
  119.   else
  120.     result := meDLLNotLoaded;
  121. end;
  122.  
  123. function DisconnectServer(hHandle:TMCHHandle):TMCHError;
  124. {Returns error if server not connected, or bad handle}
  125. begin
  126.   if DLLLoaded then
  127.     result := DisconnectServerProc(hHandle)
  128.   else
  129.     result := meDLLNotLoaded;
  130. end;
  131.  
  132. function DisconnectClient(hHandle:TMCHHandle):TMCHError;
  133. {Returns error if client not connected or bad handle}
  134. begin
  135.   if DLLLoaded then
  136.     result := DisconnectClientProc(hHandle)
  137.   else
  138.     result := meDLLNotLoaded;
  139. end;
  140.  
  141.  
  142. function WriteData(hHandle:TMCHHandle;var Buf;Count:integer):TMCHError;
  143. {Returns error if client or server not connected (or disconnects during block)
  144.  Blocks if buffer full}
  145. begin
  146.   if DLLLoaded then
  147.     result := WriteDataProc(hHandle,Buf,Count)
  148.   else
  149.     result := meDLLNotLoaded;
  150. end;
  151.  
  152. function ReadData(hHandle:TMCHHandle;var Buf;Count:integer):TMCHError;
  153. {Returns error if client or server not connected (or disconnects during block)
  154.  Blocks if buffer empty}
  155. begin
  156.   if DLLLoaded then
  157.     result := ReadDataProc(hHandle,Buf,Count)
  158.   else
  159.     result := meDLLNotLoaded;
  160. end;
  161.  
  162. function PeekData(hHandle:TMCHHandle;var BytesReady:integer):TMCHError;
  163. {Returns error if client or server not connected, never blocks}
  164. begin
  165.   if DLLLoaded then
  166.     result := PeekDataProc(hHandle,BytesReady)
  167.   else
  168.     result := meDLLNotLoaded;
  169. end;
  170.  
  171. function WaitForPeer(hHandle:TMCHHandle):TMCHError;
  172. {Lets a thread wait for the peer to connect}
  173. begin
  174.   if DLLLoaded then
  175.     result := WaitForPeerProc(hHandle)
  176.   else
  177.     result := meDLLNotLoaded;
  178. end;
  179.  
  180. begin
  181.   DLLLoaded := false;
  182. end.
  183.  
  184.