0
嗨im试图通过.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);
行,所以我修改了代码,它现在编译,但抛出一个“对象引用不设置到对象的实例”错误
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);
}
}
那只是这个问题,我没有系统:: ServiceProcess :: ServiceController的看来,当它试图在寻找它System :: serviceProcess不显示。 – cox
NVM,没有添加参考,似乎工作,谢谢! – cox
嗯现在即时通讯错误说:错误错误C3673:'Microsoft :: Win32 :: RegistryKey':类没有复制构造函数 – cox