Passing a variable in System() c++ - c++

if (t.elapsed_millisecs() > 500) {
system(("powershell(Get - WmiObject - Namespace root / WMI - Class WmiMonitorBrightnessMethods).WmiSetBrightness(1, " + brightness + string(")")).c_str());
t = {};
}
brightness is just an int that can be set from 1 to 100
I'm trying to change the brightness over a certain amount of time but I'm having problem with system() function. When the program tries to execute the system() function, the cmd outputs " '_' is not recognized as an internal or external command, operable program or batch file."
My question is how do you pass a variable to the system function correctly?

You are taking liberty with spaces in your example.
This one runs OK:
int brightness = 42;
string s = "powershell(Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1, ";
s += to_string(brightness);
s += ")";
cout << s;
system(s.c_str());
It doesn't change the brightness though, but I don't know WMI...
I can run this in PowerShell console:
$myMonitor = Get-WmiObject -Namespace root\wmi -Class WmiMonitorBrightnessMethods
$myMonitor.wmisetbrightness(3,50)
Same result - no errors, no effect.
UPDATE:
It works! (I was testing over Remote Desktop at first)

Related

how to change os.system() call to subprocess.call() for below code

Need to know how i can convert below code from os.system to subprocess.call with shell=False
Code to be modified:
command1="ls -lrt"
command2="cat file.txt"
cucDBServiceStartRC = os.WEXITSTATUS(os.system(command1 + " && " + command2))
if(cucDBServiceStartRC!=0);
do something..
I Tried:
command1="ls -lrt"
command2="cat file.txt"
cucDBServiceStartRC = os.WEXITSTATUS(subprocess.call(shlex.split(command1 + " && " + command2),shell=False))
if(cucDBServiceStartRC!=0);
do something..
But command fails to compile.
Note: I want to do it with shell=False, so i need workaround for && ( used in above code for os.system) to be used in subprocess to run 2 commands at a time.
&& is a shell operator. If you want to run two programs without a shell inbetween, you need to run subprocess.call twice:
returnCode = subprocess.call(shlex.split(command1), shell=False);
if returnCode == 0:
returnCode = subprocess.call(shlex.split(command2), shell=False);
// Do whatever with returnCode. A value of 0 means either command failed.
It would be better, not to split the command line with shlex.split. Instead keep the executable name separate from the arguments from the beginning. Otherwise, if the user can influence the content of command1 or command2, you will have the same security concerns as with shell=True.

Invoking a method thtough C#

