Solved

How do I programatically create Indy components

Posted on 2009-04-08
7
358 Views
Last Modified: 2012-05-06
Hi experts, I am trying to create a service and creating the indy components (tcpserver, udpserver) from code rather than dropping a component on a form.  I'm looking for some code sample that shows how to do this.

Thanks.
0
Comment
Question by:viziopoint
  • 3
  • 2
  • 2
7 Comments
 
LVL 13

Assisted Solution

by:ThievingSix
ThievingSix earned 25 total points
ID: 24104193
Simple.

procedure Button1Click(Sender: TObject);
var
  IRC : TidIRC;
begin
  IRC := TidIRC.Create(...);
  IRC.Connect;
  blah blah
end;

Remember to include the correct units associated with the component in your uses for it to work.
0
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 100 total points
ID: 24104567
GExperts has a very nice feature for this http://www.gexperts.org
after installing it into the Delphi IDE, do this:
put a component on your form as you normally do
then right click on the component
(gexperts will have added extra menu options)
Components to Code

This creates a variable, the component creation with the constructor and the properties

I got this, from 3 clicks and a Ctrl-V to paste:

var
  IdTCPClient1: TIdTCPClient;

  IdTCPClient1 := TIdTCPClient.Create(Self);
  with IdTCPClient1 do
  begin
    Name := 'IdTCPClient1';
    MaxLineAction := maException;
    ReadTimeout := 0;
    Port := 0;
  end;


0
 
LVL 13

Expert Comment

by:ThievingSix
ID: 24104986
Hey, that's really useful!

Thanks Geert_Gruwez =D
0
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.

 

Author Comment

by:viziopoint
ID: 24113562
The GeExpert tip is indeed a very really useful tip! - Thanks!

I'm getting some odd results though like:

Bindings := <>;
It tried to replace the <> with nil - compiles but does not work (crashes) so I just commented them out and it seems to work.

I also had problems with assigning event handlers since I don't have a form.  I was able to fix this with a little trick that I think is fine:

type
   TEventHandlers = class // create a dummy class
       procedure UDPServerUDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
   end;
...
var
   EvHandlers : TEventHandlers;
....
IdUDPSever1.OnUDPRead := EvHandlers.UDPServerUDPRead

to the component created with the GeExpert code and it seems to work.

I'll continue to test and report back.

Thanks again for the help.
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 24113885
you don't have a form and you want to use an event handler ?

the way you do it with the dummy class is actually the solution!

why you may ask ... well because how the event handler is defined
a event handler is a method, and a method is a procedure of a object

TNotifyEvent = procedure (Sender: TObject) of object;

the *of object* at the end means it must be defined inside a class.

you will have to create a instance of EvHandlers at runtime too...

0
 
LVL 13

Expert Comment

by:ThievingSix
ID: 24113977
Actually you should have the indy component in the event handler dummy class.
0
 

Author Comment

by:viziopoint
ID: 24154860
Just wanted to confirm that following the tips above, everything worked fine.  Thanks Again!
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

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…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

910 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

22 Experts available now in Live!

Get 1:1 Help Now