Page 1 of 1

Invalid window handle (Delphi)

Posted: Tue Feb 28, 2023 11:16 am
by ronaldolimajr
Hi,
I am using CEF4Delphi and receiving the error "Invalid window handle" in StartMainProcess and Free of the object GlobalCEFApp. Is there anything that could cause this error? Below, the code snippet where the process is make (in .dpr file):

Code: Select all

   if DirectoryExists(extractFilePath(ParamStr(0)) + 'cef') then
   begin
      try
         RemoveDir(extractFilePath(ParamStr(0)) + 'cef\cache\Cache');

         if not assigned(GlobalCEFApp) then
            GlobalCEFApp := TCefApplication.Create;

         GlobalCEFApp.FrameworkDirPath := extractFilePath(ParamStr(0)) + 'cef';
         GlobalCEFApp.ResourcesDirPath := extractFilePath(ParamStr(0)) + 'cef';
         GlobalCEFApp.LocalesDirPath := extractFilePath(ParamStr(0)) + 'cef\locales';
         GlobalCEFApp.cache := extractFilePath(ParamStr(0)) + 'cef\cache';
         GlobalCEFApp.UserDataPath := extractFilePath(ParamStr(0)) + 'cef\UserData';
         if not GlobalCEFApp.StartMainProcess then
            raise Exception.Create('');

      except
         { raise Exception.Create('Erro ao iniciar processo de CEF'); }
      end;
   end;

  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TdmComunicacao, dmComunicacao);
  Application.CreateForm(TfrmPrincipal, frmPrincipal);
  Application.Run;

  if Assigned(GlobalCEFApp) then
     GlobalCEFApp.Free;
PS: I'm using Delphi XE6/XE7

Re: Invalid window handle (Delphi)

Posted: Wed Mar 01, 2023 9:48 am
by salvadordf
Hi,

The GlobalCEFApp property values used in that code means that CEF will use the same executable for all the Chromium subprocesses.

Please, use the code from the demo that fits your use case as a template for your application.

Many demos use something like this in the DPR file :

Code: Select all

begin
  CreateGlobalCEFApp;

  if GlobalCEFApp.StartMainProcess then
    begin
      Application.Initialize;
      Application.MainFormOnTaskbar := True;
      Application.CreateForm(TMainForm, MainForm);
      Application.Run;
    end;

  DestroyGlobalCEFApp;
end.
CreateGlobalCEFApp is defined in a different unit in order to avoid some leaks when using GlobalCEFApp events.

If you need to delete the cache directory set GlobalCEFApp.DeleteCache to TRUE and the cache will be deleted automatically before initializing CEF.