Also ich hab teilweise das Template vom Visual Studio 2005 verwendet
Code der eigentlichen Exe:
1 | //To install/uninstall the service, type: "XXX_Service.exe -Install [-u]"
|
2 | int _tmain(int argc, _TCHAR* argv[])
|
3 | {
|
4 | if (argc >= 2)
|
5 | {
|
6 | if (argv[1][0] == _T('/'))
|
7 | {
|
8 | argv[1][0] = _T('-');
|
9 | }
|
10 |
|
11 | if (_tcsicmp(argv[1], _T("-Install")) == 0)
|
12 | {
|
13 | MyProjectInstaller^ service_installer = gcnew MyProjectInstaller();
|
14 |
|
15 | array<String^>^ myargs = System::Environment::GetCommandLineArgs();
|
16 | array<String^>^ args = gcnew array<String^>(myargs->Length - 1);
|
17 |
|
18 | // Set args[0] with the full path to the assembly,
|
19 | Assembly^ assem = Assembly::GetExecutingAssembly();
|
20 | args[0] = assem->Location;
|
21 |
|
22 | Array::Copy(myargs, 2, args, 1, args->Length - 1);
|
23 | AppDomain^ dom = AppDomain::CreateDomain(L"execDom");
|
24 | Type^ type = System::Object::typeid;
|
25 | String^ path = type->Assembly->Location;
|
26 | StringBuilder^ sb = gcnew StringBuilder(path->Substring(0, path->LastIndexOf(L"\\")));
|
27 | sb->Append(L"\\InstallUtil.exe");
|
28 | Evidence^ evidence = gcnew Evidence();
|
29 | dom->ExecuteAssembly(sb->ToString(), evidence, args);
|
30 | }
|
31 | else
|
32 | {
|
33 | ServiceBase::Run(gcnew XXX_ServiceWinService());
|
34 | }
|
35 | }
|
36 | else
|
37 | {
|
38 | ServiceBase::Run(gcnew XXX_ServiceWinService());
|
39 | }
|
40 | }
|
Code der Serviceklasse:
1 | public ref class XXX_ServiceWinService : public System::ServiceProcess::ServiceBase
|
2 | {
|
3 | public:
|
4 | XXX_ServiceWinService()
|
5 | {
|
6 | //array<String^>^test;
|
7 | InitializeComponent();
|
8 | //
|
9 | //TODO: Add the constructor code here
|
10 | //
|
11 | //this->OnStart(test);
|
12 | }
|
13 | protected:
|
14 | /// <summary>
|
15 | /// Clean up any resources being used.
|
16 | /// </summary>
|
17 | ~XXX_ServiceWinService()
|
18 | {
|
19 | if (components)
|
20 | {
|
21 | delete components;
|
22 | }
|
23 | }
|
24 |
|
25 | /// <summary>
|
26 | /// Set things in motion so your service can do its work.
|
27 | /// </summary>
|
28 | virtual void OnStart(array<String^>^ argv) override
|
29 | {
|
30 | //BLA BLA BLA
|
31 | }
|
32 |
|
33 | /// <summary>
|
34 | /// Stop this service.
|
35 | /// </summary>
|
36 | virtual void OnStop() override
|
37 | {
|
38 | // TODO: Add code here to perform any tear-down necessary to stop your service.
|
39 | }
|
40 |
|
41 | private:
|
42 | /// <summary>
|
43 | /// Required designer variable.
|
44 | /// </summary>
|
45 | System::ComponentModel::Container ^components;
|
46 |
|
47 | #pragma region Windows Form Designer generated code
|
48 | /// <summary>
|
49 | /// Required method for Designer support - do not modify
|
50 | /// the contents of this method with the code editor.
|
51 | /// </summary>
|
52 | void InitializeComponent(void)
|
53 | {
|
54 | this->components = gcnew System::ComponentModel::Container();
|
55 | this->CanStop = true;
|
56 | this->CanPauseAndContinue = true;
|
57 | this->AutoLog = true;
|
58 | this->ServiceName = L"XXX_Service";
|
59 | }
|
Und das Problem ist jetzt wie ich Parameter an die OnStart Methode
übergeben kann, welche ja aufgerufen wird, wenn der Service gestartet
wird.