🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #1648

Re: TService Compile Error

#1648
From: Jeff
Date: Tue, 26 Jun 2007 17:07
264 lines
8397 bytes
> Then the only other possibility I can think of right now without
> seeing more of your code is that you are likely missing a closing '}'
> bracket somewhere.

I double checked my brackets and found none missing, so here is my entire 
UnitService.cpp file:

//---------------------------------------------------------------------------
#include "UnitService.h"
#include "Registry.hpp"
#include "UnitMainThread.h"
#include "UnitLogThread.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TsrvMain *srvMain;
//---------------------------------------------------------------------------
const char *REGKEY = "\\Software\\XXXX\\Main";
const char *SRVDESC = "Testing Service application";
const int DEFHOUR = 21; //Default to 9:00 PM
const int DEFMIN = 0;
//---------------------------------------------------------------------------
__fastcall TsrvMain::TsrvMain(TComponent* Owner)
 : TService(Owner)
{
}

TServiceController __fastcall TsrvMain::GetServiceController(void)
{
 return (TServiceController) ServiceController;
}

void __stdcall ServiceController(unsigned CtrlCode)
{
 srvMain->Controller(CtrlCode);
}
//---------------------------------------------------------------------------

void __fastcall TsrvMain::ServiceExecute(TService *Sender)
{
 thdMain *ptr_thdMain;
 thdLogUsers *ptr_thdLog;

 try
 {
  while (!Terminated)
  {
   if (IsTime())
   {
    ptr_thdMain = new thdMain(true); //Run the worker thread
    Sleep(1000); //Ensure time has passed, so we don't get hundreds of 
threads
    ptr_thdMain->Resume();

    //Log database usage if we should
    if (boolLogUsers)
    {
     ptr_thdLog = new thdLogUsers(strEmailRecip, strServer, true);
     Sleep(1000);
     ptr_thdLog->Resume();
    }
   }

   Sleep(10);
   srvMain->ServiceThread->ProcessRequests(false);
  }
 }__finally {
  ptr_thdMain->Terminate();
  ptr_thdLog->Terminate();
  delete ptr_thdMain;
  delete ptr_thdLog;
  }
}
//---------------------------------------------------------------------------

bool __fastcall TsrvMain::IsTime()
{
 SYSTEMTIME stTime;

 GetLocalTime(&stTime);

 if ((stTime.wHour == wHour) && (stTime.wMinute == wMin) && (stTime.wSecond 
== 0))
  return true;
 else
  return false;
}
//---------------------------------------------------------------------------

void __fastcall TsrvMain::ServiceStart(TService *Sender, bool &Started)
{
 TRegistry *ptr_Reg = new TRegistry();

 try
 {
  ptr_Reg->RootKey = HKEY_LOCAL_MACHINE;
  ptr_Reg->OpenKey(REGKEY, true);

  if (ptr_Reg->ValueExists("Hour"))
   wHour = ptr_Reg->ReadInteger("Hour");
  else
  {
   wHour = DEFHOUR; //Default
   ptr_Reg->WriteInteger("Hour", DEFHOUR);
  }

  if (ptr_Reg->ValueExists("Minute"))
   wMin = ptr_Reg->ReadInteger("Minute");
  else
  {
   wMin = DEFMIN; //Default
   ptr_Reg->WriteInteger("Minute", DEFMIN);
  }

  if (ptr_Reg->ValueExists("LogUsers"))
  {
   boolLogUsers = ptr_Reg->ReadBool("LogUsers");
   if (boolLogUsers)
   {
    if (ptr_Reg->ValueExists("EmailRecip"))
     strEmailRecip = ptr_Reg->ReadString("EmailRecip");
    if (ptr_Reg->ValueExists("EmailServer"))
     strServer = ptr_Reg->ReadString("EmailServer");
   }
  }
  else
   boolLogUsers = false;

 }__finally {
  ptr_Reg->CloseKey();
  delete ptr_Reg;
  }
}
//---------------------------------------------------------------------------

