Solved

Delphi Dll Issue

Posted on 2008-10-09
2
705 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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

7 Experts available now in Live!

Get 1:1 Help Now