Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.

Invalid window handle (Delphi)

Post Reply
ronaldolimajr
Posts: 1
Joined: Fri Feb 24, 2023 11:09 am

Invalid window handle (Delphi)

Post 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
User avatar
salvadordf
Posts: 4057
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Invalid window handle (Delphi)

Post 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.
Post Reply