Solved

Delphi Dll Issue

Posted on 2008-10-09
2
712 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Mdi application Child forms get behind control 7 311
URL for downloading Google Chrome for Win XP 2 235
RESTRequest Parameter 4 54
Firemonkey allowing RTL on android 6 56
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…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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