Einzelnen Beitrag anzeigen

Scorpion3000

Registriert seit: 18. Apr 2004
47 Beiträge
 
Delphi 7 Enterprise
 
#1

DdeClientConv bzw DdeClientItem in einer Dll

  Alt 17. Jan 2005, 17:37
Hi Leute!

Ich möchte gerne durch eine Dll eine Abfrage über eine TDdeClientConv machen. Das Senden der Daten verläuft problemlos. Allerdings macht mir das Empfangen der Daten vom Server Probleme:

Hier der Quelltext von der DLL:

Delphi-Quellcode:
uses
  SysUtils,
  DdeMan,
  Windows,
  Classes;

type
   TDummy = class (TForm)
   public
      class procedure DllClientItemChange(Sender: TObject);
   end;
var
   DllClientConv : TDdeClientConv;
   DllClientItem : TDdeClientItem;
   MsgError : string;
   MsgResult : string;
   ServerResponse: boolean;


{$R *.res}

class procedure TDummy.DllClientItemChange(Sender: TObject);
begin
   if DllClientItem.Lines.Count = 2 then begin
      MsgError := DllClientItem.Lines.Strings[0];
      MsgResult:= DllClientItem.Lines.Strings[1];
   end
   else if DllClientItem.Lines.Count = 1 then
      MsgError:= DllClientItem.Lines.Strings[0]
   else
      MsgError:= 'Error in server response!';
   ServerResponse:= true;

   MsgError:= 'No Error';
end;

function GetCredits(PcName, UserName: string; var CreditValue: string;
   var Error: string; TimeOut: integer = 100): boolean;
var
   li1: integer;
begin
   DllClientConv := TDdeClientConv.Create(nil);
   DllClientItem := TDdeClientItem.Create(nil);
   DllClientConv.Name := 'DLLClientConv';
   DllClientItem.Name := 'DllClientItem';
   DllClientItem.DdeConv := DllClientConv;
   DllClientItem.OnChange:= TDummy.DllClientItemChange;
   DllClientItem.DdeItem := 'CreditManagerServerItem';
   result := DllClientConv.SetLink('Project1', 'CreditManagerServerConv');
   ServerResponse := false;
   MsgError := '';
   if result then begin
      DllClientItem.Lines.Add('GET_CREDITS');
      DllClientItem.Lines.Add(PcName);
      DllClientItem.Lines.Add(UserName);
      result:= DllClientConv.ExecuteMacroLines(DllClientItem.Lines, true);
      if result then begin
         li1:= GetTickCount;
         while GetTickCount-li1 <= 5000 do begin
            if ServerResponse = true then
               Exit;
         end;
         if ServerResponse then begin
            try
               CreditValue:= DllClientItem.Lines.Text;
               Error := PChar(MsgError+'s');
            except
               CreditValue:= '0';
               result := false;
               Error := PChar('GET_CREDITS-value is not an integer');
            end;
         end
         else begin
            CreditValue:= '0';
            result := false;
            Error := PChar(MsgError);
         end;
      end;
   end;
end;

exports
   GetCredits;

begin
end.
Hier der Code von der Client-App:

Delphi-Quellcode:
function GetCredits(PcName, UserName: string; var CreditValue: PChar; var Error: PChar; TimeOut: integer = 100): boolean; external 'D:\Project1.dll';

procedure TForm1.Button1Click(Sender: TObject);
var
   s1, s2: PChar;
begin
   GetCredits(PChar('Test'), PChar('Test'), s1, s2, 100);
   ListBox1.Items.Add('New request: '+TimeTostr(now));
   ListBox1.Items.Add('Value: '+s1);
   ListBox1.Items.Add('Error: '+s2);
   ListBox1.Items.Add('-------------------------------');
   ListBox1.Items.Add('');
   ListBox1.Selected[ListBox1.Items.Count-1]:= true;
end;
Hier der Code von der Server-App:
Delphi-Quellcode:
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DdeMan;

type
  TForm1 = class(TForm)
    Button1: TButton;
    CreditManagerServerConv: TDdeServerConv;
    CreditManagerServerItem: TDdeServerItem;
    ListBox1: TListBox;
    Label1: TLabel;
    procedure CreditManagerServerConvExecuteMacro(Sender: TObject; Msg: TStrings);
    procedure Button1Click(Sender: TObject);
    procedure CreditManagerServerConvOpen(Sender: TObject);
    procedure CreditManagerServerConvClose(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.CreditManagerServerConvExecuteMacro(Sender: TObject;
  Msg: TStrings);
begin
   if Msg.Text <> 'Recieved!then begin
      ListBox1.Items.Add('New message recieved: '+TimeTostr(now));
      ListBox1.Items.AddStrings(Msg);
      ListBox1.Items.Add('Response = 1234');
      ListBox1.Items.Add('----------------------------');
      ListBox1.Items.Add('');
      ListBox1.Selected[ListBox1.Items.Count-1]:= true;
      CreditManagerServerItem.Lines.Add('1234');
      CreditManagerServerItem.Lines.Add('No Error');
   end
   else begin
      ListBox1.Items.Add('Last message recieved by client');
      ListBox1.Items.Add('----------------------------');
      ListBox1.Items.Add('');
   end;
end;
Ist es überhaupt möglich oder hab ich etwas übersehen?
Bin euch für eure Hilfe dankbar.
  Mit Zitat antworten Zitat