Using Win32 API CreateProcess in FireBreath Framework - c++

I'm trying to develop a browser plugin using Firebreath framework. The first thing I would like to achieve is to make the plugin able to do traceroute. For now I'm doing it on Windows7. Currently I chose to use Win32API CreateProcess to call the command shell. By setting dwFlags = STARTF_USESHOWWINDOW , I'm able to hide the command shell window during execution.
PROBLEM : The createProcess is implemented in a method called run() where I called it using JS for testing. When I called plugin().run(), the traceroute is working well, and the output was succesfully written in a textfile as I wanted. However during the execution, the browser become unresponsive and lastly the plugin crashed seconds after the traceroute completed. As I am new to plugin development and only have a little knowledge on c++ , I wonder why this problem arisen. FYI, if I did not hide the commandshell window, the plugin worked wonder - the browser was responsive while the traceroute was executed.

It is very important in any NPAPI plugin (with FireBreath or otherwise) that you don't block the main (the javascript) thread. What you're trying to do could be done in a couple of ways; I'd probably pass in a callback, start a new thread, do the createprocess there, and then fire the js callback when it completes with the result.
See FireBreath Tips: Asynchronous Javascript Calls.
The one thing to watch out for is you need to be able to terminate the thread (and the process) if the plugin is shut down during the call.

Related

C++/WinRT GetGattServicesAsync() not responding in MFC app

