• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1660
  • Last Modified:

find out a programs component name

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
din345
Asked:
din345
  • 3
  • 2
  • 2
  • +2
1 Solution
 
LRHGuyCommented:
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
 
LRHGuyCommented:
In other words, if you have the component "handy", you should be able to grab it right out of the "name" property...

Larry
0
 
Greg RowlandSoftware Designer, SysDBA, WebMaster OwnerCommented:
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Greg RowlandSoftware Designer, SysDBA, WebMaster OwnerCommented:
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
 
Greg RowlandSoftware Designer, SysDBA, WebMaster OwnerCommented:
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
 
din345Author Commented:
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
 
MikProgCommented:
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
 
Wim ten BrinkSelf-employed developerCommented:
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
 
MikProgCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now