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 ar开发者_如何学Gogs[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());
}
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.
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.
精彩评论