?
Solved

find out a programs component name

Posted on 2004-08-04
10
Medium Priority
?
1,599 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
9 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:Greg Rowland
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
LVL 4

Expert Comment

by:Greg Rowland
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:Greg Rowland
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 60 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Suggested Courses

599 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