Changing the Network adapter name Programmatically in windows with Qt - c++

I know that it is possible to change the network adapter in windows using the netsh command in console. but I need to do this in Qt using C++. I have tried to use QProcess to call netsh but command prompt needs admin right. also there is space in my adapter name that make it also not easy to run. Is there any library in Qt capable of changing Network adapter name without using QProcess and calling windows commands in it?
Update 1:
When I run the program with admin right and set the \" for having " inside of my string I can change the adapter name, using QProcess and netsh command.
QProcess p;
QString pi_network = "original adapter name";
p.start("netsh interface set interface name = \""+pi_network+"\" newname = \"MyAdapter\"");

I will post it as an answer, because it might help someone (as MrEricSir said in the comments).
you can use netsh command, but be aware that you should run your program with administrator right in windows.
QProcess p;
QString pi_network = "original adapter name";
p.start("netsh interface set interface name = \""+pi_network+"\" newname = \"new network adapter name\"");
p.waitForFinished();
p.close();

Related

Execute /bin/login from QProcess

I'm working on embedded system running linux (without X11 server). I want my Qt application to be started with root privileges then from within spawn new QProcess which should be "/bin/login user_name" then provide password.
The problem is the "Password:" that usually prints on console (I assume it's stdout) doesn't show up, and QProcess.write() doesn't seem to work at all. If I run this QProcess as normal user I get an error message at redirected stderr (via setStandardErrorFile). I think I have general problem with understanding /bin/login.
I tried to
echo "userPassword" > /proc/LOGIN_PID/fd/0
from different console, but it also doesn't work.
The function I wrote in c++ goes like this:
void BashWrapper::bashEcho(QStringList t_echo){
QString bash = "/bin/login";
QStringList args(t_echo[0]);
static QProcess *newProc = new QProcess();
newProc->setStandardOutputFile("/dev/pts/2");
newProc->setStandardErrorFile("/dev/pts/2");
newProc->start(bash,args);
if(newProc->waitForStarted()){
qDebug() << newProc->state();
}
newProc->write(t_echo[1].toUtf8());
if(!newProc->waitForFinished(10000)){
qDebug() << "timeout";
}
else{
qDebug() << "finished";
}
}
t_echo contains ("user_name", "userPassword")
What I want to do is spawn new program (let's say /bin/myApp) as user_name. I thought that login as that user and setting .bash_profile to run /bin/myApp would be easy, painless and... secure?
If using /bin/login from application is impossible is there any simple way to this?
Best Regards,
MichaƂ
Ok, I found the answer here:
https://www.thegeekstuff.com/2010/10/expect-examples/ There is an example script that executes su command, simply changing it to /bin/login allowed me to pass the password via some addtional scripts.

Inject Jar and replace classes in running JVM

I want to be able to replace and add some classes to an already running JVM. I read that I need to use CreateRemoteThread, but I don't completely get it. I read this post on how to do it (Software RnD), but I can't figure out what it does and why. Besides that, it only introduces new classes, but doesn't change existing ones. How can I do it with C++?
You don't even need CreateRemoteThread - there is an official way to connect to remote JVM and replace loaded classes by using Attach API.
You need a Java Agent that calls Instrumentation.redefineClasses.
public static void agentmain(String args, Instrumentation instr) throws Exception {
Class oldClass = Class.forName("org.pkg.MyClass");
Path newFile = Paths.get("/path/to/MyClass.class");
byte[] newData = Files.readAllBytes(newFile);
instr.redefineClasses(new ClassDefinition(oldClass, newData));
}
You'll have to add MANIFEST.MF with Agent-Class attribute and pack the agent into a jar file.
Then use Dynamic Attach to inject the agent jar into the running VM (with process ID = pid).
import com.sun.tools.attach.VirtualMachine;
...
VirtualMachine vm = VirtualMachine.attach(pid);
try {
vm.loadAgent(agentJarPath, options);
} finally {
vm.detach();
}
A bit more details in the article.
If you insist on using C/C++ instead of Java API, you may look at my jattach utility.

find list of all accessible hostnames

I have an application where a user can dynamically configure TCP connections between remote processes. I'd like to make sure the user input is valid, by providing them with a QComboBox that is pre-populated with all the valid hostnames on their network. Is there a way to find the list of hostnames using Qt?
If possible I'd like to do this on both windows and linux.
This can be achieved using Qt classes, but you'll also need to use system tools to gather the hostname information, and those tools are different between linux and windows. That said, with a simple preprocessor switch, we can use QProcess to call the right one, and pull the hostnames out of the result using a QRegExp:
// find valid hostnames
QStringList hostnames;
QRegExp hostnameRx("\\\\\\\\(.*)");
QProcess cmd(this);
#ifdef _WIN32
cmd.start("cmd.exe");
cmd.write("net view\r\n");
cmd.write("exit\r\n");
#else
cmd.start("smbtree", QStringList() << "--no-pass");
#endif // _WIN32
cmd.waitForFinished();
while (!cmd.atEnd())
{
QString line = cmd.readLine();
hostnameRx.indexIn(line);
if (!hostnameRx.cap(1).trimmed().isEmpty())
{
hostnames << hostnameRx.cap(1).trimmed();
}
}
The regex strips the begining '\\' returned by both net view and smbtree, because QTcpSocket connections take hostnames without it.
Obviously, the QStringListcan be used to populate a QComboBox:
QComboBox* box = new QComboBox(this);
box->insertItems(0, hostnames);
NOTE: net view and smbtree are only going to show computers with accessible shares. You can try nmap for a more complete list of live hosts, but you're going to need to run as root and you'll still probably hit a lot of firewall issues.

.vbs file not working properly from windows service

I created a windows service in C++ which runs a VBScript file on a particular event. This works fine when I do this from a Win32 application in C++, but doesn't work the same way from windows service.
Code of CPP file:
SHELLEXECUTEINFO ExecuteInfo;
memset(&ExecuteInfo, 0, sizeof(ExecuteInfo));
ExecuteInfo.cbSize = sizeof(ExecuteInfo);
ExecuteInfo.fMask = 0;
ExecuteInfo.hwnd = 0;
ExecuteInfo.lpVerb = _T("open"); // Operation to perform
ExecuteInfo.lpFile = _T("D:\\demo.vbs"); // Application name
ExecuteInfo.lpParameters = 0; // Additional parameters
ExecuteInfo.lpDirectory = 0; // Default directory
ExecuteInfo.nShow = SW_SHOW;
ExecuteInfo.hInstApp = 0;
if(ShellExecuteEx(&ExecuteInfo) == FALSE)
{
}
Assuming objShell.Windows is an instance of "Shell.Application", what you get is according to msdn
ShellWindows object: Represents a collection of the open windows that
belong to the Shell. Methods associated with this objects can control
and execute commands within the Shell, and obtain other Shell-related
objects.
The critical part is that belong to the Shell. Your problem is that the shell in your desktop is different from the shell that the services in your machine have.
Services run in a separate session.
So, your script is correctly working. It doesn't return any shell window because there is no one in the session where the services are running.
I created a normal exe instead of windows service and added that to windows startup registory.
That way i was able to achieve desired.

How to set Proxy Address using QProcessEnvironment on Linux?

I am stuck with a simple issue in Qt. I want set proxy address using Qt. The command to set proxy address
export http_proxy=http://wwgw.abcd.com:8080
works fine if passed by a terminal manually. but If the same command is run using QProcess, it fails without setting proxy. Even, I tried with QProcessEnvironment as
QProcess process_setupProxyServerUrl;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString cmd = "http://wwgw.abcd.com:8080";
env.insert("HTTP_PROXY", cmd);
process_setupProxyServerUrl.setProcessEnvironment(env);
But this also fails in setting up proxy address. QProcessEnvironment is new for me. So may be i might be using it in wrong way.
In my application, I need to change the proxy address according to users choice (at run time).
Any way using Qt would be helpfull. Please provide some suggestions/ideas to resolve this issue.
Try something like that
QProcess process_setupProxyServerUrl;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("HTTP_PROXY", "http://wwgw.abcd.com:8080");
process_setupProxyServerUrl.setProcessEnvironment(env);
Why did you use export ? This is just an executable, not the environment key