I am trying to get some data from the UWF_Volume WMI provider. Please see the following link,
https://msdn.microsoft.com/en-us/library/jj979756(v=winembedded.81).aspx
More specifically I am trying to get the exclusion files using the following class,
UInt32 GetExclusions([out, EmbeddedInstance("UWF_ExcludedFile")] string ExcludedFiles[]);
I am not familiar with out parameters but from a research I can understand that acts as a reference argument. So I wrote the following method,
public void getUWFExclusions()
{
{
string computer = ".";
ManagementScope scope = new ManagementScope(#"\\" + computer + #"\root\standardcimv2\embedded");
ManagementClass cls = new ManagementClass(scope.Path.Path, "UWF_Volume", null);
foreach (MethodData m in cls.Methods)
{
richTextBox1.AppendText("The class contains this method:" + m.Name + "\n");
}
ManagementBaseObject outParams;
foreach (ManagementObject mo in cls.GetInstances())
{
outParams = mo.InvokeMethod("GetExclusions", null, null);
richtextbox1.appendtext(string.format("ExcludedFiles" + mo[ExcludedFiles]));
}
}
catch (Exception e)
{
richTextBox1.AppendText(e.ToString());
}
}
The problem is that the line,
richtextbox1.appendtext(string.format("ExcludedFiles" + mo[ExcludedFiles]));
returns "Not Found"
I appreciate any help to debug this problem.
I guess you are missing the Object Query that actually gets data via call to WMI. I am not sure about windows 8 but till 7 we used to get data by WQL and that is not there in above code.

Sahi: _include in if statements

I'm trying to run the following code as a sahi script:
_include("initialScript.sah");
_include("secondScript.sah");
function currentTime(){
var $current = new Date();
var $hours = $current.getHours();
var $minutes = $current.getMinutes();
if ($minutes < 10){
$minutes = "0" + minutes;
}
if($hours > 11){
_log("It is " + $hours + ":" + $minutes + " PM");
}
else {
_log("It is " + $hours + ":" + $minutes + " AM");
}
if($hours >= 8 || $hours =< 20) {
_include("aScript.sah");
_include("anotherScript.sah");
...
}
else {
//do nothing and continue below
}
}
_include("yetMoreScripts.sah");
...
Simply put, I have a block of various scripts, followed by a check of the current time.
If it isn't between 8am and 8pm, the included block of scripts is skipped and the others below are executed. The _logs tell me that getting the time appears to work as intended.
Yet whenever I attempt to run the script as is, I get an immediate failure and completely unrelated Syntax Errors (such as on an _include way further down that is not faulty at all). Taking the _includes out of the if Statement seems to make the errors stop. For all I know this should work but just doesn't. Does anybody have experience with something similar and could give me a hint as to where I made a mistake?
as far as I can tell, this should work. A simple test:
test1.sah:
_log("ok");
test2.sah:
if (true) {
_include("test1.sah");
}
When I run this from the Sahi Controller, I get an "ok" logged. Maybe recreate this test and check if you get the same results.
Maybe it's the ";" missing after your includes?
Are you passing dynamic values to include? like _include($path)?

Controlling Program Flow Between VB and C++

I have a program that runs from VB/Excel and executes a C++ program in the middle of it. I have two (I think related) questions:
I capture the return value from the C++ program when it executes, but the number I get isn't zero (it's a 4-digit integer value instead, sample values I've received are 8156, 5844, 6100, 5528). I am certain the program exits normally with code 0, but I think VB is getting its value before the C++ program has completed its execution - would that explain why I am not getting a value of zero, and how I can get the final, correct return value from my C++ program?
[Probably as a solution to #1] How can I make the VB program "pause" until the C++ model has completed its execution? I need to do some additional VB work (output configuration based on the C++ model run) once the model is complete
Here is my VB code for how the model call. I am running a full-compiled C++ program through the windows shell.
'---------------------------------------------------------
' SECTION III - RUN THE MODEL AS C++ EXECUTABLE
'---------------------------------------------------------
Dim ModelDirectoryPath As String
Dim ModelExecutableName As String
Dim ModelFullString As String
Dim ret As Long
ModelDirectoryPath = Range("ModelFilePath").value
ModelExecutableName = Range("ModelFileName").value
ModelFullString = ModelDirectoryPath & ModelExecutableName
' Call the model to run
Application.StatusBar = "Running C Model..."
ModelFullString = ModelFullString & " " & ScenarioCounter & " " & NumDeals _
& " " & ModelRunTimeStamp
ret = Shell(ModelFullString)
' Add error checking based on return value
' This is where I want to do some checks on the return value and then start more VB code
1) You are capturing the Task ID of the program (this is what Shell() returns) not any return from the opened programme - that is why it is a 4 digit number
2) Shell() runs all programs asychronously.
To run a program synchronously or to run it and wait for the return, either:
Use a Windows API function (I refer you to https://stackoverflow.com/a/5686052/1101846 for a list of options / API calls you could use)
Much more easily, use the WshShell object provided by Windows Scripting Host (see https://stackoverflow.com/a/8906912/1101846 for more examples than what I give below). See Microsoft documentation of the Run method at http://msdn.microsoft.com/en-us/library/d5fk67ky
Essentially, do something like:
Set o = CreateObject("WScript.Shell")
valueReturnedFromYourProgram = o.Run( _
strCommand:="notepad", _
intWindowStyle:=1,
bWaitOnReturn:=true)
Debug.Print valueReturnedFromYourProgram
I'm not sure what the VBA code would be but have you checked out ShellExecuteEx? Below is the C/C++ code:
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "c:\\MyProgram.exe";
ShExecInfo.lpParameters = "";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);

QT UTC linux Clock skew detected. Your build may be incomplete

I have a code which I am writing it in QT (C++) where I want to display a date on a dateTime display on the GUI.. I get the time from another component and then I parse it into argument list and then I use Qprocess to run date - u command .. anyhow it seems like my make doesn't like what I am doing because it prints out this error:
QT UTC linux Clock skew detected. Your build may be incomplete.
the whole purpose of what I am doing is to update the UTC time of my linux computer according to the value I am getting and then gets that date using QT code to get the computer UTC time
here is part of the code :
if(month<10)
{
argument = "0"+ argument + QString::number(month);
}
else
{
argument = argument + QString::number(month);
}
if(day<10)
{
argument = "0"+ argument + QString::number(day);
}
else
{
argument = argument + QString::number(day);
}
if(hourUTC<10)
{
argument = "0" + argument + QString::number(hourUTC);
}
else
{
argument = argument + QString::number(hourUTC);
}
if(minUTC<10)
{
argument = "0"+ argument + QString::number(minUTC);
}
else
{
argument = argument + QString::number(minUTC);
}
argument = argument + QString::number(year);
argument = argument + ".";
if(secUTC<10)
{
argument = "0"+ argument + QString::number(secUTC);
}
else
{
argument = argument + QString::number(secUTC);
}
//argument = argument + QString::number((qint32)qRound(msUTC/ 1000.0));
argumentlist << argument;
qDebug() << argumentlist;
QProcess* proc = new QProcess();
// Change system date and time "date -u MMDDhhmmYYYY.ss" in tha application the date is created dynamically
proc->start(program, argumentlist);
proc->waitForFinished();
m_pOi->setTime();
any idea how can I force the change in time to my compute ? and yea I do run the code as a super user !
I think this error happens when your file times are newer than your system clock. make warns you that it may not be building everything correctly because of this. touching all your files should sort out the problem.
One of the causes is an SCM system that preserves file times and is ahead of your system clock.