It is currently Fri Oct 20, 2017 7:00 pm



Reply to topic  [ 2 posts ] 
C++ CLR snippets 
Author Message
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 15711
Location: On a slope
Reply with quote
Post 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

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Thu Jun 03, 2010 11:33 am
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 15711
Location: On a slope
Reply with quote
Post 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");
      }

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Thu Jun 03, 2010 1:25 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.