Page 1 of 1

Re: Problem having dynamic CEF path

Posted: Thu Feb 08, 2018 9:31 pm
by salvadordf
Hi,

The SimpleBrowser and SimpleBrowser2 demos have all you need to load the CEF libraries from a custom path.

Edit the DPR file and you will see these lines :

Code: Select all

  GlobalCEFApp.FrameworkDirPath     := 'cef';
  GlobalCEFApp.ResourcesDirPath     := 'cef';
  GlobalCEFApp.LocalesDirPath       := 'cef\locales';
Activate those 3 lines and set the directory where you copied the CEF binaries. You can use absolute or relative paths in those properties.

You need to copy the contents of the Release and Resources directories.

Remember that all the executables must be developed for the same CEF binaries version, branch and build number. If you try to run a CEF based application made for a different CEF version/branch/build you may have all kind of problems.

Don't try to use the same cache, cookies or UserDataPath in several executables at the same time. CEF doesn't allow to share those directories with other applications.

Re: Problem having dynamic CEF path

Posted: Fri Feb 09, 2018 9:56 am
by salvadordf
You are using the same executable for all the CEF processes : main browser process, render processes, etc.

In this configuration your application will run your EXE multiple times and that means that the code inside the DPR will be run for the main browser process and also for each subprocess.

The most simple code inside a DPR would be like this :
(I added some line numbers to make it clear)

Code: Select all

(1)  GlobalCEFApp := TCefApplication.Create;

(2)  if GlobalCEFApp.StartMainProcess then
       begin
(3)      Application.Initialize;
(4)      Application.CreateForm(TForm1, Form1);
(5)      Application.Run;
       end;

(6)  GlobalCEFApp.Free;
When a user runs your application the PC will execute 1, 2, 3 ,4 and 5. Then the app stays inside line 5 until the user closes the app and then line 6 is executed.

While the app is running it may create several subprocesses that will execute 1 and 2. In this case CEF detects that this is a subprocess and it will stay inside line 2 until the process is closed. When that subprocess is closed then GlobalCEFApp.StartMainProcess returns FALSE and line 6 is executed.

CEF uses many command line parameters to launch each subprocess that might be incorrectly interpreted. Use unique name-value pairs to identify your parameters and move the code to interpret them between the "begin" and line 3.

If you want to show error messages, check that GlobalCEFApp.ProcessType is ptBrowser and then check GlobalCEFApp.Status to show customized messages in case it has one of these values : asErrorMissingFiles, asErrorDLLVersion, asErrorLoadingLibrary, asErrorInitializingLibrary, asErrorExecutingProcess. If some binaries are missing then GlobalCEFApp.MissingLibFiles will have their names.

Re: Problem having dynamic CEF path

Posted: Fri Feb 09, 2018 11:46 am
by salvadordf
Use these functions from uCEFMiscFunctions to check if the CEF binaries are present :
  • CheckDLLs : Used to check the files in GlobalCEFApp.FrameworkDirPath.
  • CheckResources : Used to check the files in GlobalCEFApp.ResourcesDirPath.
  • CheckLocales : Used to check the files in GlobalCEFApp.LocalesDirPath.
Use them before creating GlobalCEFApp and don't create GlobalCEFApp is any of them returns false.

Those functions are used in TCefApplication.CheckCEFLibrary when GlobalCEFApp.CheckCEFFiles is true but you can use them before you create GlobalCEFApp.

If the user doesn't need a web browser then don't create GlobalCEFApp and don't call GlobalCEFApp.StartMainProcess

Re: Problem having dynamic CEF path

Posted: Fri Feb 09, 2018 12:58 pm
by salvadordf
Yes, you can let the user select an absolute or relative path and then save it in the windows registry as a string.

The next time your app is executed it would read the path from the windows registry and then check if the CEF binaries are present in that path.

If the binaries are present then it can create GlobalCEFApp and set GlobalCEFApp.FrameworkDirPath, GlobalCEFApp.ResourcesDirPath and GlobalCEFApp.LocalesDirPath with the path you read from the registry.

Re: Problem having dynamic CEF path

Posted: Fri Feb 09, 2018 2:57 pm
by salvadordf
As far as I know, you can't add regular command line parameters to the subprocesses like the ones you used in the previous code to set the CEF path.

You must use alternative methods to initialize GlobalCEFApp with the right path like :
  • A windows registry entry.
  • An INI file.
  • A custom configuration file.
PS : I said "regular" command line parameters because there's a way to pass command line switches to CEF with the GlobalCEFApp.AddCustomCommandLine function but it's not the same.

Re: Problem having dynamic CEF path

Posted: Sat Feb 10, 2018 1:00 pm
by salvadordf
After further inspection I found that you can add custom command line parameters to the subprocesses using the GlobalCEFApp.OnBeforeChildProcessLaunch event.

Re: Problem having dynamic CEF path

Posted: Fri Oct 09, 2020 10:42 am
by maxedi
Thanks for this thread, it also helped me to achieve something like OP was trying to do. I just want to elaborate steps in order to anyone else would implement something like this.

So, the main objective is to pass the path to CEF binaries and allow everything to work smoothly.
  • invent some very-unlikely-to-meet parameter name, like --myveryrareparameterpointingtocef
  • run your program with this parameter, something like --myveryrareparameterpointingtocef=C:\CEF\Binaries
  • your code should iterate through every ParamStr values and search for your parameter. If found - initialize CEF and run StartMainProcess
  • Catch TAclCefApplication.OnBeforeChildProcessLaunch and make the call to method commandLine.AppendSwitchWithValue with your parameter and name, in the case above it's commandLine.AppendSwitchWithValue('--myveryrareparameterpointingtocef', 'C:\CEF\Binaries')
After this the host with a dynamically passed path to CEF binaries should work.