windows lock and unlock event in C++ [duplicate] - c++

This question already has answers here:
C++: check if computer is locked
(5 answers)
Closed 3 years ago.
I want to create a program that "locks" my work station as soon as I leave or move away from my computer. And automatically starts again as soon as the user login.
I'm using Opencv to track my movement and its working.
now I want to know how can I automatically start a function again as soon as the user login using c++
I'm using
LockWorkStation();
to lock my system
I have a C# implementation for it which I got from
Programmatically Determine a Duration of a Locked Workstation?
const int SessionUnlockParam = 0x8;
if (m.WParam.ToInt32() == SessionUnlockParam)
{OnSessionUnlock(); // Do something when unlocked
}
void OnSessionUnlock()
{
// Do something......
}
can someone tell me how to to do this in C++
and which libraries to use
or
How can I access Event IDs in C++
such as: " Event_ID = 4801 - The workstation was unlocked".
for windows 8

You can register a window to receive events for session changes using WTSRegisterSessionNotification.
Then listen for WM_WTSSESSION_CHANGE with a wParam value of WTS_SESSION_UNLOCK.

Related

How do I std::cout and std::cin at the same time? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to create a console application that works as a messaging app running off of a server. I want to be able to recieve messages from a friend while I am typing my own message or while the console is awaiting my input (essentially I want to be able to cout while I cin). how would I do this? I'm assuming I will have to work on multiple threads but I cant find anything useful online.
Yes, you will have to work with multiple threads. Essentially, one thread creates and sends messages, one thread receives them. So, they work in parallel and whenever a message is received it is simply printed (or written to a log or what have you) from the reader thread, and the writer thread will constantly be waiting for user input, and will allow the user to send a message. In this code, the message is printed as soon as its received. Also, this code is highly simplified since I have no idea how your message implementation is done, so all the message specifics are left out and a generic message is just printed at some interval. This kind of implementation will also result in messages received cutting off input. You would need some kind of input space separate from the output space so they don't intercept eachother visually like this, or you could use mutexes but this defeats the purpose of getting a message instantly.
#include<iostream>
#include<thread>
#include<chrono>
void reader()
{
using namespace std::chrono_literals;
for(int i = 0; i < 10; i++)
{
std::this_thread::sleep_for(1000ms);
std::cout << "This is a message...\n";
}
}
void writer()
{
std::string message;
for(int i = 0; i < 10; i++)
{
getline(std::cin, message);
}
}
int main(void)
{
std::thread reader_thread(reader);
std::thread writer_thread(writer);
reader_thread.join();
writer_thread.join();
std::cout << "done!\n";
}
EDIT: Thought I would give more pointers on cin/cout intercepting each other. Basically, input and output happens in the same line and same place in most console implementations (Unless you use redirection or pipes or something). Most actual messaging apps have separate places for input and output. For example, you input your message somewhere, but the messages you receive show up above that. This kind of implementation means that input/output never stack on top of each other, but is also pretty much impossible to do with cin/cout. So, if you really want to do this kind of messaging application, you could use pipes or files OR you could use some libraries. Pretty much any GUI lib (Gtkmm, Qt) will let you do something like this, but if you are console-bound, ncurses will let you manipulate the console in complex ways like this so you can have one section for receiving messages and one for sending them.

How to update a variable and how to make have the updated value after the restart of the progr? [duplicate]

This question already has an answer here:
I want to store a value even after my program ends
(1 answer)
Closed 3 years ago.
I wrote an algorithm in C++, but I'd like to know how to update an int type variable, for example:
int a = 100;
a = a - 50;
So that the next time the program is started the variable is no longer 100 but 50 and so on. First of all I would like to know if it can be done.
int a = 100;
a = a - 50;
//now it's no longer 100 but 50; at the restart of the program it will be 100 again and I don't want that.
"So that the next time the program is started " - variables exist only in the running programs memory. They are not persistent between multiple runs of an application. If you want to persist/re-use data between runs, you have to save/load the data to/from a file or other persistent storage.

The correct way to make my program run at startup [duplicate]

