Solved

find out a programs component name

Posted on 2004-08-04
10
1,532 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Path  to current project in Delphi. 2 80
delphi prevent click fast 2 219
How to convert memory stream to PDF file 6 59
scroll down TListBox component in Delphi 1 11
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…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

809 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