2011-07-18 19 views
0

こんにちは私は.NETアプリケーションを介してWindowsサービスを開始/停止/無効にしようとしていますが、動作するために以下のコードを取得するようですが、エラー4エラーC2065: 'ServiceController':宣言されていない識別子Managed C++のServiceController? (.NET)

これに関する正しい参考情報は何ですか?私は、システムに正しいものを見つけるように見えるカント::

String ^servicename = "srservice"; 



      // Stop the service if it's started. 

      ServiceController^ controller = new ServiceController(servicename); 
      if (controller.Status == ServiceControllerStatus.Running) 

       controller.Stop(); 



      // Set the startup type of the service. 

      String^ serviceregistrylocation = String::Format("SYSTEM\CurrentControlSet\Services\{0}", servicename); 

      RegistryKey ^localMachine = Registry::LocalMachine; 

      RegistryKey servicekey = localMachine.OpenSubKey(serviceregistrylocation, true); 



      // Set value to 2 for automatic, 3 for manual, or 4 for disabled. 

      servicekey.SetValue("Start", 3);  

[OK]をので、私は、コードを修正し、それが今

    String ^servicename = "Fax"; 



      // Stop the service if it's started. 

      ServiceController^ controller = gcnew ServiceController(servicename); 
      if (controller->Status == ServiceControllerStatus::Running) 

       controller->Stop(); 



      // Set the startup type of the service. 

      String^ serviceregistrylocation = String::Format("SYSTEM\CurrentControlSet\Services\{0}", servicename); 

      RegistryKey ^localMachine = Registry::LocalMachine; 

      RegistryKey ^servicekey = localMachine->OpenSubKey(serviceregistrylocation, true); 



      // Set value to 2 for automatic, 3 for manual, or 4 for disabled. 
      try{ 
      servicekey->SetValue("Start", 4); 
      } 
       catch (Exception^ e) 
     { 
      MessageBox::Show(e->Message); 
     } 

     } 
をコンパイルしますが、「オブジェクト参照オブジェクトのインスタンスに設定されていません」というエラーがスローされます

答えて

1

System::ServiceProcessにはusingステートメントがありますか?そうでなければ、クラスを完全に修飾する必要がありますSystem::ServiceProcess::ServiceController。また、System.ServiceProcess.dllアセンブリへの参照を含める必要があります。プロジェクトの右クリック・コンテキスト・メニューから「プロパティー」を選択して、参照を追加することができます。次に、左側のツリービューの上部から「Common Properties」を選択します。 "Add New Reference ..."ボタンをクリックし、適切なリファレンスを選択します。それは、それを検索しようとしたときか、プロジェクトを右クリックして選択することができ、「参照...」を

enter image description here

+0

厥だけで問題を、私はシステム:: ServiceProcess :: ServiceControllerのを持っていない、それは、そうですSystem :: serviceProcessは表示されません。 – cox

+0

NVM、参照を追加していない、うまくいくようです、ありがとう! – cox

+0

Hmm今、次のようなエラーが表示されます。エラーエラーC3673: 'Microsoft :: Win32 :: RegistryKey':クラスにコピーコンストラクタがありません – cox

関連する問題