各位大虾:    小弟想要做个动态库,在其中加载clientsocket控件,使得能够发送指令给server端和接收server端返回的执行结果。望各位大虾多多指教!小弟不胜感激!分不够再加,最好能给出代码

解决方案 »

  1.   

    我在dll中建了一个form,其上放了clientsocket控件,写了接收事件,在dll中有如下过程:
    procedure SendCommand(Ip: string;Port: integer;S:string);
    var frmsoc:Tfrmsoc;
    begin
      frmsoc:=Tfrmsoc.Create(frmsoc);
      frmsoc.InitiateAction;
      frmsoc.ClientSocket1.Address:=Ip;
      frmsoc.ClientSocket1.Port:=Port;
     if not frmsoc.ClientSocket1.Active then
        frmsoc.ClientSocket1.Active:=true;
      frmsoc.ClientSocket1.Socket.SendText(S);  frmsoc.ClientSocket1.Active:=false;
      frmsoc.ClientSocket1.Close;
      frmsoc.Free;end;
    为什么服务端接收不到字符串s?
    又怎么将服务端的返回值接收到并传给调用者?
      

  2.   

    我在开发DLL时采用的是Socket API,直接用Send和Recv向服务器端发送和接收,控制比较简单,适合在一个事务内完成。
      

  3.   

    在DLL中创建Form,应该将应用程序的句柄传递给DLL的TApplication对象。而且,Form的拥有者应该是Application。下面是《Delphi 4 开发大全》上的示例:
    functoin xxxx(AHandle: THandle; ACaption: String): LongInt;
    var
      DLLForm: TDLLForm;
    begin
      Application.Handle := AHandle;
      DLLForm := TDLLForm.Create(Application);
      Result := LongInt(DLLForm);
      DLLForm.Caption := ACaption;
      DLLForm.Show;
    end;
      

  4.   

    感谢大家的支持,我的问题已解决,代码如下:
    var
      SocketReadEvt :TEvent;type
      TSocketForm = class(TWinControl)
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
      public
        RedText:String;
        procedure OnClientSocketRead(Sender: TObject;
              Socket: TCustomWinSocket);  end;constructor TSocketForm.Create(AOwner: TComponent);
    begin
        inherited ;
    end;destructor TSocketForm.Destroy;
    begin
      Inherited;
    end;
    //接收返回值
    procedure TSocketForm.OnClientSocketRead(Sender: TObject;
      Socket: TCustomWinSocket);
    begin
      RedText:=  Socket.ReceiveText;
       SocketReadEvt.SetEvent();
    end;//发送命令
    Function SendCommand(Ip: string;Port: integer;S:string):String;
    var
      AClientSocket:TClientSocket;
      frm:TSocketForm;
      buf:Array of char;
    begin
      Result := '';
      SocketReadEvt :=TEvent.Create(nil,False,False,'');
      AClientSocket := TClientSocket.Create(Application);
      frm := TSocketForm.Create(Application);
      try
        AClientSocket.Address := IP;
        AClientSocket.Port := port;
        AClientSocket.Active := True;
        AClientSocket.OnRead := frm.OnClientSocketRead;    while not (AClientSocket.Active = True) do
            Application.ProcessMessages;    setLength(Buf,Length(s)+2);
        Buf[0] := char($03);
        StrCopy(@Buf[1],PCHAR(s));
        Buf[Length(Buf)-1] := Char($0D);
        AClientSocket.Socket.SendBuf(Buf[0],Length(Buf));
        //AClientSocket.Socket.SendText(S);    while not (SocketReadEvt.WaitFor(0) = wrSignaled) do
           Application.ProcessMessages;    SetLength(Result,Length(frm.RedText));
        Result  := frm.RedText;
      finally
        AClientSocket.Free;
        SocketReadEvt.Free ;
        frm.Free();
      end;
    end;