Search code examples
delphiwinapiclassnamefindwindowcreateparams

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


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!


Solution

  • 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.