Solved

find out a programs component name

Posted on 2004-08-04
10
1,524 Views
Last Modified: 2012-06-22
Hi,
How can I find out a program's component's name (class name)? For example a program has a TEdit, named txtname.
How can i find out the TEdit's name by Delphi code?
0
Comment
Question by:din345
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 7

Expert Comment

by:LRHGuy
ID: 11717393
It depends on where you want to know it from...

If it's in from the form, try something like:


function TForm1.GetNameTEdit:string;
begin
  Result:=TEdit.Name;
end;


You get the idea?
Larry
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 11717409
In other words, if you have the component "handy", you should be able to grab it right out of the "name" property...

Larry
0
 
LVL 4

Expert Comment

by:SurferJoe
ID: 11717610
This will require ClipBrd in your uses clause

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  s: String;
  H: Hwnd;

begin
  for i := 0 to ComponentCount-1 do begin
    s := s +  IntToStr(i) + #09 + Components[i].Name + #09 + Components[i].ClassName +  #13 + #10;

  end;


// NotePad
  WinExec('notepad.exe', SW_SHOWNORMAL);
  ClipBoard.AsText := s;

  H := FindWindowEx(Findwindow('notepad', nil), 0, 'edit', nil);
  SendMessage(H, WM_SETTEXT, 0, Integer(pChar(ClipBoard.AsText)));

end;
0
 
LVL 4

Expert Comment

by:SurferJoe
ID: 11717662
Here is another useful variatio;n

procedure SetAutoEdit( Sender: TForm; State: Boolean);
var
  i: Integer;

begin
// Manage Update and Save autoEdit States 10-16-2002gr  -----------------------------------
  with (Sender) do begin
    for i := 0 to ComponentCount-1 do begin
      if (Components[i].ClassType = TwwDataSource) then
         (FindComponent(Components[i].Name) as TwwDataSource).AutoEdit := State;

      if (Components[i].ClassType = TDataSource) then
         (FindComponent(Components[i].Name) as TDataSource).AutoEdit := State;

    end;

  end;

end;
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 4

Expert Comment

by:SurferJoe
ID: 11717679
This one is kind of cool, it actually test for the existence of a property.+

procedure myClearAllTabStops(Sender: TForm);
var
 i: Integer;
 myPropInfo: PPropInfo;

begin
// if component has tabstop clear it.
  with (Sender as TForm) do begin
    for i := 0 to ComponentCount - 1 do begin
      myPropInfo := GetPropInfo(Components[i], 'TabStop');

      if (Assigned(myPropInfo)) then
        (FindComponent(Components[i].Name) as TWinControl).TabStop := False;

    end;
  end;  
end;

HTH,

G
0
 
LVL 1

Author Comment

by:din345
ID: 11717887
i want a code like this...

App1.exe - it has a TEdit on its form wit name txtname.
App2.exe - this is the app that will get the classname from App1.exe (the TEdit's classname).

App2.exe  source code, something like this:
function TFrmApp2.GetClassname: string;
begin
Result := FindWindow()...
end;

procedure TFrmApp2.Button1Click(Sender: TObject);
var wnd: THandle;
begin
ShowMessage(GetClassName);
wnd := FindWindowEx(Findwindow('FrmApp1', 'Form1'), 0,GetClassname, nil);
end;
0
 
LVL 3

Expert Comment

by:MikProg
ID: 11723215
Fellas! All of you has forgot about address space. If App1 and App2 are separated processes (i.e. one not launched from another) there is now way for App1 to get some data from App2 (except windows attributes). Component class name is keeped inside process address space and inaccessible outside. About windows class names - it might work until you know that App2 IS DELPHI APP!
0
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 30 total points
ID: 11728051
MikProg, that's not completely true. App1 could inject a DLL in the process space of App2 and then communicate with this DLL using some interprocess communication technique. App2 would never notice anything of this (except for a slower performance) but the DLL would be able to look inside the process space of App2.

By enumerating all windows and child windows you can discover quite a few controls within any application, even if these are no Delphi applications. It will tell you the contents (text) and the classtype of the control. And yes, it would even show Delphi's editboxes and panels. However, it only displays registered (within Windows!) controls so quite a few controls might not be visible. With registered, I mean that the application declared these classes as special Windows classes at startup. Delphi is already doing this without developers being aware of this.

But you don't want the type, nor the text. You want the name of the edit control. Well, during runtime that information is retrieved from the DFM, which is part of the EXE as a resource. You could, of course, extract the DFM from the EXE file and convert it back to text to analyze it. That would be the only way to get the name of the control.

Unless you inject a DLL in the process. The DLL could quite easily access the control and even modify it. Too bad that DLL injections aren't easy...
0
 
LVL 3

Expert Comment

by:MikProg
ID: 11732925
Workshop Alex> Are we talk ClassName method result about (not underlaying window registered classname!)? If so no external access to this information until you can deal with exe relocation table. Althought VCL gives us loophole

----------- Unit Controls.pas -------------------
function ObjectFromHWnd(Handle: HWnd): TWinControl;
var
  OwningProcess: DWORD;
begin
  if (GetWindowThreadProcessID(Handle, OwningProcess) <> 0) and
     (OwningProcess = GetCurrentProcessID) then
    Result := Pointer(SendMessage(Handle, RM_GetObjectInstance, 0, 0))
  else
    Result := nil;
end;

{ Find a TWinControl given a window handle }
{ The global atom table is trashed when the user logs off.  The extra test
  below protects UI interactive services after the user logs off.  
  Added additional tests to enure that Handle is at least within the same
  process since otherwise a bogus result can occur due to problems with
  GlobalFindAtom in Windows.  }
function FindControl(Handle: HWnd): TWinControl;
var
  OwningProcess: DWORD;
begin
  Result := nil;
  if (Handle <> 0) and (GetWindowThreadProcessID(Handle, OwningProcess) <> 0) and
     (OwningProcess = GetCurrentProcessId) then
  begin
    if GlobalFindAtom(PChar(ControlAtomString)) = ControlAtom then
      Result := Pointer(GetProp(Handle, MakeIntAtom(ControlAtom)))
    else
      Result := ObjectFromHWnd(Handle);
  end;
end;
----------------------------------------------------------------------------
As you can see message result of

    Pointer(SendMessage(Handle, RM_GetObjectInstance, 0, 0))

is pointer to TWinControl object instance. There is way to use it in external app? No!

   TObject(SendMessage(Handle, RM_GetObjectInstance, 0, 0)).ClassName

will not work (must not work!) because of windows interprocess protection mechanism.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 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

20 Experts available now in Live!

Get 1:1 Help Now