home *** CD-ROM | disk | FTP | other *** search
/ GCW Games & More & Wacky Windows Companion / gcw.iso / win / util / mygroups / dde.pas < prev    next >
Pascal/Delphi Source File  |  1994-06-03  |  5KB  |  200 lines

  1. Unit DDE;
  2. {Implements an object wrapper around some common DDE functions}
  3.  
  4. Interface
  5.  
  6. Uses WinTypes, DDEML,
  7.    {$IFDEF VER70} Objects; {$ELSE} WObjects; {$ENDIF}
  8.  
  9. Type PDDE = ^TDDE;
  10.      TDDE = Object(TObject)
  11.             Inst:LongInt;         {Instance ID returned by DDML lib}
  12.             Conv:hConv;           {Handle to current conversation}
  13.             CBF:TFarProc;         {Procedure instance to callback function}
  14.             Initialized:Boolean;  {True if object is initialized successfully}
  15.             Timeout:LongInt;      {Timeout value for a DDE command}
  16.             PResponse:Pointer;    {Pointer to data returned from a DDE request}
  17.             ResponseLen:LongInt;  {Length of data pointed to by PResponse}
  18.  
  19.             Constructor Init(CallBack:Pointer; Mask:LongInt);
  20.             Destructor Done; Virtual;
  21.             Function Connect(Service,Topic:PChar):Boolean;
  22.             Procedure Disconnect;
  23.             Procedure SetTimeout(TOut:LongInt);
  24.             Function Request(Item:PChar; Format:Word; Var Len:LongInt):Pointer;
  25.             Function Execute(Com:PChar; Format:Word):LongInt;
  26.             Procedure FreeRequest;
  27.             Function LastError:Word;
  28.             End;
  29.  
  30. Implementation
  31.  
  32. Uses WinProcs;
  33.  
  34. Function DDE_CB(CallType, Fmt: Word; Conv: HConv; hsz1, hsz2: HSZ;
  35.           Data: HDDEData; Data1, Data2: Longint): HDDEData; Export;
  36. {The DDE callback function. This function just returns zero for all
  37.  callbacks. It can be overridden if another response is needed}
  38.  
  39.    Begin
  40.    DDE_CB:=HDDEData(0);
  41.    End;
  42.  
  43. Constructor TDDE.Init(CallBack:Pointer; Mask:LongInt);
  44. {Initialize the DDE object.
  45.  
  46.  Input:  CallBack: Pointer to a callback procedure. If Nil, default is used.
  47.          Mask: Flags passed to DDEInitialize. See SDK manual}
  48.  
  49.    Begin
  50.    TObject.Init;
  51.    Initialized:=False;
  52.    Inst:=0;
  53.    Conv:=0;
  54.    Timeout:=1000;
  55.    PResponse:=Nil;
  56.    ResponseLen:=0;
  57.    If CallBack = Nil then
  58.       CBF:=MakeProcInstance(@DDE_CB,hInstance)
  59.    else
  60.       CBF:=MakeProcInstance(CallBack,hInstance);
  61.    If DdeInitialize(Inst,TCallBack(CBF),Mask,0) <> 0 then
  62.       Begin
  63.       Done;
  64.       Fail;
  65.       End;
  66.    Initialized:=True;
  67.    End;
  68.  
  69. Destructor TDDE.Done;
  70. {Disconnect is necessary. Free any memory and wrap up the object}
  71.  
  72.    Begin
  73.    If Initialized then
  74.       Begin
  75.       If Conv <> 0 then
  76.          Disconnect;
  77.       If PResponse <> Nil then
  78.          FreeMem(PResponse,ResponseLen);
  79.       DdeUninitialize(Inst);
  80.       Initialized:=False;
  81.       End;
  82.    FreeProcInstance(CBF);
  83.    TObject.Done;
  84.    End;
  85.  
  86. Function TDDE.Connect(Service,Topic:PChar):Boolean;
  87. {Connect to a DDE server.
  88.  Returns true if successful.
  89.  
  90.  Input: Service - Name of DDE service (i.e. PROGMAN)
  91.         Topic - Name of DDE topic (i.e. PROGMAN)}
  92.  
  93. Var hszService,hszTopic:HSZ;
  94.  
  95.    Begin
  96.    Connect:=False;
  97.    If Conv <> 0 then Exit;
  98.    hszService:=DdeCreateStringHandle(Inst,Service,cp_WinAnsi);
  99.    hszTopic:=DdeCreateStringHandle(Inst,Topic,cp_WinAnsi);
  100.    Conv:=DdeConnect(Inst,hszService,hszTopic,Nil);
  101.    DdeFreeStringHandle(Inst,hszService);
  102.    DdeFreeStringHandle(Inst,hszTopic);
  103.    Connect:=Conv <> 0;
  104.    End;
  105.  
  106. Procedure TDDE.Disconnect;
  107. {Disconnect any current DDE conversation}
  108.  
  109.    Begin
  110.    If Conv <> 0 then
  111.       DdeDisconnect(Conv);
  112.    Conv:=0;
  113.    End;
  114.  
  115. Procedure TDDE.SetTimeout(TOut:LongInt);
  116. {Set the timeout for any DDE command
  117.  
  118.  Input: TOut - Timeout}
  119.  
  120.    Begin
  121.    Timeout:=TOut;
  122.    End;
  123.  
  124. Function TDDE.Request(Item:PChar; Format:Word; Var Len:LongInt):Pointer;
  125. {Send a DDE request to the current server.
  126.  Returns a pointer to the result.
  127.  
  128.  Input:  Item - Pointer to the DDE request string
  129.          Format - One of the cf_XXXX IDs describing the format of the
  130.                   input string (i.e. cf_Text)
  131.  Output: Len - Length of result}
  132.  
  133. Var P1:Pointer;
  134.     hszItem:HSZ;
  135.     hData:HDDEData;
  136.     Result:LongInt;
  137.  
  138.    Begin
  139.    Request:=Nil;
  140.    If Conv = 0 then Exit;
  141.    hszItem:=DdeCreateStringHandle(Inst,Item,cp_WinAnsi);
  142.    hData:=DdeClientTransaction(Nil,0,Conv,hszItem,Format,xtyp_Request,
  143.       Timeout,@Result);
  144.    DdeFreeStringHandle(Inst,hszItem);
  145.    If hData = 0 then Exit;
  146.    If PResponse <> Nil then
  147.       Begin
  148.       FreeMem(PResponse,ResponseLen);
  149.       PResponse:=Nil;
  150.       End;
  151.    P1:=DdeAccessData(hData,@Len);
  152.    GetMem(PResponse,Len);
  153.    If PResponse <> Nil then
  154.       Move(P1^,PResponse^,Len);
  155.    ResponseLen:=Len;
  156.    DdeUnaccessData(hData);
  157.    DdeFreeDataHandle(hData);
  158.    Request:=PResponse;
  159.    End;
  160.  
  161. Function TDDE.Execute(Com:PChar; Format:Word):LongInt;
  162. {Send a DDE command to a server.
  163.  Returns the error code, if any.
  164.  
  165.  Input:  Com - Command string
  166.          Format - Format of command string (i.e. cf_Text)}
  167.  
  168. Var Result:LongInt;
  169.  
  170.    Begin
  171.    Execute:=0;
  172.    If Conv = 0 then Exit;
  173.    DdeClientTransaction(Com,lstrlen(Com)+1,Conv,0,Format,xtyp_Execute,
  174.       Timeout,@Result);
  175.    Execute:=Result;
  176.    End;
  177.  
  178. Procedure TDDE.FreeRequest;
  179. {Frees the memory occupied by the response to the latest DDE request}
  180.  
  181.    Begin
  182.    If PResponse <> Nil then
  183.       Begin
  184.       FreeMem(PResponse,ResponseLen);
  185.       PResponse:=Nil;
  186.       ResponseLen:=0;
  187.       End;
  188.    End;
  189.  
  190. Function TDDE.LastError:Word;
  191. {Returns the most recent DDE error}
  192.  
  193.    Begin
  194.    If Inst <> 0 then
  195.       LastError:=DdeGetLastError(Inst)
  196.    else
  197.       LastError:=0;
  198.    End;
  199.  
  200. End.