2011-07-27 31 views
1

在VB.net我可以執行在Windows的服務自定義命令:ExecuteCommand到.NET服務

桌面:

Const SmartShutdown As Integer = 12 

Dim myService As New ServiceController("SampleService") 
myService.ExecuteCommand(SmartShutdown) 

Windows的服務:

Protected Overrides Sub OnCustomCommand(ByVal command As Integer) 

是它也可以執行從VB6到.net Windows服務的命令?

回答

1

您可以使用ControlService API函數發送任何控制代碼。 這確實要求您調用OpenService並具有適當的訪問權限,並且您的進程正在以所需的級別運行。 在Vista +上,可以使用常規清單(進程範圍)或COM elevation完成此操作。

1

考慮使用VB6 Shell功能和sc.exe命令行工具來控制Windows服務,如:

Shell("cmd /C 'sc stop SampleService > %APPDATA%\svc.log'") 

更新

要發送自定義命令,如:

Shell("cmd /C 'sc control SampleService 128 > %APPDATA%\svc.log'") 

自定義命令應該在128-255範圍內。

+0

還有一種方法可以執行自定義命令嗎?我的服務使用共享DLL來管理工具。當我安裝新版本的主程序時,如果有任何Tools正在運行,我的安裝程序(用vb6編寫)應該檢查。 – joau

+0

@joau是的,你可以看到我的更新。 –