[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

find out a programs component name

Posted on 2004-08-04
10
Medium Priority
?
1,583 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

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…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month19 days, 5 hours left to enroll

834 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