实际上我这一部分都没问题,也就是你上面说的。 我能安装上Windows Service ,但我现在的问题是需要通过编程来实现Windows Service的启动和停止。 也就是我另外要做个程序来控制我的Windows Service的启动和停止。感谢您使用微软产品。您可以通过API, ADSI或WMI三种方式在VB6中启动和停止windows2000的系统服务。1.API方式。 您需要调用下面4个API函数: OpenSCManager用以获得service信息数据库 OpenService用以获得service的句炳 StartService/StopService用以启动/停止service以下链接提供了用VB查询service的状态和设置的样例程序,在Q189633等文章里面的代码可以作为您在VB里调用NT service操作的一个参考。 http://support.microsoft.com/support/kb/articles/q189/6/33.asp您可以参考以下链接提供的VC样例程序,停止一个service。 http://support.microsoft.com/support/kb/articles/q245/2/30.asp2.ADSI方式 您可以使用IADsServiceOperations接口的start/stop方法启动/停止一个NT的service. 如下例在Windows 2000 Professional中启动一个Microsoft® Fax ServiceDim cp As IADsComputer Dim so As IADsServiceOperations Set cp = GetObject("WinNT://myMachine,computer") Set so = cp.GetObject("Service", "Fax") If (so.Status <> ADS_SERVICE_RUNNING) Then ' The operation is not running. so.Start End If详细信息请参考: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netdir/adsi/iadsserviceoperations_start.asp3.WMI方式。 您也可以用WMI编程对系统service进行管理,如下例:Set ServiceSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_Service where State='Stopped' ) ‘上面的代码返回停止状态的service对象集。for each Service in ServiceSet ‘ 您可以在这里用Win32_Service对象的StartService/StopService方法启动/停止service。 Next关于WMI的详细信息请参考以下链接: http://msdn.microsoft.com/library/en-us/dnwmi/html/mngwmi.asp- 微软全球技术中心 VB技术支持本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款 (http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。 为了为您创建更好的讨论环境,请参加我们的用户满意度调查 (http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。 ====================== 这些就是我查阅到的资料,但没找到.net实现的代码。 忘告之。
安装上以后如果没有出现在系统服务列表中,参考我上面的文章,加入安装类即可。
实际上我这一部分都没问题,也就是你上面说的。
我能安装上Windows Service ,但我现在的问题是需要通过编程来实现Windows Service的启动和停止。
也就是我另外要做个程序来控制我的Windows Service的启动和停止。感谢您使用微软产品。您可以通过API, ADSI或WMI三种方式在VB6中启动和停止windows2000的系统服务。1.API方式。
您需要调用下面4个API函数:
OpenSCManager用以获得service信息数据库
OpenService用以获得service的句炳
StartService/StopService用以启动/停止service以下链接提供了用VB查询service的状态和设置的样例程序,在Q189633等文章里面的代码可以作为您在VB里调用NT service操作的一个参考。
http://support.microsoft.com/support/kb/articles/q189/6/33.asp您可以参考以下链接提供的VC样例程序,停止一个service。
http://support.microsoft.com/support/kb/articles/q245/2/30.asp2.ADSI方式
您可以使用IADsServiceOperations接口的start/stop方法启动/停止一个NT的service.
如下例在Windows 2000 Professional中启动一个Microsoft® Fax ServiceDim cp As IADsComputer
Dim so As IADsServiceOperations
Set cp = GetObject("WinNT://myMachine,computer")
Set so = cp.GetObject("Service", "Fax")
If (so.Status <> ADS_SERVICE_RUNNING) Then ' The operation is not running.
so.Start
End If详细信息请参考:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netdir/adsi/iadsserviceoperations_start.asp3.WMI方式。
您也可以用WMI编程对系统service进行管理,如下例:Set ServiceSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select *
from Win32_Service where State='Stopped' )
‘上面的代码返回停止状态的service对象集。for each Service in ServiceSet
‘ 您可以在这里用Win32_Service对象的StartService/StopService方法启动/停止service。
Next关于WMI的详细信息请参考以下链接:
http://msdn.microsoft.com/library/en-us/dnwmi/html/mngwmi.asp- 微软全球技术中心 VB技术支持本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款
(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查
(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
======================
这些就是我查阅到的资料,但没找到.net实现的代码。
忘告之。
第一个需要使用P/Invoke
后面两个.NET有内置类库支持ADSI,WMI的操作: System.DirectoryServices和System.Management实际上.NET还有一个最方便的方法来控制Windows服务:
System.ServiceProcess.ServiceBase.Run(new yourServiceClass());
//ToggletheTelnetservice-
//Ifitisstarted(running,paused,etc),stoptheservice.
//Ifitisstopped,starttheservice.
ServiceControllersc=newServiceController("Telnet");
Console.WriteLine("TheTelnetservicestatusiscurrentlysetto{0}",
sc.Status.ToString());if((sc.Status.Equals(ServiceControllerStatus.Stopped))||
(sc.Status.Equals(ServiceControllerStatus.StopPending)))
{
//Starttheserviceifthecurrentstatusisstopped.Console.WriteLine("StartingtheTelnetservice...");
sc.Start();
}
else
{
//Stoptheserviceifitsstatusisnotsetto"Stopped".Console.WriteLine("StoppingtheTelnetservice...");
sc.Stop();
}//Refreshanddisplaythecurrentservicestatus.
sc.Refresh();
Console.WriteLine("TheTelnetservicestatusisnowsetto{0}.",
sc.Status.ToString());
ServiceController sc = new ServiceController("你的windows服务名");通过State来检查当前服务的状态
停止服务:
if (sc.Status != ServiceControllerStatus.Stopped && sc.Status != ServiceControllerStatus.StopPending)
sc.Stop();
至于别的,如启动使用sc.Start();
有办法吗?
不通过MS提供的安装工具。
string path = Application.StartupPath;
System.Diagnostics.ProcessStartInfo ps = new System.Diagnostics.ProcessStartInfo(@"C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\installutil.exe",path + @"\RemotingWinService.exe");
ps.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = ps;
try
{
p.Start();
p.WaitForExit();
p.Close();
p = null;
ServiceController sc = new ServiceController("RemotingService");
// sc.
// this.label1.Text = "正在启动服务……请稍候!";
if(sc.Status!=ServiceControllerStatus.Running&&sc.Status!=ServiceControllerStatus.StartPending)
{
sc.Start();
// sc.WaitForStatus(ServiceControllerStatus.Running);
}
//this.CreateCustomLog();
//Application.Exit();}
catch
{}卸载类似