C++, WinRT, VS2017 MFC, Win10
I have a C++/WinRT VS2017 console app as a test platform to find my Bluetooth LE device, enumerate the services and characteristics, and then write a value to the Tx characteristic, etc. I have all of that working and now I am trying to move that code to an existing VS2017 MFC app.
In the console app the BluetoothLEAdvertisementWatcher and callback to the watcher.Received() were done in the main.cpp. Once my BLE device was found, a separate function was called to create the device object from the deviceAddress and then enumerate the services and characteristics.
In the MFC app I created a separate function run in a separate thread to establish the watcher and attach the callback. That all works fine up to the point that it needs to GetGattServicesAsync(). In the console app the function OpenDevice() used to create the device object and get the services was done with a get() as in:
OpenDevice(deviceAddress).get();
The first thing OpenDevice() does is create the device object using
auto device = co_await BluetoothLEDevice::FromBluetoothAddressAsync(deviceAddress);
If the device object is created, then the next thing it did was get the Services with:
auto services = co_await device.GetGattServicesAsync();
Here is where my MFC code fails. In the function thread that creates the watcher and watcher.Received callback my MFC code does the same call to OpenDevice(). In OpenDevice the device object is indeed created but then the call to GetGattServicesAsync() will never finish so matter how long I wait. If I enter the GetGattServicesAsync() in Debug mode, however, then it works fine.
For testing I have also put the OpenDevice() code within the watcher thread but, again, it stalls on GetGattServicesAsync().
In this case, however, I cannot use the co_await but had to use
auto services = device.GetGattServicesAsync().get();
Regardless, the GetGattServicesAsync() never finished.
Any suggestions of what I need to do or what I am doing wrong?
Since running in Debug was working I was trying to solve why this was not working in Release mode. I was relying on my log file entries to track the progress after the fact. For the halibut I put in some Beeps() to let me hear the progress and I was hearing beeps but not seeing log entries after that GetGattServicesAsync(). Apparently what was happening is that, for what ever reason, the GetGattServicesAsync() was NULLing out the handle to my log file so no writes were taking place. I did a check for NULL after the GetGattServicesAsync() and, if it was, re-established the log file. All of a sudden my log file was showing the progress that I thought was not happening. Sometime I think it is just me...:-(

How to kill chromium embedded framework subprocess?

in a PC game I have ingame browser used for news, virtual currency shop and social networks. It's built with quite fresh update of Chromium Embedded Framework. The problem is when I open a browser window (website is working fine there) and then close, for certain websites CEF sub-process doesn't finish. I also may continue hearing audio, if it was Youtube video, for example. I use offscreen rendering, other native windows are not created, only subprocesses. To close the browser window I remove all references to CefBrowser and call:
m_browser->GetHost()->CloseBrowser(true);
I also tried other ways to close/destroy/finalize that render subprocess, such as loading 'about:blank' before closing, but that was no help: process stayed awake, audio continued playing.
Important note: it happens only on certain websites, which I suppose use some feature, that others don't. When I tried to disable JavaScript in CEF settings, the bug disappeared, but I need JS.
Is there a way to force kill browser subprocess? (Notice that GetWindowHandle returns 0, because it does not have a window)
Is there another way to correctly terminate browser which I don't know?
What feature of the websites may cause such bug?
Thank you!
CEF runtime configuration: multi-process, single threaded message loop, with subprocess path, windowless rendering, no sandbox.
PC configuration: OS Windows 8, VS 2010, Chromium Embedded Framework version 3.3071, build 1649, C++ language.
You should check your implementation of onbeforeunload.
CEF GeneralUsage writes about CefBrowserHost::CloseBrowser:
The parent window then needs to call CloseBrowser(false) and wait for a second OS close event to indicate that the browser has allowed the close. The second OS close event will not be sent if the close is canceled by a JavaScript ‘onbeforeunload’ event handler or by the DoClose() callback.
And if you still want to just kill the sub-process , I would suggest you use the browser IPC message and exit at the app.
At your game run
CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create(KILL_subprocess);
m_browser->SendProcessMessage(PID_RENDERER, msg);
and at the subprocess implement “OnProcessMessageReceived”:
if (msg->GetName() == KILL_subprocess)
{
delete this;
std::exit(EXIT_FAILURE);
}

Can an application run the code of another application?

Hi,
I'm relatively new to C++ and WinAPI. So far I've managed to create an application, that is using the CreateProcess function and a STARTUPINFO structure to create a new desktop, launch inside that new desktop a new explorer.exe process and switch to it.
Next, because I wanted to be able to switch at any time between these two desktops, at a press of a key (LCTRL in my case), I've made another application that uses the SetWindowsHookEx function to create a global hook for the keyboard.
Because the hook is active only in the calling destkop, in the first app, using CreateProcess, before creating the explorer.exe process and switching to the new desktop, i've launched the executable of the second app twice: once in the current desktop and once in the new one.
Everything is working fine, I'm able to make the switch between desktops at any time, but now I've been asked to do something about the structure of the processes launched, somehow, to make the seconds app code run inside the first one, without creating a new process. Because this is my first post, I can't upload a snippet of the process tree, but the procexp application from live.systernals is showing the following structure:
---FirstApp.exe:
-------------SecondApp.exe (original desktop)
-------------explorer.exe (new desktop)
-------------SecondApp.exe (new desktop)
So basically, my question is: can I make the code of the application that hooks the keyboard run in the same thread as the FirstApp? This implementation, an app that starts these three processes, and the second app that hooks the keyboard, was my idea (I was not requested to do it this way, I was only asked to create a new desktop and switch between them), so I am open to suggestions towards making a better implementation for this problem too.
It could be possible since there is little difference between a DLL and an EXE on Windows, so I think you could try to export the routines from SecondApp and then import them in FirstApp with LoadLibrary.
But IMHO the clean way to do that is to break SecondApp in two pieces : a DLL containing code that actually does the job and an EXE that would be a simple frontend calling routines from the DLL.
That way, it will be trivial (and portable across different versions of Windows and SDK) to call the routines of the DLL from FirstApp.

C++ / C call method from running software

I would like to use C++ / C to call a method of an running application. So it should work the following way:
An application is running, let's say Chrome and another application is running (plugin.exe). The other application should call a Chrome c++ method from the outside. Is that possible?
The method that I would like to call is the method displaying auto suggests. I would like to develop an omnibox with multiple keywords.
Somehow I would like to hook into the application and send / receive events and call methods. I guess event reeving can be done, since it's possible to use the Microsoft debugger with an executable and catch a lot of events.
While researching a bit I found out how to load a dll plugin in Chrome:
https://developer.chrome.com/extensions/npapi
I've done the same in Pepper API as well, but from there I can't reach any further.

watchdog in vc++ application

I have written a simple vc++ background application. What am trying is like a watchdog service that could monitor if the application is running or not. If the application crashed then the service should start the application
For creating a setup through windows installer am using only the app.exe and app.dll.
Is that possible to create this watchdog - service in the exe itself ?
Unfortunately I have no idea of how to write such a program, does anyone have some example code that would demonstrate this technique please?
if so then how to make the default exe and watchdog exe as a single application to install ?
Your best route would be to create a separate service to act as the watchdog. Technically, it's possible to have the service and the "real application" in the same executable. You can differentiate between the two depending on how the exe has been started, but it will make maintenance quite difficult.
This article might be of interest.
Here - http://yadi.sk/d/EtzBRSMi3FqVH - is my implementation of WatchDog app, working in systray. Do not mind that it's written with Qt - the main functionality is with WinAPI.
This app is watching in processes list for several processes and restarts them if can't find. The second feature is that it monitors all windows in system for suspicious window title (for ex. "'My Great App' causes a system error and will be closed. Send message to developers ?") and, if find, restarts them too
P.S. I didn't i18n it, but I think there will no troubles )
Update: (according to #CodyGray comment)
Here's pastebin's links: WatchDog.cpp and WatchDog.h
Such a watchdog can be set up to, for example, write to a file every minute (or whatever). If the file hasn't been updated in two or more minutes then there is most likely a deadlock in the application and it has to be restarted.