Get printer details based on their status - visual-c++

Using Windows Management Instrumentation (WMI) in VC++ we can find the SystemInfo like System Name and other properties.
Example for GetComputerName :
BOOL WINAPI GetComputerName(
_Out_ LPTSTR lpBuffer,
_Inout_ LPDWORD lpnSize
);
There are 3 printers attached in my system 1 thermal and 2 Shared printers ,
How can i get the information about printer which is offline ?
How can i classify/list printers based on their status ?
Thanks

See also EnumPrinters
DWORD flags = PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS;
DWORD bufsize, printerCount;
DWORD level = 2; //2 is for PRINTER_INFO_2
::EnumPrinters(flags, NULL, level, NULL, 0, &bufsize, &printerCount);
if (bufsize)
{
BYTE* buffer = new BYTE[bufsize];
::EnumPrinters(flags, NULL, level, buffer, bufsize, &bufsize, &printerCount);
if (bufsize && printerCount)
{
const PRINTER_INFO_2* info = (PRINTER_INFO_2*)buffer;
for (DWORD i = 0; i < printerCount; i++)
{
if (info->pServerName) cout << "pServerName: " << info->pServerName << endl;
if (info->pPrinterName) cout << "pPrinterName: " << info->pPrinterName << endl;
if (info->pShareName) cout << "pShareName: " << info->pShareName << endl;
if (info->pPortName) cout << "pPortName: " << info->pPortName << endl;
if (info->Attributes & PRINTER_ATTRIBUTE_LOCAL) cout << "[local]\n";
if (info->Attributes & PRINTER_ATTRIBUTE_NETWORK) cout << "[network]\n";
wcout << "status: " << info->Status << endl;
if (info->Status & PRINTER_STATUS_ERROR) cout << "status: error\n";
wcout << endl;
info++;
}
}
delete[] buffer;
}

Related

main.cpp:4:10: fatal error: Car.h: No such file or directory

