Trying to detect monitor - c++

I'm trying to get the monitor in order to check if is off or not.
Before checking with GetDevicePowerState, I'm trying to retrieve monitor in this way:
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <winuser.h>
using namespace std;
int main(int argc, char *argv[])
{
POINT* p = new POINT;
p->x=0;
p->y=0;
HMONITOR* monitor = MonitorFromPoint(p,DWORD.MONITOR_DEFAULTTOPRIMARY);
system("PAUSE");
return EXIT_SUCCESS;
}
But it continually gives me:
main.cpp `MonitorFromPoint' undeclared (first use this function)
Where have I gone wrong?

Your code has a number of problems, but none of them should cause the error message you're seeing. Here's code with some corrections, and a little more added to show at least some kind of result from the test:
#include <iostream>
#include <windows.h>
int main(int argc, char *argv[])
{
POINT p{ 0, 0 };
HMONITOR monitor = MonitorFromPoint(p, MONITOR_DEFAULTTONULL);
if (monitor == NULL)
std::cout << "No monitor found for point (0, 0)\n";
else {
MONITORINFOEX info;
info.cbSize = sizeof(info);
GetMonitorInfo(monitor, &info);
std::cout << "Monitor: " << info.szDevice << "\n";
}
}
I've tested this with both VC++ 2013 and MinGW 4.8.1, and in both cases it's compiled and run without any problems, producing:
Monitor: \\.\DISPLAY1
...as its output in both cases.

Related

Is there a function for sleeping a set amount in C++ for windows?

I know there is one in C, Sleep(ms), but is there one for C++? I am trying to return an error, then print to the console, then sleep enough for the user to read it before returning the errorcode. Code in C would be:
#include <stdio.h>
#include <windows.h>
int main (int argc, const char *argv[]) {
char *err = "Have an error!";
printf("Error: %s. Program terminating in 5 seconds...", err);
Sleep(5000);
return 1;
}
You could include <windows.h> and just call the WinApi function Sleep just as you would from C. This is mostly pure C++ :
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
using namespace std;
int main (int argc, const char *argv[]) {
string err = "Have an error!";
cout << "Error: " << err << ". Program terminating in 5 seconds..." << endl;
std::chrono::milliseconds timespan(5000);
std::this_thread::sleep_for(timespan);
return 1;
}

Raspicam free resources

I'm trying to do basic image capture with the raspicam C++ libraries from http://www.uco.es/investiga/grupos/ava/node/40
The image capture generally works but seems that I can't run the program again once it's ended, or it hangs on grab_retrieve. The system requires a reboot. My guess is that something is not freeing up resources but am not sure how to diagnose that.
How can I get this utility to clean up after itself?
Code is below. Alternative suggestions are also welcome, as I am not tied to this library.
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <stdio.h>
#include "../raspicam-0.1.3/src/raspicam.h"
#include "../raspicam-0.1.3/src/raspicam_still.h"
#include "../raspicam-0.1.3/src/raspicamtypes.h"
using namespace std;
int main ( int argc, char *argv[] ) {
raspicam::RaspiCam_Still still;
still.setEncoding ( raspicam::RASPICAM_ENCODING_JPEG );
int length=still.getImageBufferSize();
unsigned char *data=new unsigned char[length];
cout<<"Capturing image at size " << length <<endl;
if(!still.grab_retrieve(data, length)) {
cerr<<"Error in grab"<<endl;
return -1;
}
still.release();
cout<< "Got it" << endl;
ofstream file ( "picture.jpg",ios::binary );
file.write ( ( char* ) data, length );
delete[] data;
return 0;
}

Why doesn't using std::vector work in NetBeans?

I was writing a sample code on NetBeans and I realized that using std::foo does not work on NetBeans. It gives a "unresolved identifier error". This is the sample code below;
#include <cstdlib>
#include <iostream>
#include <vector> //Required whenever you use vectors
/*
*
*/
using std::vector;
using std::cout;
using std::endl;
int main(int argc, char** argv) {
vector<int> integers (4, 100); // Creates a vector [100 100 100 100]
cout << integers[0] << endl;
return 0;
}
The code builds and runs but the error is still visible on the text page.

SFML TCP socket doesn't wait for data

Code is here
#include <SFML/Network.hpp>
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
char mode = *argv[1];
if(mode == (char) "-s") {
sf::Packet recMessage;
sf::TcpListener tcpListener;
sf::TcpSocket inClient;
tcpListener.accept(inClient);
inClient.receive(recMessage);
cout << recMessage << endl;
}
}
When ran with -s i'm expecting the program not to close until it receives data but when I run the program it closes.
I changed mode to a std::string and took out the char castings
The if statements run now if I run into any more trouble I'll make another question

C++ dup2 and execl

I am working on an assignment and I need to create pipes so that other programs handle different functions. I am able to pipe through the command line no problem, thats easy. However using dup2 and execl have been tricky for me. At one point I was able to get output from one part of my program but it wasn't reading anything in from another part.
here is what i have:
pipeline.cpp
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <cstdlib>
#include <iostream>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
#include<cstdlib>
#include<unistd.h>
#include<iomanip>
#include <sys/wait.h>
using namespace std;
int main(int argc, char *argv[])
{
int number = atoi(argv[1]);
int x2ypipe[2];
pipe(x2ypipe);
if(x2ypipe==0){
cout<<"ERROR:"<<errno<<endl;
}
pid_t xchild =fork();
if(xchild==0){
dup2(x2ypipe[1],STDOUT_FILENO);
close(x2ypipe[0]);
close(x2ypipe[1]);
execl("./part1.cpp","part1.cpp", (char *)NULL);
}
pid_t ychild =fork();
if(ychild==0){
dup2(x2ypipe[0],STDIN_FILENO);
close(x2ypipe[0]);
close(x2ypipe[1]);
execl("./part2.cpp", "part2.cpp", (char *)NULL);
}
close(x2ypipe[0]);
close(x2ypipe[1]);
wait(NULL);
wait(NULL);
part1.cpp
#include<iostream>
#include<cstdlib>
#include<unistd.h>
#include<iomanip>
using namespace std;
int main(int argc, char *argv[])
{
int number = atoi(argv[1]);
for (int k = 1; k <= 9; k++)
{
cout << k << " " << flush;
sleep(1);
}
return 0;
}
part2.cpp
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <iomanip>
using namespace std;
int main()
{
int number;
while (cin >> number)
{
cout << 2 * number - 1 << " " << flush;
}
return 0;
}
Ok so pipeline.cpp : forks twice and creates a pipe between the two children. Then each use excel to replace its process with the programs part1 and part2. So my understanding is that part1 program would run and anything it outputs will be picked up by the second child which runs part2 and from there part two would output normally since it's output descriptor wasn't changed. Am I missing or misusing something here?
I noticed a couple of things:
You're not passing the number to the part1 process when you exec it
You're not checking for failure from execl() or any of the other OS functions
I think once you do these two things, you'll find out what the real problem is. I won't just tell you what the answer is, because it's worthwhile learning how to diagnose such problems yourself. (I was able to run your code successfully with only minor modifications. The problem does not lie in how you're handling the pipes and file descriptors.)
I think you need to return 0; after your exec calls. But I am even more lost than you it seems.