3

I have two applications, where the first one needs to find a handle of the form from the second (also my own) but different application (not instance, but different application). I've seen some techniques, but I would like to know if it's safe what I would like to do or what is efficient way to do this.

I was thinking about to use the FindWindow function where you can pass the class name, so if I would change the WinClassName member in CreateParams of the form to some unique value (e.g. GUID), then I would be pretty easily able to find this Window with a big chance it is the one from my application.

Application whose form needs to be found:

procedure TForm1.CreateParams(var Params: TCreateParams);
begin
  inherited;
  Params.WinClassName := '{1EE65C52-2F4B-4600-AAE2-079C29AD2220}';
end;

The other application which needs to find the form from the previous one:

procedure TForm1.Button1Click(Sender: TObject);
var
  FormHandle: HWND;
begin
  FormHandle := FindWindow('{1EE65C52-2F4B-4600-AAE2-079C29AD2220}', nil);
  ShowMessage(IntToStr(FormHandle));
end;

My question is:

Is it safe to change this member of the TCreateParams to whatever I want or is it unsafe in something ? Or how would you look for a handle of the form from your own another application (not application instance, but your own another application) ?

Thanks a lot!

Martin Reiner
  • 2,167
  • 2
  • 20
  • 34

2 Answers2

5

Yes it is perfectly safe to do this. Naturally each different class must have a unique name, but it's up to you to ensure that is the case.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • @Ken - I know about it, I was asking if it's safe to change it, if it's safe it will differ from the class name from design time. And in case like from my question, where the GUID beginning with number or the `{` char this would be impossible. And I wanted to avoid the use of form caption in the FindWindow. – Martin Reiner May 24 '12 at 07:55
  • @David, probably should have been to the original question. Seems it doesn't matter, though. :) Deleting. – Ken White May 24 '12 at 12:26
3

Rather than using the same TForm1 class name in both apps, you should get in the habit of changing the default class names to more meaningful names, like TApp1MainForm and TApp2MainForm. The VCL automatically assigns a form's ClassName as its TCreateParams.WinClassName by default, in TWinControl.CreateParams(). If you change the form's Name property in the Object Inspector at design-time, it updates the ClassName for you, then you do not have to change the TCreateParams.WinClassName value manually.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • +1. This is the proper way to do it (and the better answer to the question asked). – Ken White May 23 '12 at 22:07
  • 1
    Thanks! But I would like to use GUID as a class name and it's impossible to have a form name beginning with number (in this case). Anyway I knew about it, this question was about if it's safe to change it so the form class name from design time differs from the assigned. – Martin Reiner May 24 '12 at 07:53
  • It depends. Personally I prefer to have the main form of all my apps named `TMainForm`. If I were to prefix the app title then it would have a large knock on effect throughout the code base. Some people might be content with that but certainly I could sympathise if people did not want to do it that way. – David Heffernan May 24 '12 at 08:03