1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
   |  
// avec _WIN32_DCOM a ajouter dans "preprocessors definitions"
 
#include "stdafx.h"
 
#include <Winsvc.h>
#include <stdio.h>
 
// Ole db
#include "atldbcli.h"
#include "afxdb.h"
#include <comdef.h>
// Log
#include "Log.h"
 
 
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
 
CWinApp theApp;
 
using namespace std;
 
// Props
SERVICE_STATUS	ServiceStatus; 
SERVICE_STATUS_HANDLE hStatus;
CDataSource	m_dbDataSource;
CSession	m_dbSession;
 
// Methods
bool		Connect();
void		ServiceMain(int argc, char** argv); 
void		ControlHandler(DWORD request); 
HRESULT		InitService();
 
// ************************************************
// Service initialization
// ************************************************
HRESULT InitService() 
{
  HRESULT hr = S_OK;
  CLog::IniLog();
  CLog::WriteMessage("InitService.");
  hr = CoInitializeEx (NULL, COINIT_MULTITHREADED);
  return hr; 
} 
 
// ************************************************
// Service Main
// ************************************************
void ServiceMain(int argc, char** argv) 
{ 
  BOOL bCtlStarted = FALSE;
  HRESULT hr;
 
  ServiceStatus.dwServiceType        = SERVICE_WIN32; 
  ServiceStatus.dwCurrentState       = SERVICE_START_PENDING; 
  ServiceStatus.dwControlsAccepted   = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
  ServiceStatus.dwWin32ExitCode      = 0; 
  ServiceStatus.dwServiceSpecificExitCode = 0; 
  ServiceStatus.dwCheckPoint         = 0; 
  ServiceStatus.dwWaitHint           = 0; 
 
  hStatus = RegisterServiceCtrlHandler(
			_T("Servicetest"), 
			(LPHANDLER_FUNCTION)ControlHandler);
 
  if( hStatus == (SERVICE_STATUS_HANDLE)0 ) { 
    // Registering Control Handler failed
    return; 
  } 
  // Initialize Service 
  hr = InitService();
 
  if( FAILED(hr) ) {
      // Initialization failed
      ServiceStatus.dwCurrentState       = SERVICE_STOPPED; 
      ServiceStatus.dwWin32ExitCode      = -1; 
      SetServiceStatus(hStatus, &ServiceStatus); 
      return; 
  } 
  // We report the running status to SCM. 
  ServiceStatus.dwCurrentState = SERVICE_RUNNING; 
  SetServiceStatus (hStatus, &ServiceStatus);
      // -------------------------
      // TODO : job here
      try {
        if( Connect() ) {
          CLog::WriteMessage(_T("Connected to db !\nClose connection..."));
          m_dbSession.Close();
          m_dbDataSource.Close();
        } else {
          CLog::WriteMessage(_T("Unconnected... :( "));
        }
      } catch(...) {
      }
      // -------------------------
 
    // Stop the service
    ServiceStatus.dwCurrentState       = SERVICE_STOPPED; 
    SetServiceStatus (hStatus, &ServiceStatus);
 
  return; 
}
 
 
// ************************************************
// Control handler function
// ************************************************
void ControlHandler(DWORD request) 
{ 
  switch(request) 
    { 
        case SERVICE_CONTROL_STOP:
            ServiceStatus.dwWin32ExitCode = 0; 
            ServiceStatus.dwCurrentState  = SERVICE_STOPPED; 
            SetServiceStatus (hStatus, &ServiceStatus);
            return; 
 
        case SERVICE_CONTROL_SHUTDOWN: 
            ServiceStatus.dwWin32ExitCode = 0; 
            ServiceStatus.dwCurrentState  = SERVICE_STOPPED; 
            SetServiceStatus (hStatus, &ServiceStatus);
            return; 
 
        default:
            break;
    } 
 
    // Report current status
    SetServiceStatus (hStatus,  &ServiceStatus);
 
    return;
}
 
// ************************************************
// Connect to a database trough OLE DB using 
// default connection parameters
// ************************************************
bool Connect()
{
  HRESULT hr = S_OK;
 
  CString strProvider("OraOLEDB.Oracle");
  CComBSTR bstrDatabase("DB");
  CComBSTR bstrUser("LOGIN");
  CComBSTR bstrPassword("PWD");
 
  CDBPropSet  dbinit(DBPROPSET_DBINIT);
  dbinit.AddProperty(DBPROP_AUTH_PASSWORD, bstrPassword);
  dbinit.AddProperty(DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, false);
  dbinit.AddProperty(DBPROP_AUTH_USERID, bstrUser);
  dbinit.AddProperty(DBPROP_INIT_DATASOURCE, bstrDatabase);
  dbinit.AddProperty(DBPROP_INIT_LCID, (long)1049);
  dbinit.AddProperty(DBPROP_INIT_PROMPT, (short)4);
 
  hr = m_dbDataSource.Open(strProvider, &dbinit);
  if(FAILED(hr)) {
    CLog::WriteDcomError(hr, _T("Connect >"));
    m_dbDataSource.Close();
    return false;
  } else {
    CLog::WriteMessage(_T("Connect Ok"));
    if ( m_dbSession.Open(m_dbDataSource) != S_OK) {
      return false;
    }
  }
  return true;
}
 
 
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
  int nRetCode = 0;
 
  // initialize MFC and print and error on failure
  if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  {
    // TODO: change error code to suit your needs
    cerr << _T("Fatal Error: MFC initialization failed") << endl;
    nRetCode = 1;
  }
  else
  {
    SERVICE_TABLE_ENTRY ServiceTable[2];
    ServiceTable[0].lpServiceName = _T("ServiceTest");
    ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
    ServiceTable[1].lpServiceName = NULL;
    ServiceTable[1].lpServiceProc = NULL;
    // Start the control dispatcher thread for our service
    StartServiceCtrlDispatcher(ServiceTable);
  }
  return nRetCode;
} | 
Partager