2

I am trying to build a new windows Service that i can run as a console App during debug. My thought is that i would need instantiate the service class and spit all output to the console.

So instead of using the below call it would be the new instance.

ServiceBase::Run(gcnew myWinService());

Currently it is a skeleton and just want to get some insight on this. Thanks!

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 myWinService());
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
HexBlit
  • 1,172
  • 1
  • 13
  • 31
  • @0A0D: not a duplicate. The referenced question is about launching another program from a windows service. This question is about running the *same* program as a service or as a regular process depending on release/debug mode. – André Caron Jun 21 '11 at 16:44
  • You could use an `#ifdef` so the relevant code gets compiled/does not get compiled. – Merlyn Morgan-Graham Jun 21 '11 at 16:54
  • @Merlyn: yes, except that it's rather delicate if all of OP's code is in `myWinService`, as you would have to invoke the different event handlers manually. – André Caron Jun 21 '11 at 17:00
  • @Andre: Yes, he'd probably have to make a big refactor. Throwing implementation directly in a framework's callbacks tends to make your application less easily portable... – Merlyn Morgan-Graham Jun 21 '11 at 17:04
  • @Merlyn: in this case, it means you're re-implementing part of the framework to re-use you code, though. – André Caron Jun 21 '11 at 17:12

2 Answers2

2

If you really wanted to, you could simply replace the call to ServiceBase::Run() with custom code that invokes the service methods. As the On*() service notification handlers are protected, this would basically require a workaround, such as a public method of myWinService that automates the proper calls to the notification handlers.

However, this is a really bad idea and feels somewhat hack-ish. You should really write a second program that executes whatever is usually done in the service's background thread (which you probably launch in the OnStart() method) in synchronous mode.

Basically, make two programs that put the relevant boiler-plate code for the process type and have them invoke the same code, which really contains your application.

André Caron
  • 44,541
  • 12
  • 67
  • 125
0

There is some ability to interact with the console from a service. In the Windows service properties dialog look for a check box named 'allow interaction with console".

You might also consider putting the majority of your application in an assembly/dll and calling the functions from either a console application or the windows service.

Jay
  • 13,803
  • 4
  • 42
  • 69