How can I send quit command to current ffmpeg.exe - javascript

var fpath="C:\\TVT_"+cur_date+"_"+cur_time+".avi";
Components.utils.import("resource://gre/modules/FileUtils.jsm");
var env = Components.classes["#mozilla.org/process/environment;1"]
.getService(Components.interfaces.nsIEnvironment);
var shell = new FileUtils.File(env.get("COMSPEC"));
var args = ["/c", "cd.. & cd.. & C: & cd C:/ffmpeg/bin & record.bat "+fpath];
var process = Components.classes["#mozilla.org/process/util;1"]
.createInstance(Components.interfaces.nsIProcess);
process.init(shell);
process.runAsync(args, args.length);
I don't want to use taskkill.exe, /MIN, /C, /B, rather I want to quit this ffmpeg process
I read about CMDOW but did not find cmdow.exe inside system32 directory.
So how can I send quit command within the same window which is running ffmpeg process?
Using Windows XP service pack 2 with Firefox 12
Thanks..

That's not quite trivial - Firefox doesn't have any built-in functionality for that meaning that you would need to use js-ctypes for that and call Win32 API functions directly. Win32 - Get Main Wnd Handle of application describes how you would get hold of the top-level window for an application, after that you can send WM_QUIT message to it. This approach actually works:
Components.utils.import("resource://gre/modules/ctypes.jsm");
var userlib = ctypes.open("user32");
var HWND = ctypes.voidptr_t;
var UINT = ctypes.uint32_t;
var WPARAM = ctypes.uint16_t;
var LPARAM = ctypes.uint32_t;
var LRESULT = ctypes.uint32_t;
var DWORD = ctypes.uint32_t;
var WNDENUMPROC = ctypes.FunctionType(ctypes.stdcall_abi,
ctypes.bool,
[HWND, LPARAM]).ptr;
var WM_CLOSE = 0x0010;
var EnumWindows = userlib.declare(
"EnumWindows", ctypes.winapi_abi,
ctypes.bool,
WNDENUMPROC, LPARAM
);
var GetWindowThreadProcessId = userlib.declare(
"GetWindowThreadProcessId", ctypes.winapi_abi,
DWORD,
HWND, DWORD.ptr
);
var IsWindowVisible = userlib.declare(
"IsWindowVisible", ctypes.winapi_abi,
ctypes.bool,
HWND
);
var SendMessage = userlib.declare(
"SendMessageW", ctypes.winapi_abi,
LRESULT,
HWND, UINT, WPARAM, LPARAM
);
var callback = WNDENUMPROC(function(hWnd, lParam)
{
var procId = DWORD();
GetWindowThreadProcessId(hWnd, procId.address());
if (procId.value == pid && IsWindowVisible(hWnd))
SendMessage(hWnd, WM_CLOSE, 0, 0);
return true;
});
EnumWindows(callback, 0);
userlib.close();
I tested this and could successfully close a Notepad window with it (the usual warning will appear if the text hasn't been saved so it is a clean shutdown). In your case the problem might be however that you aren't running your ffmpeg directly but rather via the command line shell - so you will close the command line window which might not terminate ffmpeg. I guess you will just have to try, if it doesn't work you will probably have to look at the title of the window instead of its process ID (which is obviously a less reliable approach).

Related

JavaScript immediately closes after opening it

I am working on a Raspberry3 model B.
I've written a code that I want to launch on reboot.
If I launch the script in the bash it works perfectly. But when I try to start the script via doubleclick (execute in terminal) it opens the terminal for a very short duration and closes it immediatly after.
Same thing happens if I want to start this script at reboot.
Can anyone tell me what I'm doing wrong?
var blynkLib = require('blynk-
library');
var sensorLib = require('node-dht-
sensor');
var AUTH = 'xxx';
// Setup Blynk
var blynk = new
blynkLib.Blynk(AUTH);
// Setup sensor, exit if failed
var sensorType = 22; // 11 for DHT11, 22 for DHT22 and AM2302
var sensorPin = 2; // The GPIO pin number for sensor signal
if
(!sensorLib.initialize(sensorType,
sensorPin)) {
console.warn('Failed to
initialize sensor');
process.exit(1);
}
// Automatically update sensor value every 2 seconds
setInterval(function() {
var readout = sensorLib.read();
blynk.virtualWrite(3,
readout.temperature.toFixed(1));
blynk.virtualWrite(4,
readout.humidity.toFixed(1));
console.log('Temperature:',
readout.temperature.toFixed(1) +
'C');
console.log('Humidity: ',
readout.humidity.toFixed(1) +
'%');
}, 2000);
Assuming your question is How can i pause my program :
using python : you should import os then os.system("pause"); :
import os;
os.system("pause");
Using nodejs : use one of the module from npm :
https://www.npmjs.com/package/system-sleep
https://www.npmjs.com/package/pause

Why does Node hang when using deasync with x11 events?

I'm trying to use the node modules deasync and x11 to perform actions when certain keys are pressed.
When I use deasync inside a callback that has been initiated by a keypress deasync seems to be stuck in an endless loop.
It works fine if I create a generic event myself.
Run the following script using xtrace to see that X11 does respond:
xtrace -D :10 ./the-script
#!/usr/bin/env node
var deasync = require('deasync');
var x11 = require('x11');
var display = (deasync(x11.createClient)());
var client = display.client;
var getInputFocus = deasync(client.GetInputFocus)
.bind(client);
var focus1 = getInputFocus();
console.log("getting focus here works:", focus1);
// grab the "1"-key - keyCode = 10
client.GrabKey(display.screen[0].root, 0, null, 10, 0, 1);
client.on('event', processKeyPressEvent);
// client.emit("event"); // works
function processKeyPressEvent(event) {
console.log("can see this");
var focus2 = getInputFocus(); // problem
console.log("never get here");
}
Thanx for your help.

Nodejs/Javascript Getting Process Memory of any process

I am looking for a way of getting the process memory of any process running.
I am doing a web application. I have a server (through Nodejs), my file app.js, and an agent sending information to app.js through the server.
I would like to find a way to get the process memory of any process (in order to then sending this information to the agent) ?
Do you have any idea how I can do this ? I have searched on google but I haven't found my answer :/
Thank you
PS : I need a windows compatible solution :)
Windows
For windows, use tasklist instead of ps
In the example below, i use the ps unix program, so it's not windows compatible.
Here, the %MEM is the 4st element of each finalProcess iterations.
On Windows the %MEM is the 5th element.
var myFunction = function(processList) {
// here, your code
};
var parseProcess = function(err, process, stderr) {
var process = (process.split("\n")),
finalProcess = [];
// 1st line is a tab descriptor
// if Windows, i should start to 2
for (var i = 1; i < process.length; i++) {
finalProcess.push(cleanArray(process[i].split(" ")));
}
console.log(finalProcess);
// callback to another function
myFunction(finalProcess);
};
var getProcessList = function() {
var exec = require('child_process').exec;
exec('ps aux', parseProcess.bind(this));
}
// thx http://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript
function cleanArray(actual){
var newArray = new Array();
for(var i = 0; i<actual.length; i++){
if (actual[i]){
newArray.push(actual[i]);
}
}
return newArray;
}
getProcessList();

Getting special Folder path for a given user in Jscript

How to get the path of a shell folder like "Local Settings" or "Local Appdata" for a specific user other than the current user?
While there are methods for getting special folder paths in Windows Script Host — WshShell.SpecialFolders and Shell.NameSpace — they return paths for the current user only. Getting other users' special folder paths is a bit tricky.
The proper way to do this is to use the Windows API SHGetKnownFolderPath function (or SHGetFolderPath on Windows versions prior to Vista). But the problem is, Windows Script Host doesn't support calling WinAPI functions, so to make use of these functions in your script you'll have to expose them via a custom-written COM component.
Another possible but undocumented solution is to read the special folder paths from that user's registry hive, specifically, the HKEY_USERS\<user_SID>\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders key.
The paths in the User Shell Folders key are typically specified using the %USERPROFILE% environment variable; so to get fully-qualified paths you'll have to substitute this variable with the ProfileImagePath value from the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\<user_SID> key.
Also, the HKEY_USERS\<user_SID> key is only available when the corresponding user is currently logged on. For a general solution, you would have to load the user's hive (<UserProfile>\ntuser.dat) into a temporary registry key (say, HKEY_USERS\Temp) and read values from this key instead.
Below is sample JScript code that demonstrates how your task can be accomplished. On Windows 7 and Vista, you may need to run the script as Administrator depending on your UAC settings.
NOTE: This method is discouraged, as Raymond Chen explains in his article The long and sad story of the Shell Folders key. There's no guarantee it will keep working in future versions of Windows.
var strUser = "foo";
var strDomain = "bar";
// If the account is local, domain name = computer name:
// var strDomain = getComputerName();
var strSID = getSID(strUser, strDomain);
var strProfilePath = getProfilePath(strSID);
// Load the user's registry hive into the HKEY_USERS\Temp key
var strTempKey = "Temp";
loadHKUHive(strTempKey, strProfilePath + "\\ntuser.dat");
// Get unexpanded path, e.g. %USERPROFILE%\AppData\Roaming
//var strAppData = getAppData(strSID);
var strAppData = getAppData(strTempKey);
WScript.Echo(strAppData);
// Expand the previous value to a fully-qualified path, e.g. C:\Users\foo\AppData\Roaming
strAppData = strAppData.replace(/%USERPROFILE%/i, strProfilePath);
WScript.Echo(strAppData);
// Unload the user's registry hive
unloadHKUHive(strTempKey);
function getComputerName() {
var oShell = new ActiveXObject("WScript.Shell");
return oShell.ExpandEnvironmentStrings("%COMPUTERNAME%");
}
function getSID(strUser, strDomain) {
var oAccount = GetObject("winmgmts:root/cimv2:Win32_UserAccount.Name='" + strUser + "',Domain='" + strDomain + "'");
return oAccount.SID;
}
function getProfilePath(strSID) {
var oShell = new ActiveXObject("WScript.Shell");
var strValue = oShell.RegRead("HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\" + strSID + "\\ProfileImagePath");
return strValue;
}
function getAppData(strSID) {
var oShell = new ActiveXObject("WScript.Shell");
var strValue = oShell.RegRead("HKEY_USERS\\" + strSID + "\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders\\AppData");
return strValue;
}
function loadHKUHive(strKeyName, strHiveFile) {
var oShell = new ActiveXObject("WScript.Shell");
oShell.Run("reg load HKU\\" + strKeyName + " " + strHiveFile, 0, true);
}
function unloadHKUHive(strKeyName) {
var oShell = new ActiveXObject("WScript.Shell");
oShell.Run("reg unload HKU\\" + strKeyName, 0, true);
}

How to Run an External jar file from within a firefox extension

Here is the code I have so far...
// Run the external encryption process
var fileExe = Components.classes["#mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
fileExe.initWithPath("~/tmp/Encrypt.jar");
var process = Components.classes["#mozilla.org/process/util;1"]
.createInstance(Components.interfaces.nsIProcess);
process.init(fileExe);
var args = ["java -jar Encrypt.jar -e toEncrypt"];
process.run(true, args, args.length);
document.getElementById('hello-world-status-bar-icon').label = "DONE";
This currently does not work. Any suggestions??
EDIT
I've also tried..
// Run the external encryption process
var fileExe = Components.classes["#mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
fileExe.initWithPath("java");
var process = Components.classes["#mozilla.org/process/util;1"]
.createInstance(Components.interfaces.nsIProcess);
process.init(fileExe);
var args = new Array();
args[0] = " -jar";
args[1] = "~/tmp/Encrypt.jar";
args[2] = "-e";
args[3] = "toEncrypt";
process.run(true, args, args.length);
document.getElementById('hello-world-status-bar-icon').label = "DONE";
Thanks,
Pat
I think you need to init the process with a reference to the local file that is the "java" executable. That's what needs to be executed at the system level. The arguments need to be passed as an array of individual strings, not a single string.

Categories