// Description   : DotNET Service for the NANO OS Core $Revision: 5 $

// Copyright     : Proxima Centauri Romania SRL. ALL RIGHTS RESERVED.

// Author        : http://www.AlexanderMihail.ProximaCentauri.ro

 

#include <TCHAR.h>

#include <string.h>

 

using namespace System;

using namespace System::ComponentModel;

using namespace System::Configuration::Install;

using namespace System::ServiceProcess;

using namespace System::Text;

using namespace System::Security::Policy;

using namespace System::Reflection;

using namespace System::Threading;

using namespace System::Collections;

using namespace System::Runtime::InteropServices;

using namespace System::Security::Permissions;

 

[DllImport("RING.1.dll", CharSet=CharSet::Auto)] extern "C" void InitSharedMemory();

[DllImport("RING.1.dll", CharSet=CharSet::Auto)] extern "C" void DoneSharedMemory();

[DllImport("RING.1.dll", CharSet=CharSet::Auto)] extern "C" void InitRing1(const char*name);

[DllImport("RING.1.dll", CharSet=CharSet::Auto)] extern "C" void*StartSyncThread(const char*name);

[DllImport("RING.1.dll", CharSet=CharSet::Auto)] extern "C" void StopSyncThread(void*th);

 

 

namespace Service

{

      public ref class ServiceWinService : public System::ServiceProcess::ServiceBase

      {

      public:

            ServiceWinService()

            {

                  InitializeComponent();

            }

      protected:

            ~ServiceWinService()

            {

                  if (components)

                  delete components;

            }

            void*thread;

            virtual void OnStart(array<String^>^ args) override

            {

                  ::InitSharedMemory();

                  ::InitRing1("SERVICE.SYNC");

                  thread=::StartSyncThread("TH.SERVICE.SYNC");

            }

            virtual void OnStop() override

            {

                  ::StopSyncThread(thread);

                  ::DoneSharedMemory();

            }

      private:

            System::ComponentModel::Container ^components;

    #pragma region Windows Form Designer generated code

            void InitializeComponent(void)

            {

                  this->components = gcnew System::ComponentModel::Container();

                  this->CanStop = true;

                  this->CanPauseAndContinue = true;

                  this->AutoLog = true;

                  this->ServiceName = L"DOM Synchronisation Service";

            }

    #pragma endregion

      };

}

 

namespace WindowsService

{

    [RunInstaller(true)]

    public ref class WindowsServiceInstaller : Installer

    {

      public:

            WindowsServiceInstaller()

        {

            ServiceProcessInstaller^ serviceProcessInstaller = gcnew ServiceProcessInstaller();

            ServiceInstaller^ serviceInstaller = gcnew ServiceInstaller();

            //# Service Account Information

                  serviceProcessInstaller->Account = ServiceAccount::LocalSystem;

            serviceProcessInstaller->Username = nullptr;

            serviceProcessInstaller->Password = nullptr;

            //# Service Information

            serviceInstaller->DisplayName = "NANO Active Directory Sync Service";

            serviceInstaller->StartType = ServiceStartMode::Automatic;

            //# This must be identical to the WindowsService.ServiceBase name

            //# set in the constructor of WindowsService.cs

            serviceInstaller->ServiceName = "NANO Active Directory Sync Service";

            Installers->Add(serviceProcessInstaller);

            Installers->Add(serviceInstaller);

        }

    };

}

 

//To install/uninstall the service, type: "Service.exe -Install [-u]"

int _tmain(int argc, _TCHAR* argv[])

{

      if (argc >= 2)

      {

            if (argv[1][0] == _T('/'))

                  argv[1][0] = _T('-');

            if (_tcsicmp(argv[1], _T("-Install")) == 0)

            {

                  array<String^>^ myargs = System::Environment::GetCommandLineArgs();

                  array<String^>^ args = gcnew array<String^>(myargs->Length - 1);

                  // Set args[0] with the full path to the assembly,

                  Assembly^ assem = Assembly::GetExecutingAssembly();

                  args[0] = assem->Location;

                  Array::Copy(myargs, 2, args, 1, args->Length - 1);

                  AppDomain^ dom = AppDomain::CreateDomain(L"execDom");

                  Type^ type = System::Object::typeid;

                  String^ path = type->Assembly->Location;

                  StringBuilder^ sb = gcnew StringBuilder(path->Substring(0, path->LastIndexOf(L"\\")));

                  sb->Append(L"\\InstallUtil.exe");

                  Evidence^ evidence = gcnew Evidence();

                  dom->ExecuteAssembly(sb->ToString(), evidence, args);

            }

      }

      else

            ServiceBase::Run(gcnew Service::ServiceWinService());

}

 

[assembly:AssemblyTitleAttribute("NANO SyncService")];

[assembly:AssemblyDescriptionAttribute("Service to synchronise the NANO Active Directory with the local file systems")];

[assembly:AssemblyConfigurationAttribute("")];

[assembly:AssemblyCompanyAttribute("")];

[assembly:AssemblyProductAttribute("NANO SyncService")];

[assembly:AssemblyCopyrightAttribute("Proxima Centauri Romania SRL (c)  2009, 2010")];

[assembly:AssemblyTrademarkAttribute("")];

[assembly:AssemblyCultureAttribute("")];

[assembly:AssemblyVersionAttribute("1.0.*")];

[assembly:ComVisible(false)];

[assembly:CLSCompliantAttribute(true)];

[assembly:SecurityPermission(SecurityAction::RequestMinimum, UnmanagedCode = true)];