Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to enumerate services
#1
I want to use the AR Services Manager Library 1.03 enumeration service, but it is not successful

Macro Macro1
Code:
Copy      Help
Services.clsServices ses._create
Services.clsService se
foreach se ses
,out se.DisplayName


Attached Files Image(s)
       
#2
Strange, fails on Windows 10 only, and I cannot find the reason.
This COM component is very old. Now better use .NET class ServiceController.

Macro Scripting C#, services
Code:
Copy      Help
CsScript x
x.AddCode("")

x.Call("Class1.Test")


#ret
using System;
using System.ServiceProcess;

public class Class1
{
public static void Test()
{
foreach (var se in ServiceController.GetServices())
{
Console.WriteLine(se.DisplayName);
//se.ServiceName
//se.Status
}
}
}
#3
Strange, the following code can run successfully

At present, I am not familiar with C#. I want to implement the functions of viewing service status, running, stopping services, etc. I am worried that the components that are too old are not working well. Can you use C# to provide sample code with the same function? thank you very much


Macro Macro1
Code:
Copy      Help
Services.clsService se._create
BSTR dispName="Quick Macros"

se.DisplayName=dispName
if(!se.ServiceType) mes- "Service not found." "" "x"

str state
sel se.CurrentState    
,case Services.Stopped state="stopped"
,case Services.Start_Pending_20 state="start pending"
,case Services.Stop_Pending_20 state="stop pending"
,case Services.Running state="running"
,case Services.continue_pending_20 state="continue pending"
,case Services.Pause_Pending_20 state="pause pending"
,case Services.Paused state="paused"    
,case else state="unknown"
int i=ListDialog("Start[]Stop[]Pause[]Continue" F"Current state: {state}")
if(!i) ret
if(!IsUserAdmin) mes- "To change service state, QM must be running as admin." "" "x"
sel i    
,case 1 se.StartService    
,case 2 se.StopService    
,case 3 se.PauseService
,case 4 se.ContinueService
#4
This code works on my PC too.

Using C# in QM is not very easy. Another way - use Windows API directly. I don't know which way is easier in QM. I could create a service manager class in QM, but now don't have time for it.

If you want to use C#, look for documentation of .NET class ServiceController and related classes in MSDN Library. Also you can find code examples on the internet.
If you want to use Windows API, start from function OpenSCManager. This and related functions are documented in MSDN Library, and you can find C/C++ examples on the internet.
#5
Also Shell32.Shell class has some service functions.

Macro Macro100
Code:
Copy      Help
Shell32.Shell k._create
str s="quickmacros2"
if !k.IsServiceRunning(s)
,k.ServiceStart(s VariantBool(1))
else
,out "running"
#6
This should be the simplest code to start or stop the service, thank you very much Smile


Forum Jump:


Users browsing this thread: 1 Guest(s)