--- pgAgent-NG/win32.cpp	2013-09-11 07:56:01.132861731 +0200
+++ pgagent/win32.cpp	2013-09-11 12:02:08.414191462 +0200
@@ -14,8 +14,8 @@
 // This is for Win32 only!!
 #ifdef WIN32
 
-//#include <wx/filename.h>
-//#include <wx/config.h>
+#include <wx/filename.h>
+#include <wx/config.h>
 #include <process.h>
 
 // for debugging purposes, we can start the service paused
@@ -25,8 +25,8 @@
 
 static SERVICE_STATUS serviceStatus;
 static SERVICE_STATUS_HANDLE serviceStatusHandle;
-static std::string serviceName;
-static std::string user = (".\\Administrator"), password;
+static wxString serviceName;
+static wxString user = wxT(".\\Administrator"), password;
 static HANDLE threadHandle = 0;
 
 
@@ -57,14 +57,14 @@
 	serviceIsRunning = true;
 }
 
-void LogMessage(std::string msg, int level)
+void LogMessage(wxString msg, int level)
 {
 	if (eventHandle)
 	{
 		LPCTSTR *tmp;
 
 		tmp = (LPCTSTR *)malloc(sizeof(LPCTSTR));
-        tmp[0] = _wcsdup(msg.c_str());
+		tmp[0] = _wcsdup(msg.wc_str());
 
 		switch (level)
 		{
@@ -105,20 +105,20 @@
 		{
 			case LOG_DEBUG:
 				if (minLogLevel >= LOG_DEBUG)
-                    std::cout << std::string("DEBUG: ") + msg + "\n" << std::endl;
+					wxPrintf(_("DEBUG: %s\n"), msg);
 				break;
 			case LOG_WARNING:
 				if (minLogLevel >= LOG_WARNING)
-                std::cout << std::string("WARNING: ") + msg + "\n" << std::endl;
+					wxPrintf(_("WARNING: %s\n"), msg);
 				break;
 			case LOG_ERROR:
-                std::cout << std::string("ERROR: ") + msg + "\n" << std::endl;
+				wxPrintf(_("ERROR: %s\n"), msg);
 				pgagentInitialized = true;
 				exit(1);
 				break;
 				// Log startup/connection warnings (valid for any log level)
 			case LOG_STARTUP:
-                std::cout << std::string("WARNING: ") + msg + "\n" << std::endl;
+				wxPrintf(_("WARNING: %s\n"), msg);
 				break;
 		}
 	}
@@ -128,8 +128,7 @@
 
 unsigned int __stdcall threadProcedure(void *unused)
 {
-    connInfo *serviceConnInfo = new connInfo();
-    MainLoop(serviceConnInfo);
+	MainLoop();
 	return 0;
 }
 
@@ -140,7 +139,7 @@
 // _popen doesn't work in Win2K from a service so we have to
 // do it the fun way :-)
 
-HANDLE win32_popen_r(const TCHAR *command)
+HANDLE win32_popen_r(const TCHAR *command, HANDLE &handle)
 {
 	HANDLE hWrite, hRead;
 	SECURITY_ATTRIBUTES saAttr;
@@ -186,16 +185,13 @@
 	if (!ret)
 		return NULL;
 	else
-	{
-		CloseHandle(piProcInfo.hProcess);
 		CloseHandle(piProcInfo.hThread);
-	}
-
 
 	// Close the write end of the pipe and return the read end.
 	if (!CloseHandle(hWrite))
 		return NULL;
 
+	handle = piProcInfo.hProcess;
 	return hRead;
 }
 
@@ -326,7 +322,7 @@
 
 ////////////////////////////////////////////////////////////
 // installation and removal
-bool installService(const std::string &serviceName, const std::string &executable,  const std::string &args, const std::string &displayname, const std::string &user, const std::string &password)
+bool installService(const wxString &serviceName, const wxString &executable,  const wxString &args, const wxString &displayname, const wxString &user, const wxString &password)
 {
 	DWORD dwData;
 	bool done = false;
@@ -334,11 +330,11 @@
 	SC_HANDLE manager = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS);
 	if (manager)
 	{
-        std::string cmd = executable + wxT(" ") + args;
+		wxString cmd = executable + wxT(" ") + args;
 
-        std::string quser;
-        if (!user.find_first_of("\\"))
-            quser = std::string(".\\") + user;
+		wxString quser;
+		if (!user.Contains(wxT("\\")))
+			quser = wxT(".\\") + user;
 		else
 			quser = user;
 
@@ -365,7 +361,7 @@
 			    (LPTSTR) &lpMsgBuf,
 			    0, NULL
 			);
-            std::string error;
+			wxString error;
 			error.Printf(wxT("%s"), lpMsgBuf);
 			LogMessage(error, LOG_ERROR);
 		}
@@ -374,29 +370,28 @@
 	}
 
 	// Setup the event message DLL