I am using zybook, and it requires for me to create a program that maintains the inventory of a car rental agency.
The three files for this program are:
car.h - Class declaration
car.cpp - Class definition
main.cpp - main() function and other functions as described
When I wrote the program, I am getting an error:
main.cpp:4:10: fatal error: Car.h: No such file or directory
4 | #include "Car.h"
| ^~~~~~~
compilation terminated.
car.cpp:3:10: fatal error: Car.h: No such file or directory
3 | #include "Car.h"
| ^~~~~~~
compilation terminated.
Here is my code:
#include <iostream>
#include <string.h>
#include <vector>
#include "Car.h"
#include <algorithm>
using namespace std;
void addCar(vector<Car>& cars);
//bool deleteCar(vector<Car>& cars);
bool updateCarCondition(vector<Car>& cars);
void displayCar(vector<Car>& cars);
void displayAllCars(vector<Car>& cars);
bool rentCar(vector<Car>& cars);
bool returnCar(vector<Car>& cars);
int main(){
char option;
vector<Car> cars;
while (true){
cout << "CAR RENTAL AGENCY MENU" << endl;
cout << "a - Add car to inventory" << endl;
cout << "d - Delete car by id from inventory" << endl;
cout << "u - Update car by id condition in inventory" << endl;
cout << "s - Display one car by id from inventory" << endl;
cout << "i - Display list of all cars in inventory" << endl;
cout << "c - Rent a car by id in inventory" << endl;
cout << "r - Return a car by id in inventory" << endl;
cout << "q - Quit" << endl;
cout << "Choose an option: " << endl;
cin >> option;
cin.ignore();
cout << endl;
switch(option){
case 'a': addCar(cars);
break;
//case 'd': deleteCar(cars);
//break;
case 'u': updateCarCondition(cars);
break;
case 's': displayCar(cars);
break;
case 'i': displayAllCars(cars);
break;
case 'c': rentCar(cars);
break;
case 'r': returnCar(cars);
break;
case 'q': break;
default: cout << "Please enter a valid option" << endl;
}
}
return 0;
}
void addCar(vector<Car>& cars){
int id, year;
string model, make, condition;
cout << "ADD CAR TO INVENTORY" << endl;
cout << "Enter an ID: " << endl;
cin >> id;
cout << "Enter the make: " << endl;
cin >> make;
cout << "Enter the model: " << endl;
cin >> model;
cout << "Enter the year: " << endl;
cin >> year;
cout << "Enter the condition (new, slighty_used, used): " << endl;
cin >> condition;
Car car(id, make, model, year, condition);
cars.push_back(car);
}
/*bool deleteCar(vector<Car>& cars){
cout << "REMOVE CAR FROM INVENTORY" << endl;
int id;
cout << "Enter the ID of the car to delete: " << endl;
cin >> id;
vector<Car>::iterator it;
for (int i = 0; i < cars.size(); i++){
if (cars.at(i).Getid() == id){
//Car const& const_car = cars.at(i);
//std::vector<Car>::iterator itr = std::find(cars.begin(), cars.end(), const_car);
it = cars.at(i);
cars.erase(it);
return true;
}
}
return false;
}*/
bool updateCarCondition(vector<Car>& cars){
cout << "UPDARE CAR CONDITION IN INVENTORY" << endl;
int id;
string condition;
cout << "Enter the ID of the car to update condition: " << endl;
cin >> id;
cout << "Enter the condition (new, slighty_used, used): " << endl;
cin >> condition;
for (int i = 0; i < cars.size(); i++){
if (cars.at(i).Getid() == id){
cars.at(i).Setcondition(condition);
return true;
}
}
return false;
}
void displayCar(vector<Car>& cars){
cout << "DISPLAY CAR IN INVENTORY" << endl;
int id;
cout << "Enter the ID of the car to display: " << endl;
cin >> id;
for (int i = 0; i < cars.size(); i++){
if (cars.at(i).Getid() == id){
cars.at(i).displayCar();
break;
}
}
}
void displayAllCars(vector<Car>& cars){
cout << "DISPLAY ALL CARS IN INVENTORY" << endl;
for (int i = 0; i < cars.size(); i++){
cars.at(i).displayCar();
}
}
bool rentCar(vector<Car>& cars){
cout << "RENT CAR IN INVENTORY" << endl;
int id;
cout << "Enter the ID of the car to rent: " << endl;
cin >> id;
for (int i = 0; i < cars.size(); i++){
if (cars.at(i).Getid() == id){
if (cars.at(i).Getrented()){
cout << "Car is already rented" << endl;
return false;
}
else if (cars.at(i).Getrented() == false){
cout << "Car has been successfully rented to you" << endl;
cars.at(i).toggleRented();
return true;
}
}
}
cout << "Car " << id << " not found in inventory" << endl;
return false;
}
bool returnCar(vector<Car>& cars){
cout << "RENT CAR TO INVENTORY" << endl;
int id;
cout << "Enter the ID of the car to return: " << endl;
cin >> id;
for (int i = 0; i < cars.size(); i++){
if (cars.at(i).Getid() == id){
if (cars.at(i).Getrented()){
cars.at(i).toggleRented();
cout << "Car returned successfully!!" << endl;
return true;
}
}
}
cout << "Car " << id << " not found in inventory" << endl;
return false;
}

Consumer doesn't see state of variable changing

