#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <windows.h>

/* 
Code based on discussion: "How to send CTRL+C signal to detached command-line process?"
http://social.msdn.microsoft.com/Forums/en-US/windowsgeneraldevelopmentissues/thread/dc9586ab-1ee8-41aa-a775-cf4828ac1239
*/

class Error
{
public:
	Error(const std::string& message) : _message(message) {};
	friend std::ostream& operator<<(std::ostream& stream, Error& err);
private:
	std::string _message;
};
std::ostream& operator<<(std::ostream& stream, Error& err) { stream << err._message; return stream; }

int main(int argc, char* argv[])
{
	try
	{
		std::string eventName = "PiAnoS_windows_server_apache_stop_event";
		std::cout << "Creating global IPC event '" << eventName << "'" << std::endl;
		HANDLE cancelEvent = CreateEvent(NULL, FALSE, FALSE, eventName.c_str());
		if (cancelEvent == INVALID_HANDLE_VALUE)
		{
			DWORD err = GetLastError();
			std::stringstream ss;
			ss << "CreateEvent() failed with error code " << err << std::endl;
			throw Error(ss.str());
		}

		BOOL ret = SetEvent(cancelEvent);
		if (ret == 0)
		{
			DWORD err = GetLastError();
			std::stringstream ss;
			ss << "SetEvent() failed with error code " << err << std::endl;
			throw Error(ss.str());
		}

		return 0;
	}
	catch (Error& err)
	{
		std::cout << "ERROR: " << err << std::endl;
		return 1;
	}
}
