Solved

Delphi Dll Issue

Posted on 2008-10-09
2
711 Views
Last Modified: 2008-10-09
Having a little issue here with a Delphi Dll.
Problem is when I close the application that uses the Dll the process still shows up, which means my application isn't terminating properly.

Dll Code

library dllclient;

uses
  SysUtils,
  Classes,
  IdTCPClient;

{$R *.res}

var
  Client: TCPClient;

function TcpConnect(_host: pchar; _port: integer) : Integer; export; stdcall;
begin
  Client:= TCPClient.Create(nil);
  Client.Port:= _port;
  Client.Host:= _host;
  try
    Client.Connect;
  except
    result:= -1;
  end;
  result:= Integer(Client);
end;

function TcpConnected(socket: integer) : Boolean; export; stdcall;
begin
  result:= TcpClient(socket).Connected;
end;

function CloseSocket(socket: integer) : Boolean; export; stdcall;
begin
  try
    if Assigned(TcpClient(socket)) and (TcpClient(socket).Connected) then
    begin
      TcpClient(socket).IOHandler.InputBuffer.Clear;
      TcpClient(socket).Socket.Close;
      TcpClient(socket).Free;
    end;
  except
    result:= False;
  end;
  result:= True;
end;

exports TcpConnect, TcpConnected, CloseSocket;

begin
end.

Form Code
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdCustomTCPServer,
  IdTCPServer, IdContext;

type
  TForm1 = class(TForm)
    IdTCPServer1: TIdTCPServer;
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure IdTCPServer1Execute(AContext: TIdContext);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  socketint: integer;

function TcpConnect(host: PChar; port: integer):Integer; stdcall; external 'dllclient.dll';
function TcpConnected(socket: Integer): Boolean; stdcall; external 'dllclient.dll';
function CloseSocket(socket: Integer) : Boolean; stdcall; external 'dllclient.dll';

procedure TForm1.Button1Click(Sender: TObject);
begin
  if(socketint = -1) then
  begin
    socketint:= TcpConnect('localhost', 33333);
    memo1.Lines.add(IntToStr(socketint));

    if(TcpConnected(socketint)) then
      memo1.lines.add('We are connected!');
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if(socketint <> -1) then
  begin
    if(CloseSocket(socketint)) then
    begin
      memo1.lines.add('Socket Closed!');
      socketint:= -1;
    end
    else
      memo1.lines.add('Failed to Close Socket');
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  socketint:= -1;
end;

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
begin
  while(True) do
  begin
    sleep(100);
  end;
end;

end.
0
Comment
Question by:yewnix
2 Comments
 
LVL 6

Accepted Solution

by:
ChristianWimmer earned 500 total points
ID: 22682690
What about  TForm1.IdTCPServer1Execute?
It's obviously an endless loop. I guess this could be a problem. Maybe you should add a Terminate variable or similar.
0
 

Author Comment

by:yewnix
ID: 22682918
Ya.. I'm an idiot. Shouldn't of even of had to post this.

Changed that code to..

if not (AContext.Connection.IOHandler.InputBufferIsEmpty) then
begin
   // do stuff here
end
else
   sleep(100);

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question