首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >托管c++中的ServiceController?(.NET)

托管c++中的ServiceController?(.NET)
EN

Stack Overflow用户
提问于 2011-07-19 03:56:40
回答 1查看 786关注 0票数 0

您好,我试图通过.NET应用程序启动/停止/禁用windows服务,但我似乎无法获得以下代码工作,它一直在说错误4错误C2065:'ServiceController‘:未声明的标识符

这方面的正确参考是什么?我似乎在系统中找不到正确的:

代码语言:javascript
复制
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);       

好的,所以我修改了代码,它现在编译了,但是抛出了一个"object reference not set to an object instance of an object“错误

代码语言:javascript
复制
                 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 );
        }

         }
EN

回答 1

Stack Overflow用户

发布于 2011-07-19 04:05:09

你有System::ServiceProcess的using语句吗?如果不是,则必须使用System::ServiceProcess::ServiceController完全限定该类。此外,还必须包含对System.ServiceProcess.dll程序集的引用。您可以通过从项目的右键单击上下文菜单中选择Properties来添加引用。然后从左侧树视图的顶部选择"Common Properties“。单击“添加新引用...”按钮,然后选择适当的引用。或者,您可以右键单击项目并选择“引用...”

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6738649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档