hello everyone
i have a huge problem developing a windows service
i'm trying to develop some app that reads information from WMI and sends it via amqp (using apache qpid client)
everything works fine on windows xp, but problems appear on vista:
when i start the program i get an insant error 1053, and not even one line of code runs (i tried writing some output in a file, that doesn't execute)
the ONLY way it runs as a service, is if i set it to login (services > testService > logon > user/pass) with my user (and not with localsystem)
i did further testing and got out that it ONLY runs with a logged user (if i set it to run with user2/pass2, and he's not logged on pc it won't start, if i do switch user and login with user2, and come back to my user with switch user, then start the service , it works)
so if i set it to automatic and restart the computer it won't start even if it's set with my logged user because services start before the login of users.
here's what i tried:
if you look through the code you'll see an ifndef there that disables the wmi execution. if i disable it, it works (with running qpid)
same goes with qpid, if i dont include qpid and leave only wmi, it also works
all the dll's are in place, the boost library dll's, the qpid dll's are in the executable's folder
if i
i'm out of ideas
here's the code
WMI.cpp
//#define WMIDISABLE
#include "WMI.h"
int WMI::start() {
#ifndef WMIDISABLE
HRESULT hres;
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
return 1;
}
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres)) {
CoUninitialize();
return 1;
}
pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres)) {
CoUninitialize();
return 1;
}
pSvc = NULL;
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
pLoc->Release();
CoUninitialize();
return 1;
}
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
pSvc->Release();
pLoc->Release();
CoUninitialize();
#endif
return 0;
}
WMI.h
#ifndef _WMI_H
#define _WMI_H
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
#include <boost/thread/mutex.hpp>
# pragma comment(lib, "wbemuuid.lib")
class WMI {
public:
int start();
IWbemServices *pSvc;
IWbemLocator *pLoc;
};
#endif
main.cpp
#include "WMI.h"
#include <qpid/client/Connection.h>
#include <qpid/client/Session.h>
#include <qpid/client/SubscriptionManager.h>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
using namespace qpid::client;
using namespace qpid::framing;
#include <windows.h>
#include <tchar.h>
#include <conio.h>
TCHAR* serviceName = TEXT("testService");
SERVICE_STATUS serviceStatus;
SERVICE_STATUS_HANDLE serviceStatusHandle;
HANDLE stopServiceEvent = 0;
void WINAPI ServiceControlHandler(DWORD controlCode)
{
switch(controlCode)
{
case SERVICE_CONTROL_INTERROGATE:
break;
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
SetEvent(stopServiceEvent);
case SERVICE_CONTROL_PAUSE:
break;
case SERVICE_CONTROL_CONTINUE:
break;
default:
if (controlCode >= 128 && controlCode <= 255)
break; //user defined
else
break; //unrecognised
}
SetServiceStatus(serviceStatusHandle, &serviceStatus);
}
void WINAPI ServiceMain(DWORD argc, TCHAR* argv[])
{
serviceStatus.dwServiceType = SERVICE_WIN32;
serviceStatus.dwCurrentState = SERVICE_STOPPED;
serviceStatus.dwControlsAccepted = 0;
serviceStatus.dwWin32ExitCode = NO_ERROR;
serviceStatus.dwServiceSpecificExitCode = NO_ERROR;
serviceStatus.dwCheckPoint = 0;
serviceStatus.dwWaitHint = 0;
serviceStatusHandle = RegisterServiceCtrlHandler(serviceName, ServiceControlHandler);
if (serviceStatusHandle)
{
serviceStatus.dwCurrentState = SERVICE_START_PENDING;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
stopServiceEvent = CreateEvent(0, FALSE, FALSE, 0);
serviceStatus.dwControlsAccepted |= (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
serviceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
try {
Connection connection;
connection.open(
"10.10.5.51",
5672,
"user",
"pass"
);
} catch(...) {
}
WMI wmi;
wmi.start();
do
{
Sleep(1000);
}
while (WaitForSingleObject(stopServiceEvent, 1) == WAIT_TIMEOUT);
serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
CloseHandle(stopServiceEvent);
stopServiceEvent = 0;
serviceStatus.dwControlsAccepted &= ~(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
serviceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
}
}
void RunService()
{
SERVICE_TABLE_ENTRY serviceTable[2];
serviceTable[0].lpServiceName = serviceName;
serviceTable[0].lpServiceProc = ServiceMain;
serviceTable[1].lpServiceName = NULL;
serviceTable[1].lpServiceProc = NULL;
StartServiceCtrlDispatcher(serviceTable);
}
void InstallService()
{
SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
if (serviceControlManager)
{
TCHAR path[_MAX_PATH - 1];
int gmfn = GetModuleFileName(0, path, sizeof(path)/sizeof(path[0]));
if (GetModuleFileName(0, path, sizeof(path)/sizeof(path[0]) ) > 0)
{
SC_HANDLE service = CreateService(
serviceControlManager,
serviceName,
serviceName,
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_IGNORE,
path,
0,
0,
0,
0,
0);
if (service)
{
CloseServiceHandle(service);
}
}
CloseServiceHandle(serviceControlManager);
}
}
void UninstallService()
{
SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT);
if (serviceControlManager)
{
SC_HANDLE service = OpenService(
serviceControlManager,
serviceName,
SERVICE_QUERY_STATUS | DELETE);
if (service)
{
SERVICE_STATUS serviceStatus;
if (QueryServiceStatus(service, &serviceStatus))
{
if (serviceStatus.dwCurrentState == SERVICE_STOPPED)
DeleteService(service);
}
CloseServiceHandle(service);
}
CloseServiceHandle(serviceControlManager);
}
}
int _tmain(int argc, TCHAR* argv[])
{
if (argc > 1 && lstrcmpi(argv[1], TEXT("install")) == 0)
{
InstallService();
}
else if (argc > 1 && lstrcmpi(argv[1], TEXT("uninstall")) == 0)
{
UninstallService();
}
else
{
RunService();
}
return 0;
}