Solved

Delphi Dll Issue

Posted on 2008-10-09
2
708 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now