ClanKiller.com
http://forums.clankiller.com/

C++ CLR snippets
http://forums.clankiller.com/viewtopic.php?f=24&t=3567
Page 1 of 1

Author:  Satis [ Thu Jun 03, 2010 11:33 am ]
Post subject:  C++ CLR snippets

I'm just using this thread to store C++ common language runtime snippets as I find them.

Windows service - add installer
- right-click design view, Add Installer, serviceProcessInstaller1 and choose service user in properties
- to use, type [servicename] -Install
Windows service - add uninstall
- edit *WinService.cpp
- in _tmain after if (_tcsicmp(argv[1], _T("-Install")) == 0) codeblock add
Code:
      if (_tcsicmp(argv[1], _T("-Uninstall")) == 0)
      {
         //Uninstall this Windows Service using InstallUtil.exe
         array<String^>^ myargs = System::Environment::GetCommandLineArgs();
         array<String^>^ args = gcnew array<String^>(myargs->Length);

         args[0] = L"-u";
         args[1] = (myargs[0]);

         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);
      }

- to use, type [servicename] -Uninstall

Author:  Satis [ Thu Jun 03, 2010 1:25 pm ]
Post subject:  Re: C++ CLR snippets

Start a thread
Code:
using namespace System::Threading;
      
Thread^      sThread;
      virtual void OnStart(array<String^>^ args) override
      {
         //start loop thread
         ThreadStart^ tDelegate = gcnew ThreadStart(this, &SymonSystemStatusWinService::mainLoop);
         sThread = gcnew Thread( tDelegate );
         sThread->Start();
      }
      void mainLoop(){
         sleepTime = 10000;   //10 second
         stopping = false;
         while(!stopping){
            sThread->Sleep(sleepTime);
         }
      }


Write event log
Code:
using namespace System::Diagnostics;
      void writeEvent(String^ _log, String^ _event, String^ _source){
         if(!EventLog::SourceExists(_source))
            EventLog::CreateEventSource(_source, _log);
         EventLog^ el = gcnew EventLog;
         el->Source = _source;
         el->WriteEntry(_event);
      }


Write debug event
Code:
using namespace System::Diagnostics;

      virtual void OnStart(array<String^>^ args) override
      {
         //log service start
         Debug::WriteLine(L"Starting service");
      }

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/