Solved

Delphi Dll Issue

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax Check Delphi Seattle IOS app without MAC ? 1 97
Convert a string into a TDateTime 5 60
Multiple image collision 13 74
JAudiorecorder record freezing the app 29 67
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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