Is it safe to change TCreateParams.WinClassName or how to find a form handle of another own application?

1k views Asked by At

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!

2

There are 2 answers

2
David Heffernan On BEST ANSWER

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.

3
Remy Lebeau On

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.