How come console app doesn't end? - visual-c++

My first time working with Visual C++ (new to the language, too) - experienced C# ...so I have my first console app that I started in Visual Studio.
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int i;
cin >> i;
return 0;
}
How come the console window - thus the application - doesn't close when I press enter? No other input - just enter ...
More importantly, how can I make the app exit (don't want to use exit()) properly if I just hit enter?

std::cin awaits one non-empty string from you and then tries to convert this string to integer.
When you press Enter std::cin only gets an empty string and continues to wait for some valid input. This is by design. std::cin is not meant for emulating other interaction.
To terminate the app on keypress, you have to use OS-specific facilities to read keyboard presses.
This is the kbhit() function from "conio.h" on DOS/console Windows and the termio functions on POSIX systems.
From your source I can conclude that you use the MSVC++ compiler, so try replacing
std::cin >> i
by
while(!kbhit()) {}
Do not forget to add the
#include <conio.h>
and remember this is a Windows-specific solution.

Related

MSVC compiler error for external variables and OpenMP's threadprivate

I am trying to compile a c++ application using MSVC (Compiler Version 19.32.31329 for x86). The author of the application has complied it successfully using GCC 8.3, 9.3, and 10.3.
I run into an error when I enable OpenMP for parallel computing - in particular in the random.h and random.cpp files. I have found a small code snippet that throws the same compile error on MSVC but has no issues when compiled with GCC according to the author.
#include<omp.h>
#include<iostream>
struct point2d
{
int x;
int y;
#ifndef DECLARE_AS_POD_TYPE
point2d() {}
#endif
};
int main(int argc, char* argv[])
{
using namespace std;
extern point2d myPoint;
#pragma omp threadprivate(myPoint)
//point2d myPoint;
myPoint.x = omp_get_thread_num();
cout << myPoint.x << endl;
}
The MSVC error I get is error C3053: 'myPoint': 'threadprivate' is only valid for global or static data items
I am pretty new to OpenMP but have done a fair amount of googling. I am not the first person to have this issue but I have not found a solution that works for me. In particular, I have looked at:
Using threadprivate directive in Visual Studio
Threadprivate directive after external declaration of global variables
OpenMP threadprivate directive is not working.
I am using this application as a bit of a learning exercise so I am happy to make changes to the code but I have tried quite a few permutations and I am not able to make progress.
Any suggestions are welcome.

Adding a new system call Linux 5-9.8 and make it appear on boot screen

I'm trying to add a new system call to linux -5.9.8 for this I've been following this tutorial from a prev question and this one and this from a guide they are both really similar and everything does work as intended BUT to get the system call to show with dmesg we get to compile the following code after boot up:
#include <linux/kernel.h>
#include <sys/syscall.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#define __NR_identity 440
long identity_syscall(void)
{
return syscall(__NR_identity);
}
int main(int argc, char *argv[])
{
long activity;
activity = identity_syscall();
if(activity < 0)
{
perror("Sorry. Your system call appears to have failed.");
}
else
{
printf("Congratulations! Your system call is functional. Run the command dmesg in the terminal and find out!\n");
}
return 0;
}
This code does in fact add the message to dmesg but it does it after boot up and I need to run the code everytime, how should I go to actually make my system call appear on the boot up screen and not later, what file should I edit to make it happen?
If the goal is to merely display some sort of message at system startup time, you don't need a system call but rather a kernel module statically linked or dynamically loaded.
In the file where you define the system call, you can add an "init" routine which will be called by the kernel at startup. To make it, the kernel build chain uses "__init" section identifier to gather all the initialization entry points into a dedicated section. Then, in the boot procedure, all the functions from this section are called. You can try something like the following from your example (I didn't tried it !):
#include <linux/kernel.h>
#include <linux/syscalls.h>
SYSCALL_DEFINE0(identity)
{
printk("I am Jihan Jasper Al-rashid.\n");
return 0;
}
void __init identity_entry(void)
{
printk("Boot message from identity\n");
}

How to rotate a Qt5 application using the linux framebuffer?

I have an embedded linux application running directly on the linux framebuffer (no x-Windows). We now have to physically rotate the display 180 degrees. How do I get my Qt application to rotate so it doesn't appear upside down? I saw reference to using the following option:
-platform linuxfb:fb=/dev/fb0:rotation:180
However, the rotation option seems to be ignored.
Using Qt 5.9.2 on Ubuntu server 16.04.6
You could handle it on application level. With QML thats easy, but with QWidgets the best I could come up with is to render the Widget on a QGraphicsScene and rotate it like this:
#include "mainwindow.h"
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
QGraphicsScene *scene = new QGraphicsScene();
QGraphicsView *view = new QGraphicsView();
view->setGeometry(w.geometry());
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scene->addWidget(&w);
view->setScene(scene);
view->show();
view->rotate(180);
//w.show();
return a.exec();
}
It seems a bit glitchy, but you could give it a try.
Also I think the correct syntax is -platform linuxfb:fb=/dev/fb0:rotation=180 note the = instead of :
Edit: but that did not make a difference for me either.

Why MainWindow in Qt is closing after start the program?

I was working on Windows with this program and was ok..
When im running it on linux (using QT creator, same on windows) window show and hide immediately.
The program is to big to paste it all, this is my main.cpp code
int main(int argc, char *argv[]){
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
What should it be?
If there is no fatal exception that exits the app then the event loop (a.exec()) is quitting due to some event served. So, the visibility is not enough to conclude why your app is quitting on Linux.

Code injection - Solaris & Linux

I have an executable module created by third party. I would like to "inject" my code (kind of watchdog running in separate thread) into this process.
So far there are two possible ways - one is to run my code as executable and dynamically load a proess on top of it (seems to be very hard and tricky) or to make my code a shared object, load it via LD_PRELOAD and initialize from some static variable constructor.
Are there more convenient ways to do this ?
My OS are Linux x86 and Solaris-SPARC.
Update: If possible, I'd like not to patch the process, but load my code dynamicaly.
Sounds like you're looking for InjectSo. There's a Powerpoint presentation that explains how it works. I haven't gotten around to trying it out yet.
Hotpatch should do this for you. It is more capable than injectso.
Rob Kennedy told you about InjectSo - that's probably what you need.
Beware that the introduction of a thread into a non-threaded process would be fraught with synchronization issues. The problems are less serious if the application is already threaded, but even so, the application may object to a thread that it doesn't know about.
I have not used the mentioned InjectSo but it is a noteworthy information.
If you are looking for alternatives here is a simple way to inject your code:
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>
int main()
{
struct passwd* pswd = getpwuid(1000);
if(pswd)
printf("%s\n", pswd->pw_name);
return 0;
}
gcc test.c -o test
#define _GNU_SOURCE
#include <dlfcn.h>
#include <sys/types.h>
#include <pwd.h>
#include <stdlib.h>
#include <stdio.h>
static char* hocus = "hocus pocus";
struct passwd *getpwuid(uid_t uid)
{
static struct passwd *(*orig_getpwuid)(uid_t uid);
if(!orig_getpwuid) {
orig_getpwuid = (struct passwd* (*)(uid_t))dlsym(RTLD_NEXT, "getpwuid");
}
struct passwd* original_passwd = (*orig_getpwuid)(uid);
if(original_passwd) {
original_passwd->pw_name = hocus;
}
// your code here
return original_passwd;
}
gcc inject.c -shared -o libinject.so
run with LD_LIBRARY_PATH=. LD_PRELOAD=libinject.so ./test
Should say hocus pocus. You can override arbitrary libc functions, like printf, snprintf - just find what is that module using.
In the "your code here" you can start arbitrary threads, watchdogs etc.

Resources