Raising/Setting WIN32 event - c++

I created a small Windows application that creates an event and waits for it to be triggered, using the CreateEvent() and WaitForSingleObject() Win32 APIs.
I also created another application that is used to trigger the event that was created by the previously mentioned application, using the SetEvent() API.
They work well with each other, but I am looking to simplify how the triggering is done. Is there a way via command prompt or PowerShell to trigger an event that was created by a Win32 C++ application? I want to remove the need for maintaining an additional application.
I've tried looking at what PowerShell provides, but have not had much luck yet.

Is there a way via command prompt or PowerShell to trigger an event that was created by a Win32 C++ application?
PowerShell scripts have access to .NET classes, so you can use the EventWaitHandle class to open and trigger the C++ app's event.
$SetEventName = 'MyEvent'
function Signal-SetEvent {
$SetEventObject = [System.Threading.EventWaitHandle]::OpenExisting( $SetEventName )
$SetEventObject.Set()
}

Related

How to create a event log at "Application and Services Logs" section using c++

I want to write logs in Event viewer under "Applications and Services logs"section using c++. But I cannot find any interface about it, Does anybody know?
I have tried "RegisterEventSource", "ReportEvent", but i can only write event viewer logs in Applications but not under Applications and Services logs.
I also find some samples in C#, but my application is in C++.
Thanks.
You can have a look at the EventWrite function. It is contained in the evntprov.h. This allows you to write a specific entry to a Register Handle. By using EventRegister you can create a Register Handle. Remember to call EventUnregsiter when you are done with the Register Handle.
I have unfortunately not worked with this in quite a while. Have a read over at Windows Docs about evntprov.h and all its functionality. I am sure it will point you in the correct direction.

Custom Event Listener in C++

In a command line application which is single threaded, I want to know when user closes (by clicking on red cross on top right) shutdowns PC or Logoff without logging out of my application first, so that I can clear the cookies that I am storing in window's registry.
Is there any way I can know when user has done the events mentioned above and call a function within my application or call a separate EXE or a Batch file following the event ?
I need this functionality because I want to prevent a possible scenario where in Registry is full of unwanted thrash created by not logging out of application properly.
Since my application runs over command prompt likeC:\Users\admin\Desktop>Application.exe -task "ConnectServer" --ip 127.0.0.1 I am looking out for some way to either manage cookies in efficient way or to delete the cookies after catching the events mentioned above.
You could use SetConsoleCtrlHandler()

Simple online game servers monitor

I want to make a tool that runs on win32 to monitor our online game servers. The servers actually are .exe files. I need to know whether they have crashed so I can restart them. Therefore, the tool will have 2 main features:
Frequently check a list of the server programs to see whether they are running or not
Reopen the executable of any server that has crashed
Does anyone have any idea or knows an API to start with?
If you're in Win32, you can start out with C# using System.Diagnostics
using System.Diagnostics;
Then get a process list:
Process[] processlist = Process.GetProcesses();
foreach(Process theprocess in processlist){
Console.WriteLine(“Process: {0} ID: {1}”, theprocess.ProcessName, theprocess.Id);
}
And from there it's up to you what you want to do with the info.
Game servers should produce logs. You should make a service / cron job to monitor the logs. Depends on the contents of logs, your service should act respective actions, e.g. restart service, trigger alarms, etc.
I realize that this is not exactly what you asked for, but what about doing the monitoring with a fully-fledged monitoring tool such as Nagios? You would of course have to "teach" the monitoring tool about the processes that shall be monitored but you would also profit from its more advanced functions. In the case of Nagios, for example, these would comprise automatic e-mail notifications, an online dashboard of process status, notifications via SMS etc.
You should start the game servers using CreateProcess or similar in the win32 api.
You will get back a process handle in the lpProcessInformation parameter.
You can use WaitForSingleObject to WaitForMultipleObjects to wait until that handle is notified which will happen when the process terminates for any reason.

Wait for Service to Start/Stop

I am using the service controller in C++ to manage a windows service.
I can use the StartService( ) and ControlService( ) functions to start and stop the service.
However, is there a standard way to wait for the service to actually start and stop?
I could obviously loop calling QueryServiceStatusEx( ) and wait until the status is Running or Stopped respectively.
Is there a neater way of achieving the same?
Thanks.
Afaik Service start and stop create windows event log events. Maybe you could install a handler for those and wait for the event from your application. If your application is also a service mark it as depending on the 3rd party service then you should not have to wait on it.
In C# you can do this using ServiceController.WaitForStatus. You could do it this way (one line of code) and wrap the C# within your C++ code.
Otherwise, you will likely be futzing around with WMI (Win32_Service class) or the Event Log.

Windows Service with GUI monitor?

I have a C++ Win32 application that was written as a Windows GUI project, and now I'm trying to figure out to make it into a Service / GUI hybrid. I understand that a Windows Service cannot / should not have a user interface. But allow me to explain what I have so far and what I'm shooting for.
WHAT I HAVE NOW is a windows application. When it is run it places an icon in the system tray that you can double-click on to open up the GUI. The purpose of this application is to process files located in a specified directory on a nightly schedule. The GUI consists of the following:
A button to start an unscheduled scan/process manually.
A button to open a dialog for modifying settings.
A List Box for displaying status messages sent from the processing thread.
A custom drawn window for displaying image data (the file processing includes the creation and saving of images).
A status bar - while a process is not running, it shows a countdown to the next scheduled scan. During a scan it also provides some status feedback, including a progress bar.
WHAT I'M SHOOTING FOR is a service that will run on boot-up and not require a user to login. This would consist of the scheduled file processing. However, when a user logs in I would still like the tray icon to be loaded and allow them to open up a GUI as I described above to monitor the current state of the service, change settings, start a scan manually, and monitor the progress of a scan.
I'm sure that I have seen applications like this - that function as a service even when I'm not logged in, but still give me a user interface to work with once I do log in.
I'm thinking that instead of having a single multi-threaded application that sends messages to the GUI thread from the processing thread, I need two applications - a Service to perform the processing and a GUI application to provide visual feedback from the Service and also send messages to the Service (for example, to start a scan manually). But I am new to Windows Services and have no idea how this is done.
It is also possible that I'm completely off base and a Service is not what I'm looking for at all.
Any help / ideas / suggestions would be greatly appreciated! Thank you.
You can't do this as a service.
You'll need to make your Windows Service as a normal service application. This will startup on system startup, and run the entire time the system is up.
You'd then make a completely separate GUI application, which "talks" to the service. This can be set to run when a user logs in, in the user's account.
In order to make them "talk" to each other, you'll need to use some form of IPC. Since these run on the same system (but in different accounts, typically), named pipes or sockets both work quite well.
There is a simple way of doing it.
You can’t have the service access any user’s session (session 1,2,3..) since services are isolated and can access session 0 only. This is a change from 2011.
You should write a win32 program to be launched by your service per each user who logs in using https://msdn.microsoft.com/en-us/library/windows/desktop/ms682429(v=vs.85).aspx
The service can continue performing any task that isn’t user specific.