void __fastcall TsrvMain::ServiceAfterInstall(TService *Sender)
{
 LPSERVICE_DESCRIPTION sdService = (LPSERVICE_DESCRIPTION) LocalAlloc(LPTR, 
4096);
 SERVICE_STATUS_PROCESS ssStatus;
    DWORD dwOldCheckPoint;
 DWORD dwStartTickCount;
    DWORD dwWaitTime;
 DWORD dwBytesNeeded;
 SC_HANDLE schManager; //Service Control Manager Handle
 SC_HANDLE schService; //Service Handle

 try
 {
  try
  {
   schManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
   schService = OpenService(schManager, srvMain->Name.c_str(), 
SERVICE_ALL_ACCESS);

   //Set the service description
   sdService->lpDescription = (char *)SRVDESC;
   ChangeServiceConfig2(schService, SERVICE_CONFIG_DESCRIPTION, sdService); 
//Set the description

   //Start the service
   StartService(schService, 0, NULL);

   //Check the pending status
   QueryServiceStatusEx(schService, SC_STATUS_PROCESS_INFO, (unsigned char 
*)&ssStatus, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded);

   //Save the tick count and initial checkpoint.
   dwStartTickCount = GetTickCount();
   dwOldCheckPoint = ssStatus.dwCheckPoint;

   while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
   {
    //Do not wait longer than the wait hint. A good interval is
    //one tenth the wait hint, but no less than 1 second and no
    //more than 10 seconds.

    dwWaitTime = ssStatus.dwWaitHint / 10;

    if(dwWaitTime < 1000)
     dwWaitTime = 1000;
    else if (dwWaitTime > 10000)
     dwWaitTime = 10000;

    Sleep(dwWaitTime);

    //Check the status again.
    if (!QueryServiceStatusEx(schService, SC_STATUS_PROCESS_INFO, 
(LPBYTE)&ssStatus, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded))
     break;

    if (ssStatus.dwCheckPoint > dwOldCheckPoint) //The service is making 
progress.
    {
     dwStartTickCount = GetTickCount();
     dwOldCheckPoint = ssStatus.dwCheckPoint;
    }
    else
    {
     if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint) //No progress 
made within the wait hint
      break;
    }
   }
  } __finally {
   LocalFree(sdService); //Free allocated memory
   //Close any open handles
   if (schService)
    CloseServiceHandle(schService);
   if (schManager)
    CloseServiceHandle(schManager);
   }
 } catch (Exception &exception) {
  Sysutils::ShowException(&exception, System::ExceptAddr());
  }
}
//---------------------------------------------------------------------------

void __fastcall TsrvMain::ServiceBeforeUninstall(TService *Sender)
{
 SERVICE_STATUS ssStatus;
 SERVICE_STATUS_PROCESS sspStatus;
 DWORD dwStartTime = GetTickCount();
 DWORD dwBytesNeeded;
 SC_HANDLE schManager; //Service Control Manager Handle
 SC_HANDLE schService; //Service Handle

 try
 {
  try
  {
   schManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
   schService = OpenService(schManager, srvMain->Name.c_str(), 
SERVICE_ALL_ACCESS);

   //Stop the service
   ControlService(schService, SERVICE_CONTROL_STOP, &ssStatus);

   //Wait for the service to stop
   while (ssStatus.dwCurrentState != SERVICE_STOPPED)
   {
    Sleep(ssStatus.dwWaitHint);

    if (ssStatus.dwCurrentState == SERVICE_STOPPED)
     break;

    QueryServiceStatusEx(schService, SC_STATUS_PROCESS_INFO, 
(LPBYTE)&sspStatus, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded);
    if (GetTickCount() - dwStartTime > (unsigned)srvMain->WaitHint)
     break;
   }
  } __finally {
   //Close any open handles
   if (schService)
    CloseServiceHandle(schService);
   if (schManager)
    CloseServiceHandle(schManager);
   }
 } catch (Exception &exception) {
  Sysutils::ShowException(&exception, System::ExceptAddr());
  }
}
//--------------------------------------------------------------------------- 
Technical Details
Message-ID: <46814809@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <46802aa8$1@newsgroups.borland.com> <4680321e$1@newsgroups.borland.com> <468033ac$1@newsgroups.borland.com> <4680497f$1@newsgroups.borland.com> <468122e3$1@newsgroups.borland.com> <46814544$2@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,e4ab627349eb1efd
X-Google-Attributes: gid10cc67,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Jeff" <Jeff@nospam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <46802aa8$1@newsgroups.borland.com> <4680321e$1@newsgroups.borland.com> <468033ac$1@newsgroups.borland.com> <4680497f$1@newsgroups.borland.com> <468122e3$1@newsgroups.borland.com> <46814544$2@newsgroups.borland.com>
Subject: Re: TService Compile Error
Date: Tue, 26 Jun 2007 11:07:20 -0600
Lines: 266
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3138
NNTP-Posting-Host: 206.104.31.90
Message-ID: <46814809@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1182877705 206.104.31.90 (26 Jun 2007 10:08:25 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail