PDA

View Full Version : Accessing a string from a pointer - EnumDependentServices API call



jcarver
08-23-2005, 08:16 PM
I'm struggling with getting a list of active services depending on a service. I'm using the API call EnumDependentServices, and I've managed to get the array of service structs back from the function. But I'm having trouble accessing the strings (service name and display name).

Here's my code so far. The calls to EnumDependentServices succeed, and I can iterate through the dependents and see the names as displayed by the SprintBox. But the pointers can't be used in functions that require a string so the ListAddString function fails. How do I access the strings pointed to in that struct?



typedef SERVICE_STATUS
begin
NUMBER dwServiceType;
NUMBER dwCurrentState;
NUMBER dwControlsAccepted;
NUMBER dwWin32ExitCode;
NUMBER dwServiceSpecificExitCode;
NUMBER dwCheckPoint;
NUMBER dwWaitHint;
end;

typedef ENUM_SERVICE_STATUS
begin
POINTER psServiceName;
POINTER psServDisplayName;
SERVICE_STATUS ssStatus;
end;

prototype BOOL Advapi32.EnumDependentServicesA(NUMBER, NUMBER, POINTER, NUMBER, POINTER, POINTER);

prototype GetDependentServices(BYREF STRING, LIST);

function GetDependentServices(szService, lzDependents)
NUMBER nReturn, nErrNo;
ENUM_SERVICE_STATUS structEnum; // For getting size of struct
ENUM_SERVICE_STATUS POINTER pssEnumArray;
ENUM_SERVICE_STATUS POINTER pssEnum;
STRING sServiceName, sServDisplayName;
POINTER psServiceName;
NUMBER nBufSize, nBytesNeeded;
NUMBER nNumServices;
NUMBER nI;

begin
nReturn = NTServiceAPI_InitServiceConnections(szService);
if (nReturn = 0) then
nReturn = EnumDependentServicesA(NTServiceAPI_hService, SERVICE_ACTIVE, pssEnumArray,
nBufSize, &nBytesNeeded, &nNumServices);
nErrNo = GetLastError();
if ((nReturn != 0) && (nBytesNeeded = 0)) then
// No dependent services
elseif ((nErrNo != ERROR_MORE_DATA) || (nBytesNeeded > 64000)) then
// Unrecoverable error or risk of buffer overflow
nReturn = -1;
else
// Allocate a buffer for the dependencies
pssEnumArray = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nBytesNeeded);
nBufSize = nBytesNeeded;
nReturn = EnumDependentServicesA(NTServiceAPI_hService, SERVICE_ACTIVE, pssEnumArray,
nBufSize, &nBytesNeeded, &nNumServices);
nErrNo = GetLastError();

if (nReturn != 0) then
for nI = 0 to (nNumServices - 1)
pssEnum = pssEnumArray + (SizeOf(structEnum) * nI);
SprintfBox(INFORMATION, "","%s\n%s", pssEnum->psServiceName, pssEnum->psServDisplayName);

ListAddString(lzDependents, pssEnum->psServiceName, AFTER);
endfor;
endif;
HeapFree(GetProcessHeap(), 0, pssEnumArray);
endif;
endif;
end;

jcarver
08-25-2005, 05:39 PM
Well, the answer was staring me in the face. If SprintfBox can display the string then Sprintf can write it to a string variable. Don't know if this is the best way to handle the problem but it works.

John

oBladeo1
01-05-2007, 05:27 AM
Thanks allot this was doing my head in as well