Jak zjistit status Windows service v X++?


Pokud potřebujete zjistit stav jakékoli služby v systému Windows, můžete jako inspiraci použít následující kód:

{
InteropPermission interopPermission = new InteropPermission(InteropKind::DllInterop);
str machineName = "machineName";
str serviceName = "Browser";
DLL dll;
DLLFunction dllFunction;
int hSCM;
Binary binMachineName;
Binary binDBName = new Binary("ServicesActive"); // Opens default database, works also in AX 3.0
DLLFunction openServiceFunction;
DLLFunction queryServiceStatusFunction;
DLLFunction closeServiceHandleFunction;
Binary binServiceName;
int hService;
Binary serviceStatus = new Binary(28);
int status;
str serviceInfoMsg = "Service is %1.";
;

interopPermission.assert();

dll = new DLL("advapi32.dll");
if (dll)
{
dllFunction = new DLLFunction(dll, "OpenSCManagerA");
if (dllFunction)
{
dllFunction.arg(ExtTypes::Pointer, ExtTypes::Pointer, ExtTypes::DWord);
dllFunction.returns(ExtTypes::DWord);

closeServiceHandleFunction = new DLLFunction(dll, "CloseServiceHandle");
if (closeServiceHandleFunction)
{
closeServiceHandleFunction.arg(ExtTypes::DWord);
closeServiceHandleFunction.returns(ExtTypes::DWord);
}
else
{
error("Failed to get 'CloseServiceHandle' function.");
return;
}

binMachineName = new Binary(machineName);
hSCM = dllFunction.call(binMachineName, binDBName, 0x80000000);
if (hSCM)
{
openServiceFunction = new DLLFunction(dll, "OpenServiceA");
if (openServiceFunction)
{
openServiceFunction.arg(ExtTypes::DWord, ExtTypes::Pointer, ExtTypes::DWord);
openServiceFunction.returns(ExtTypes::DWord);
binServiceName = new Binary(serviceName);
hService = openServiceFunction.call(hSCM, binServiceName, 0x80000000);
if (hService)
{
queryServiceStatusFunction = new DLLFunction(dll, "QueryServiceStatus");
if (queryServiceStatusFunction)
{
queryServiceStatusFunction.arg(ExtTypes::DWord, ExtTypes::Pointer);
queryServiceStatusFunction.returns(ExtTypes::DWord);
queryServiceStatusFunction.call(hService, serviceStatus);

status = serviceStatus.dWord(4);

switch(status)
{
case 1:
info(strFmt(serviceInfoMsg, "not running"));
break;

case 2:
info(strFmt(serviceInfoMsg, "starting"));
break;

case 3:
info(strFmt(serviceInfoMsg, "stopping"));
break;

case 4:
info(strFmt(serviceInfoMsg, "running"));
break;

case 5:
info(strFmt(serviceInfoMsg, "continue is pending"));
break;

case 6:
info(strFmt(serviceInfoMsg, "pause is pending"));
break;

case 7:
info(strFmt(serviceInfoMsg, "paused"));
break;
}
}
else
{
error("Failed to get 'QueryServiceStatus' function.");
closeServiceHandleFunction.call(hService);
closeServiceHandleFunction.call(hSCM);
return;
}

closeServiceHandleFunction.call(hService);
}
else
{
error(strFmt("Failed to get service '%1'.", serviceName));
closeServiceHandleFunction.call(hSCM);
return;
}
}
else
{
error("Failed to get 'OpenServiceA' function.");
closeServiceHandleFunction.call(hSCM);
return;
}
closeServiceHandleFunction.call(hSCM);
}
else
{
error("Failed to open service manager.");
return;
}
}
else
{
error("Failed to get 'OpenSCManagerA' function.");
return;
}
}
else
{
error("Failed to load 'advapi32.dll'.");
return;
}
}



Martin F

Skip to main content