CefMainArgs with argc and argv on Windows

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

CefMainArgs with argc and argv on Windows

Postby BenjiSBRK » Tue Jan 28, 2014 10:09 am

On different tutorials I see CefMainArgs is used with argc and argv, but the CefMainArgs takes HINSTANCE instead. I am working on cross-platform code that still uses argc and argv, even on Windows, so what should I pass to CefMainArgs on Windows when I have argc and argv ? For the moment I pass an empty CefMainArgs to CefExecuteProcess.

Also, I'm getting errors from CEF in the console, that I think might have a link with this. The first one is

[0128/155648:ERROR:renderer_main.cc(223)] Running without renderer sandbox


The second one is
[0128/155649:ERROR:singleton_hwnd.cc(44)] Cannot create windows on non-UI thread


I'm guessing it has to do with a child process not returning before I instanciate a window on my app, but I do check the return value of CefExecuteProcess and returns when the value is >= 0. If I remove the return, the result is the same.
I remember at some point I didn't have these errors anymore, but then I refactored my code again and they came back.
BenjiSBRK
Techie
 
Posts: 35
Joined: Tue Jan 21, 2014 9:59 am

Re: CefMainArgs with argc and argv on Windows

Postby magreenblatt » Wed Jan 29, 2014 10:25 am

The HINSTANCE allows Chromium to pull command-line arguments from the executable's command-line. You can usually pass GetModuleHandle(NULL) to CefMainArgs and get the same result. If that doesn't work you can populate the command-line manually in CefApp::OnBeforeCommandLineProcessing.

The errors are nothing to worry about and are removed in release branches. Alternately, you can enable the sandbox with the current CEF trunk.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: CefMainArgs with argc and argv on Windows

Postby BenjiSBRK » Wed Jan 29, 2014 1:02 pm

Ok thank you. Another thing that annoys me and is probably abnormal is that when I stop the program in debug with the stop button of Visual Studio, the program displays some errors then crashes (and doesn't stop).
I'm getting the following errors once I stop the execution:

[0129/185851:ERROR: render_process_impl.cc(94)] WebFrame LEAKED 1 TIMES
[0128/185851:FATAL:v8_impl.cc(79)] Check failed: context_map_.empty().


If I close the program with the close button, everything closes normally.
BenjiSBRK
Techie
 
Posts: 35
Joined: Tue Jan 21, 2014 9:59 am

Re: CefMainArgs with argc and argv on Windows

Postby magreenblatt » Wed Jan 29, 2014 1:19 pm

BenjiSBRK wrote:Ok thank you. Another thing that annoys me and is probably abnormal is that when I stop the program in debug with the stop button of Visual Studio, the program displays some errors then crashes (and doesn't stop).

Don't do that. CEF3 has multiple processes that won't shutdown correctly in that case.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: CefMainArgs with argc and argv on Windows

Postby ScottD » Fri Feb 03, 2017 9:29 pm

magreenblatt wrote:The HINSTANCE allows Chromium to pull command-line arguments from the executable's command-line. You can usually pass GetModuleHandle(NULL) to CefMainArgs and get the same result. If that doesn't work you can populate the command-line manually in CefApp::OnBeforeCommandLineProcessing.


I'm in the same boat. I tried to implement this but it doesn't seem to work. I know it's untested and not to be used, but if I set single_process = true my app will at least load the web page I requested. Using a subprocess I basically get random results.

Here's what I've got:

cefRunner.cpp
Code: Select all
#include <iostream>
#include <string>

#include "cef_app.h"
#include "cef_client.h"


class AppHandler : public CefApp {
   public:
      void SetArgs(int argc, char *argv[]) {
         _argc = argc;
         _argv = argv;
      }

      void OnBeforeCommandLineProcessing(const CefString &processType, CefRefPtr<CefCommandLine> commandLine) {
         (void)processType;

         std::string temp;

         for (int i=0; i<_argc; i++) {
            if (i > 0) temp = temp.append(" ");
            temp = temp.append(_argv[i]);
         }

         CefString allArgs(temp);
         commandLine->InitFromString(allArgs);
      }

   private:
      int    _argc;
      char **_argv;

   IMPLEMENT_REFCOUNTING(AppHandler)
};


int main(int argc, char *argv[]) {

   CefMainArgs cefArgs;
   CefRefPtr<AppHandler> appHandler = new AppHandler();

   appHandler->SetArgs(argc, argv);
   int r = CefExecuteProcess(cefArgs, appHandler, nullptr);

   return r;
}


Is that even close to being the proper way to pass an argc, *argv[] pair on Windows?
ScottD
Techie
 
Posts: 20
Joined: Mon Jan 30, 2017 5:22 pm
Location: Smithton, IL, USA, Earth

Re: CefMainArgs with argc and argv on Windows

Postby ScottD » Sat Feb 04, 2017 5:45 pm

For "fun" I rewrote my cefRunner.cpp subprocess app to look like this:

cefRunner.cpp
Code: Select all
#ifdef _WIN32
#define VC_EXTRALEAN
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif

#include <iostream>
#include <fstream>

#include "cef_app.h"
#include "cef_client.h"

#define LOGNAME "cefRunner.log"


void hackyLogging(const char *logLine) {
   std::ofstream logFile(LOGNAME, std::ios_base::app | std::ios_base::out);
   logFile << logLine << std::endl;
   std::cout << logLine << std::endl;
}


#ifdef _WIN32
int CALLBACK WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow) {
   (void)hPrev;
   (void)szCmdLine;
   (void)nCmdShow;
   CefMainArgs cefArgs(hInst);
#else
int main(int argc, char *argv[]) {
   CefMainArgs cefArgs(argc, argv);
#endif

   hackyLogging("{-DEBUG-} Starting cefRunner.");
   int r = CefExecuteProcess(cefArgs, nullptr, nullptr);
   hackyLogging("{-DEBUG-} Stopping cefRunner.");

   return r;
}


I see in the log that it is started up twice by the main application. I still don't see it loading anything. Maybe I don't understand what all needs implemented in the stub application? I'm trying to run JS and do OSR.

Thanks.
ScottD
Techie
 
Posts: 20
Joined: Mon Jan 30, 2017 5:22 pm
Location: Smithton, IL, USA, Earth

Re: CefMainArgs with argc and argv on Windows

Postby ScottD » Sat Feb 04, 2017 6:23 pm

This is turning into another issue. Moving to it's own thread. http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=14881
ScottD
Techie
 
Posts: 20
Joined: Mon Jan 30, 2017 5:22 pm
Location: Smithton, IL, USA, Earth


Return to Support Forum

Who is online

Users browsing this forum: Majestic-12 [Bot] and 46 guests