This question already has answers here:
How to run a program automatically as admin on Windows 7 at startup?
(9 answers)
Closed 4 years ago.
(1) I want to make my program run at startup. I did that step:
I added my program path to that registry key:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run [Or]
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
Previously, the program was running at startup but after I have added the Manifest File to give my program the administrative privileges as follow:
Since that action, the program doesn't run at startup.
Finally, the code which adds the value to Software\\Microsoft\\Windows\\CurrentVersion\\Run:
// Set launch at startup setting
bool startup = wxAtoi(CPublic::getConfigItem("settings/startup"));
wxString appName = wxTheApp->GetAppName();
wxRegKey regKey(wxRegKey::HKCU, "Software\\Microsoft\\Windows\\CurrentVersion\\Run");
if (startup == 1) {
regKey.SetValue(appName, wxStandardPaths::Get().GetExecutablePath());
} else {
regKey.DeleteValue(appName);
}
What's the problem then?
(2) There is another simple question related to that question:
How to make my program hides after running at startup into the system tray?
To answer the 2nd question (the 1st one is answered in the comment by #Snetfel above), you simply need to create a wxTaskBarIcon and avoid creating any (visible) normal windows on startup.

ProgressBar lag when setting position with PBM_SETPOS [duplicate]

This question already has answers here:
Windows 7 Aero Theme Progress Bar Bug?
(7 answers)
TProgressBar never fills up all the way - seems to be updating too fast? [duplicate]
(1 answer)
Closed 8 years ago.
I have a simple C++/MFC dialog that has a progress bar control in it. I set its position with the PBM_SETPOS message, or MFC's:
//CProgressCtrl myCtrl;
myCtrl.SetPos(position);
It works fine, except when I need this position to grow fast, it seems to lag behind.
Is there any way to remove this lag?
PS. I tried my app on older version of Windows (with classic visual styles) and this lag is not present there.
The lag is by design when visual styles are enabled to provide a smoother animated experience to the user. This is a little documented but well-known issue. You cannot remove the lag, but you can work around it. The lag only happens when increasing the position but not when decreasing it. Call SetPos(position+1) followed by SetPos(position), and the bar will jump immediately. The tricky part comes at the end. When you want to set the position to the max value, you have to first increase the max value +1, then set the desired position +1, then set the real position, then finally restore the original max value. That will allow the progressbar to fill the entire bar.
int lower, upper;
myCtrl.GetRange(lower, upper);
if (position >= upper)
{
myCtrl.SetRange(lower, upper+1);
myCtrl.SetPos(upper+1);
myCtrl.SetPos(upper);
myCtrl.SetRange(lower, upper);
}
else
{
myCtrl.SetPos(position+1);
myCtrl.SetPos(position);
}

SwitchDesktop works momentarily but switches back after a moment

I've got some code to create a new desktop and launch a process into that desktop.
One a few select Windows XP machines, when this code runs, I can see it switch to the new desktop and start the process, but almost immediately, the desktop switches back to the normal desktop.
This code works fine on about 98% of machines, and I can't seem to isolate any reason for this not working on the others.
Should SwitchDesktop be reliable? Can I hook calls to SwitchDesktop that might be called from another application?
My code:
int DLL_EXP_IMP WINAPI Process_Desktop(char *szDesktopName, char *szPath)
{
HDESK hOriginalThread;
HDESK hOriginalInput;
HDESK hNewDesktop;
int procSuccess;
// Save original ...
hOriginalThread = GetThreadDesktop(GetCurrentThreadId());
hOriginalInput = OpenInputDesktop(0, FALSE, DESKTOP_SWITCHDESKTOP);
// Create a new Desktop and switch to it
hNewDesktop = CreateDesktop(szDesktopName, NULL, NULL, DF_ALLOWOTHERACCOUNTHOOK, GENERIC_ALL, NULL);
SetThreadDesktop(hNewDesktop);
SwitchDesktop(hNewDesktop);
// This call blocks until the process exits, and is confirmed to work on the affected machines
procSuccess = StartProcess(szDesktopName, szPath);
// Restore original ...
SwitchDesktop(hOriginalInput);
SetThreadDesktop(hOriginalThread);
// Close the Desktop
CloseDesktop(hNewDesktop);
if (procSuccess != 0)
{
return procSuccess;
}
else
{
return 0;
}
}
My guess is that SetThreadDesktop() fails.
From MSDN:
"The SetThreadDesktop function will fail if the calling thread has any windows or hooks on its current desktop (unless the hDesktop parameter is a handle to the current desktop)."
You mentioned that StartProcess() blocks until the process terminated.
So then there is nobody referencing the new desktop and thus the desktop will go away.
You may want to consider wrapping fallible system calls in C++
-- throwing an exception in case of they fail.
And certainly the pair CreateDesktop/CloseDesktop belongs into a C++ resource wrapper.
This is 2013!
Either SwitchDesktop is failing (most of the time is access denies, or error 170 because of existing handles in another desktop), or there is another program that switches back to the default desktop.
I know for a fact that Yahoo toolbar did this (versions 5-6-7, perhaps they fixed now); KABE4.exe (I don't know what this is), an Acronis program (backup scheduler, AFAIK), and more. All of these are calling SwitchDesktop without any user intervention (a big no-no).
I proved this for Yahoo toolbar; hooking the SwitchDesktop by injecting another dll into yt.dll (loaded by IE) and returning FALSE from the hooked call solved my problem.
The proof of concept sent almost 2 years ago to Yahoo remained unanswered to this day.
In your posted code, there is that part:
// Create a new Desktop and switch to it
hNewDesktop = CreateDesktop(szDesktopName, NULL, NULL, DF_ALLOWOTHERACCOUNTHOOK, GENERIC_ALL, NULL);
SetThreadDesktop(hNewDesktop);
SwitchDesktop(hNewDesktop);
// This call blocks until the process exits, and is confirmed to work on the affected machines
procSuccess = StartProcess(szDesktopName, szPath);
// Restore original ...
SwitchDesktop(hOriginalInput);
SetThreadDesktop(hOriginalThread);
Your call to StartProcess function is between two calls to SwitchDesktop.
No function in this code stop (pause) or delay the running code, thread or process, so as you switch to hNewDesktop, you immediately switch back to hOriginalInput. You should add a while loop with end condition, after the call to StartProcess, and before the second call to SwitchDesktop. I don't know what will be the end condition for the while loop, but you do know, you will choose, after all it is your program.
For example you can use either GetKeyState or GetAsyncKeyState function to check which key is pressed on the keyboard, and make it as the end condition for the while loop, so when you will press that key, you will return immediately to your original desktop!