In the complete code (see below), at some point (after N iterations) the consumer (function executionServiceHandler while (inService_) is entered, despite inService_ having been changed (in the same thread) [or so it seems from log output].
I have as far as possible tried to guard std::cout as well, although I'm not sure this is necessary.
Could you perhaps crit the code. I know strictly speaking I don't need to use StopServiceCmd to let the thread complete, but in my mind I can't see why it shouldn't work.
Also, I could have used std::function, but the example is simplified from original.
I could add that I tested this on GCC (latest), after the original example failed on VCC (2017)
Code follows (EDIT - now using std::function:
#include <thread>
#include <functional>
#include <mutex>
#include <iostream>
#include <queue>
#include <sstream>
#include <condition_variable>
class ThreadCmdConsumer
{
public:
using Guard = std::lock_guard<std::mutex>;
using UniqueLock = std::unique_lock<std::mutex>;
ThreadCmdConsumer()
: inService_(),
executionServiceThread_(std::bind(&ThreadCmdConsumer::executionServiceHandler, this))
{
//Wait while thread does not exist...
UniqueLock lock(cmdQMutex_);
while (!inService_) {
conditional_.wait(lock);
}
std::cout << std::hex << this << " constructed and consumer waiting..." << std::endl;
}
~ThreadCmdConsumer() {
static std::size_t nth = 0;
{
UniqueLock lock(cmdQMutex_);
std::cout << "destructing (" << std::dec << nth++ << "): " << std::hex << this << std::endl;
}
process([this](){
//Note: inService_ can only be changed in consumer thread...
inService_ = false;
std::cout << "consumer_->inService state: " << inService_ << std::endl;
});
UniqueLock lock(cmdQMutex_);
std::cout << "producer " << std::hex << this << " destructor has lock" << std::endl;
while (inService_) {
std::cout << "producer " << std::hex << this << " destructor in service, entering wait" << std::endl;
conditional_.wait(lock);
std::cout << "producer " << std::hex << this << " destructor exited wait - has lock" << std::endl;
}
// Join will always succeed as result of StopServiceCmd that sets inService to false
// (in its own thread context). Once join completes, we are certain of executionServiceHandler
// exiting normally.
std::cout << "produces " << std::hex << this << " destructor joining" << std::endl;
lock.unlock();
try {
executionServiceThread_.join();
}
catch (const std::system_error& ex) {
UniqueLock lock(cmdQMutex_);//for cout
std::cout << "Exception during join" << ex.what() << std::endl;
abort();
}
}
void executionServiceHandler()
{
{ //Starts the service...
UniqueLock lock(cmdQMutex_);
inService_ = true;
lock.unlock();
conditional_.notify_one();
}
try {
UniqueLock lock(cmdQMutex_);
while (inService_) {
std::cout << "consumer " << std::hex << this << " has lock" << std::endl;
// Catering for spurious wake-ups too, hence while...
while (cmdQ_.empty()) {
std::cout << "consumer " << std::hex << this << " waiting" << std::endl;
conditional_.wait(lock);
std::cout << "consumer " << std::hex << this << " woken" << std::endl;
}
//Now we have the lock, and queue most certainly not empty
auto cmd = std::move(cmdQ_.front());
cmdQ_.pop();
//###lock.unlock(); // Don't want to be locked while executing... removed conservatively
(cmd)();
}
std::cout << "consumer " << std::hex << this << " execution complete" << std::endl;
}
catch(const std::exception& ex) {
std::cerr << "Unexpected " << ex.what() << std::endl;
abort();
}
//Not in service - notify when we've left (then we can truly join...)
conditional_.notify_one();
}
void process(std::function<void()>&& cmd)
{
UniqueLock lock(cmdQMutex_);
std::cout << "producer " << std::hex << this << " has lock" << std::endl;
bool notificationRequired = cmdQ_.empty();
cmdQ_.push(move(cmd));
if (notificationRequired) {
std::cout << "producer " << std::hex << this << " notifying" << std::endl;
lock.unlock();
conditional_.notify_one();
}
else {
std::cout << "producer " << std::hex << this << " not notifying" << std::endl;
}
}
private:
bool inService_;
std::queue<std::function<void()>> cmdQ_;
std::condition_variable conditional_;
std::mutex cmdQMutex_;
std::thread executionServiceThread_;
};
typedef std::function<void(const std::string&)> Handler;
struct ThreadOwner
{
ThreadCmdConsumer executor_;
// Do it done in the context of executor....
void doIt(const Handler& handler)
{ }
};
int main()
{
std::cout << "Program started" << std::endl;
//Sometimes deadlocks on thread being killed
for (int i = 0; i < 1000; ++i) {
auto handler = [](const std::string&){};
{
ThreadOwner owner;
owner.executor_.process([&handler, &owner]() {
owner.doIt(handler);
});
}
}
}

Why the variable 'len_' never been change in the function ca_time

void ca_time(int *arr,int &len_) //the variable len_'s value always is 0
{
cout << "You only got five seconds to type the number 1~5,,ready go,,\n";
_sleep(5000);
cout << "Sorry time out!!\n";
cout << "Ok, here is your greads:\n";
for(int i = 0; i < len_; i ++)
{
cout << arr[i] << " ";
}
cout << endl;
cout <<"-->" << len_ << endl;
return;
}
void fun_type(int *arr,int &len_)
{
memset(arr,'\0',sizeof(arr));
for(; len_ < 5; len_ ++)
{
cin >> arr[len_];
}
}
int main()
{
int arr[100];
int len = 0;
thread time(ca_time,arr,len);
time.detach();
fun_type(arr,len);
system("pause");
return 0;
}
But it work when changed the quote to the address(point variable).Why?
Somebody say that's a IED's bug?But I don't think so .So What the hell?

winsock : bluetooth client-server not connecting

I have written a small bluetooth server and client progrem using winsock
I am not able to figure out why the client is not getting connected to the server. Both are running in different pcs and
both are paired through bluetooth.
The server code is
void server()
{
SOCKET server_socket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM), new_socket;
if (server_socket == INVALID_SOCKET)
{
cout << "socket creation failed...Error code : " << WSAGetLastError() << endl;
Sleep(2000);
return;
}
cout << "socket created" << endl;
SOCKADDR_BTH sa, sa2;
int channel = 0, len=sizeof(sa2);
memset(&sa, 0, sizeof(SOCKADDR_BTH));
sa.addressFamily = AF_BTH;
sa.port = channel & 0xff;
//bind
if (bind(server_socket, (SOCKADDR *)&sa, sizeof(sa)))
{
cout << "Binding failed...Error code : " << WSAGetLastError() << endl;
closesocket(server_socket);
Sleep(2000);
return;
}
cout << "binding done" << endl;
cout << "\nWaiting for client" << endl;
listen(server_socket, 3);
new_socket = accept(server_socket, (sockaddr *)&sa2, &len);
cout<<"connection accepted";
}
The client code is
void client()
{
SOCKET client_socket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
int channel = 0;
BTH_ADDR bt_addr;
char* server_address = "34:02:86:26:c1:62";
if (client_socket == INVALID_SOCKET)
{
cout << "socket creation failed...Error code : " << WSAGetLastError() << endl;
Sleep(2000);
return;
}
cout << "socket created" << endl;
if (str2ba(server_address, &bt_addr) == 1)
{
cout << "address conversion error..." << endl;
Sleep(2000);
return;
}
SOCKADDR_BTH sa;
sa.addressFamily = AF_BTH;
sa.port = channel & 0xff;
sa.btAddr = bt_addr;
cout << "\nconnecting..." << endl;
if (connect(client_socket, (sockaddr *)&sa, sizeof(sockaddr)))
{
cout << "Error in connecting...Error code : " << WSAGetLastError() << endl;
closesocket(client_socket);
Sleep(2000);
return;
}
cout << "\nConnected" << endl;
Sleep(2000);
}
int str2ba(char *str_bt_addr, BTH_ADDR *bt_addr)//for converting string to bluetooth address
{
unsigned int addr[6];
if (sscanf_s(str_bt_addr, "%02x:%02x:%02x:%02x:%02x:%02x",
&addr[0], &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6)
{
return 1;
}
*bt_addr = 0;
BTH_ADDR tmpaddr;
int i;
for (i = 0;i < 6;++i)
{
tmpaddr = (BTH_ADDR)(addr[i] & 0xff);
*bt_addr = ((*bt_addr) << 8) + tmpaddr;
}
return 0;
}
Why are these not getting connected? What am I missing?
Please help me.
Thanks in advance for any help.
In my short Bluetooth experience, the problem is normally somewhere in the SOCKADDR_BTH declarations.
I hard coded the MAC Address of each Endpoint: "38:2D:E8:B9:FA:EB" in Hex
RemoteEndPoint.btAddr = BTH_ADDR(0x382DE8B9FAEB);
Also make sure your Ports are the same on each Endpoint, I used:
RemoteEndPoint.port = 0;
and
LocalEndpoint.port = 0;
I have some code here: C++ WinSock Bluetooth Connection - AT Command - Error Received where I have an issue also.
Bluetooth is not as easy as some may think, thus the lack of answers received by the OP's

How do I copy files and folders using boost and Visual Studio 2005?

I'm trying to use boost::filesystem to copy files and folders (just like a standard copy a folder and paste it in windows explorer).
Although I've been to the boost::filesystem documentation, I still don't really know how to go about doing this.
Do you have to recursively go though each directory (creating it) and find each file copying it?
Additionally, how do you copy the file in C++/Boost?
P.S. I'm using Boost 1.40.0
Update
I think I may have ended up creating an answer to this one, only concern being that I don't do any try-catch errors to check for locked files and folders.
The following code makes a copy of a directory in the relative path "../example/ecomm" and duplicates it to a non-existing path "../example/dup_ecomm":
#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/regex.hpp>
#include <iostream>
#include <fstream>
#include<string>
bool copy_dir( const boost::filesystem::path & ext_dir_path, // the existing directory
const boost::filesystem::path & duplicate_dir_path // the duplicate directory
)
{
std::cout << "BEGIN: copy_dir " << endl;
std::cout << "- ext_dir_path: " << ext_dir_path << endl;
std::cout << "- duplicate_dir_path: " << duplicate_dir_path << endl;
// 1. Ensure that the directory we are trying to copy exists.
if (!boost::filesystem::exists( ext_dir_path ) ) return false;
bool createdDir = boost::filesystem::create_directory( duplicate_dir_path );
// cout << "createdDir: " << createdDir << endl;
copy_dir(ext_dir_path, // the existing directory
duplicate_dir_path, // the duplicate directory,
ext_dir_path, // the base path for the existing directory
duplicate_dir_path,
true);
std::cout << "END: copy_dir " << endl;
}
bool copy_dir( const boost::filesystem::path & ext_dir_path, // the existing directory
const boost::filesystem::path & duplicate_dir_path, // the duplicate directory,
const boost::filesystem::path & base_ext_dir_path, // the base path for the existing directory
const boost::filesystem::path & base_duplicate_dir_path, // the base path for the duplicate of the exisiting directory
bool isRootPath)
{
// Debug input arguments
std::cout << "BEGIN: copy_dir " << endl;
std::cout << "- ext_dir_path: " << ext_dir_path << endl;
std::cout << "- duplicate_dir_path: " << duplicate_dir_path << endl;
std::cout << "- base_ext_dir_path: " << base_ext_dir_path << endl;
std::cout << "- base_duplicate_dir_path: " << base_duplicate_dir_path << endl;
std::cout << "- isRootPath: " << isRootPath << endl;
boost::filesystem::directory_iterator end_itr; // default construction yields past-the-end
cout << "--Beginning itr loop" << endl;
for ( boost::filesystem::directory_iterator itr( ext_dir_path );
itr != end_itr;
++itr )
{
if ( boost::filesystem::is_directory(itr->status()) )
{
cout << "---itr->path(): " << itr->path() << endl;
boost::filesystem::path newExtDir(itr->path());
string dup_path = itr->path().string();
boost::algorithm::replace_first(dup_path, base_ext_dir_path.string(), base_duplicate_dir_path.string());
cout << "dup_path: " << dup_path << endl;
boost::filesystem::path new_dup_dir(dup_path);
bool createdDir = boost::filesystem::create_directory( new_dup_dir );
cout << "creating directory " << dup_path << " created: " << createdDir << endl;
boost::filesystem::path newDuplicateDir(duplicate_dir_path);
copy_dir(newExtDir, // the existing directory
newDuplicateDir, // the duplicate directory,
base_ext_dir_path,
base_duplicate_dir_path,
false);
}
else
{
cout << "---isLeaf: " << itr->path() << endl;
string dup_path = itr->path().string();
boost::algorithm::replace_first(dup_path, base_ext_dir_path.string(), base_duplicate_dir_path.string());
string src_path = itr->path().string();
cout << "src_path: " << src_path << endl;
cout << "dup_path: " << dup_path << endl;
boost::filesystem::path s_path(src_path);
boost::filesystem::path d_path(dup_path);
boost::filesystem::copy_file(s_path, d_path);
}
}
std::cout << "--Ending itr loop" << endl;
std::cout << "END: copy_dir " << endl;
return false;
}
test_suite*
init_unit_test_suite( int, char* [] ) {
boost::filesystem::path ext_dir("..\\example\\ecomm");
boost::filesystem::path dir_dup("..\\example\\dup_ecomm");
copy_dir(ext_dir,
dir_dup); // the duplicate directory,
// ... unit tests...etc...
}
My question now is what I'm I forgetting to do in regards to locked files and directories?
boost::filesystem::recursive_directory_iterator

Resources