-//TODO - THST - Make the MS Way
 	wxRegKey *msgKey = new wxRegKey(wxT("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\") + serviceName);
 	if(!msgKey->Exists())
 	{
 		if (!msgKey->Create())
-            LogMessage(("Could not open the message source registry key."), LOG_WARNING);
+			LogMessage(_("Could not open the message source registry key."), LOG_WARNING);
 	}
 
-    std::string path = executable.BeforeLast('\\') + std::string("\\pgaevent.dll");
+	wxString path = executable.BeforeLast('\\') + wxT("\\pgaevent.dll");
 
-    if (!msgKey->SetValue(std::string("EventMessageFile"), path))
-        LogMessage(("Could not set the event message file registry value."), LOG_WARNING);
+	if (!msgKey->SetValue(wxT("EventMessageFile"), path))
+		LogMessage(_("Could not set the event message file registry value."), LOG_WARNING);
 
 	dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
 
-    if (!msgKey->SetValue(std::string("TypesSupported"), dwData))
-        LogMessage(("Could not set the supported types."), LOG_WARNING);;
+	if (!msgKey->SetValue(wxT("TypesSupported"), dwData))
+		LogMessage(_("Could not set the supported types."), LOG_WARNING);;
 
 	return done;
 }
 
 
-bool removeService(const std::string &serviceName)
+bool removeService(const wxString &serviceName)
 {
 	bool done = false;
 
@@ -429,7 +424,6 @@
 	}
 
 	// Remove the event message DLL
-//TODO - THST - Make the MS Way
 	wxRegKey *msgKey = new wxRegKey(wxT("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\") + serviceName);
 	msgKey->DeleteSelf();
 
@@ -438,30 +432,28 @@
 
 
 
-void usage(const std::string &executable)
+void usage(const wxString &executable)
 {
-    //wxFileName *fn = new wxFileName(executable);
-    size_t pos = executable.find_last_of('\\');
-    std::string fn = executable.substr(pos,executable.size()-pos);
-
-    std::cout << "Usage:\n" << std::endl;
-    std::cout << fn << (" REMOVE <serviceName>\n" << std::endl;
-    std::cout << fn << (" INSTALL <serviceName> [options] <connect-string>\n" << std::endl;
-    std::cout << fn << (" DEBUG [options] <connect-string>\n" << std::endl;
-    std::cout << "options:\n" << std::endl;
-    std::cout << "-u <user or DOMAIN\\user>\n" << std::endl;
-    std::cout << "-p <password>\n" << std::endl;
-    std::cout << "-d <displayname>\n" << std::endl;
-    std::cout << "-t <poll time interval in seconds (default 10)>\n" << std::endl;
-    std::cout << "-r <retry period after connection abort in seconds (>=10, default 30)>\n" << std::endl;
-    std::cout << "-l <logging verbosity (ERROR=0, WARNING=1, DEBUG=2, default 0)>\n" << std::endl;
+	wxFileName *fn = new wxFileName(executable);
+
+	wxPrintf(_("Usage:\n"));
+	wxPrintf(fn->GetName() + _(" REMOVE <serviceName>\n"));
+	wxPrintf(fn->GetName() + _(" INSTALL <serviceName> [options] <connect-string>\n"));
+	wxPrintf(fn->GetName() + _(" DEBUG [options] <connect-string>\n"));
+	wxPrintf(_("options:\n"));
+	wxPrintf(_("-u <user or DOMAIN\\user>\n"));
+	wxPrintf(_("-p <password>\n"));
+	wxPrintf(_("-d <displayname>\n"));
+	wxPrintf(_("-t <poll time interval in seconds (default 10)>\n"));
+	wxPrintf(_("-r <retry period after connection abort in seconds (>=10, default 30)>\n"));
+	wxPrintf(_("-l <logging verbosity (ERROR=0, WARNING=1, DEBUG=2, default 0)>\n"));
 }
 
 
 
 ////////////////////////////////////////////////////////////
 
-void setupForRun(int argc, char **argv, bool debug, const std::string &executable)
+void setupForRun(int argc, char **argv, bool debug, const wxString &executable)
 {
 	if (!debug)
 	{
@@ -479,9 +471,9 @@
 	// Statup wx
 	wxInitialize();
 
-    wxFileName file = std::string::FromAscii(*argv++);
+	wxFileName file = wxString::FromAscii(*argv++);
 	file.MakeAbsolute();
-    std::string executable = file.GetFullPath();
+	wxString executable = file.GetFullPath();
 
 	if (argc < 3)
 	{
@@ -489,21 +481,21 @@
 		return;
 	}
 
-    std::string command;
-    command = std::string(*argv++);
+	wxString command;
+	command = wxString::FromAscii(*argv++);
 
-    if (command != std::string("DEBUG"))
+	if (command != wxT("DEBUG"))
 	{
-        serviceName = std::string(*argv++);
+		serviceName = wxString::FromAscii(*argv++);
 		argc -= 3;
 	}
 	else
 		argc -= 2;
 
-    if (command == std::string("INSTALL"))
+	if (command == wxT("INSTALL"))
 	{
-        std::string displayname = ("PostgreSQL Scheduling Agent - ") + serviceName;
-        std::string args = std::string("RUN ") + serviceName;
+		wxString displayname = _("PostgreSQL Scheduling Agent - ") + serviceName;
+		wxString args = wxT("RUN ") + serviceName;
 
 		while (argc-- > 0)
 		{
@@ -528,14 +520,14 @@
 					}
 					default:
 					{
-                        args += wxT(" ") + std::string(*argv);
+						args += wxT(" ") + wxString::FromAscii(*argv);
 						break;
 					}
 				}
 			}
 			else
 			{
-                args += wxT(" ") + std::string(*argv);
+				args += wxT(" ") + wxString::FromAscii(*argv);
 			}
 
 			argv++;
@@ -543,11 +535,11 @@
 
 		bool rc = installService(serviceName, executable, args, displayname, user, password);
 	}
-    else if (command == std::string("REMOVE"))
+	else if (command == wxT("REMOVE"))
 	{
 		bool rc = removeService(serviceName);
 	}
-    else if (command == std::string("DEBUG"))
+	else if (command == wxT("DEBUG"))
 	{
 		setupForRun(argc, argv, true, executable);
 
@@ -558,12 +550,12 @@
 
 		WaitForSingleObject(threadHandle, INFINITE);
 	}
-    else if (command == std::string("RUN"))
+	else if (command == wxT("RUN"))
 	{
-        std::string app = std::string("pgAgent Service");
+		wxString app = _("pgAgent Service");
 
 		SERVICE_TABLE_ENTRY serviceTable[] =
-        { (LPWSTR)app.c_str(), serviceMain, 0, 0};
+		{ (LPWSTR)app.wc_str(), serviceMain, 0, 0};
 
 		setupForRun(argc, argv, false, executable);
 
