0

I'm writing an app that interacts with the Windows API from a Windows Service.

After loads of help from @chowey here, I sort of got the hang of things and started a basic library which I've put on GitHub here.

I've now moved on to "Services", with the requirement to list all Windows Services on a machine, start, stop, restart them. The start/stop/restart look pretty straight forward once you've got a service handle to work with, but I'm struggling with getting a list of installed services.

EnumServicesStatusEx in Advapi32.dll is the function I need to call, but it requires a pointer to pre-allocated memory for an array of ENUM_SERVICE_STATUS_PROCESS structs.

You can call the function with a null pointer and it will return the memory allocation size required, but I don't believe there is a way to directly allocate memory in Go.

At first I thought I could get the memory allocation requirement, divide it by the SizeOf the struct using the unsafe package, create a slice containing that number of elements, then pass a pointer to the first element to the function, but it says the memory needs to include space for the string data, which this wouldn't.

Does anyone know how this could be accomplished, pretty please? :).

Community
  • 1
  • 1
iamacarpet
  • 417
  • 5
  • 13
  • From EnumServicesStatusEx doco: `lpServices [out, optional] A pointer to the buffer that receives the status information. ... To determine the required size, specify NULL for this parameter and 0 for the cbBufSize parameter. The function will fail and GetLastError will return ERROR_MORE_DATA. The pcbBytesNeeded parameter will receive the required size.`. Once you know how large your buffer need to be, call EnumServicesStatusEx again with buffer of that size. – alex May 16 '16 at 11:35
  • Hello @alex, as described above, I don't know of a way to make Go allocate memory directly. You have to give it a data structure to make it allocate memory, then expose that memory via the unsafe package. – iamacarpet May 16 '16 at 13:18
  • 1
    you said it yourself: `create a slice containing that number of elements, then pass a pointer to the first element to the function`. From EnumServicesStatusEx doco: `cbBufSize [in] The size of the buffer pointed to by the lpServices parameter, in bytes.` So you can create array of cbBufSize bytes, and pass address of first element of that array into EnumServicesStatusEx. See https://github.com/golang/go/blob/master/src/internal/syscall/windows/registry/value.go#L71 for example. Unlike the example, you should start with passing lpServices=nil as per EnumServicesStatusEx doco. – alex May 16 '16 at 23:45
  • Yes I thought that, but if I created a slice of structs, those structs would contain pointers. The memory allocated would be enough to store the pointer address, but wouldn't actually allocate any memory for the API call to store the string data the pointer should reference as required by the MSDN docs. Although if the size required is given in bytes, I could create a byte slice that size then try to cast it to the correct type using unsafe. Will have to assume the memory layout will be compatible. I'll give that a try, thanks! – iamacarpet May 17 '16 at 08:36
  • You should allocate byte slice of requested size. It is up to the API that you call to layout everything it needs inside your byte buffer. Your job will be to cast address to your first byte into correct type using unsafe. Just like you said. – alex May 17 '16 at 23:28

1 Answers1

1

After the suggestions from @alex, I've got the following example code working.

Looks like we create a byte slice of the right size then use the unsafe class to cast to our struct type.

    _, _, _ = svcEnumServicesStatusEx.Call(
        uintptr(handle),
        uintptr(uint32(SVC_SC_ENUM_PROCESS_INFO)),
        uintptr(uint32(SVC_SERVICE_WIN32)),
        uintptr(uint32(SVC_SERVICE_STATE_ALL)),
        uintptr(0),
        0,
        uintptr(unsafe.Pointer(&bytesReq)),
        uintptr(unsafe.Pointer(&numReturned)),
        uintptr(unsafe.Pointer(&resumeHandle)),
        uintptr(0),
    )

    if bytesReq > 0 {
        var buf []byte = make([]byte, bytesReq)

        ret, _, _ := svcEnumServicesStatusEx.Call(
            uintptr(handle),
            uintptr(uint32(SVC_SC_ENUM_PROCESS_INFO)),
            uintptr(uint32(SVC_SERVICE_WIN32)),
            uintptr(uint32(SVC_SERVICE_STATE_ALL)),
            uintptr(unsafe.Pointer(&buf[0])),
            uintptr(bytesReq),
            uintptr(unsafe.Pointer(&bytesReq)),
            uintptr(unsafe.Pointer(&numReturned)),
            uintptr(unsafe.Pointer(&resumeHandle)),
            uintptr(0),
        )

        if ret > 0 {
            var sizeTest ENUM_SERVICE_STATUS_PROCESS
            iter := uintptr(unsafe.Pointer(&buf[0]))

            for i := uint32(0); i < numReturned; i++ {
                var data *ENUM_SERVICE_STATUS_PROCESS = (*ENUM_SERVICE_STATUS_PROCESS)(unsafe.Pointer(iter))

                fmt.Printf("Service Name: %s - Display Name: %s - %#v\r\n", syscall.UTF16ToString((*[4096]uint16)(unsafe.Pointer(data.lpServiceName))[:]), syscall.UTF16ToString((*[4096]uint16)(unsafe.Pointer(data.lpDisplayName))[:]), data.ServiceStatusProcess)

                iter = uintptr(unsafe.Pointer(iter + unsafe.Sizeof(sizeTest)))
            }
        } else {
            return nil, fmt.Errorf("Failed to get Service List even with allocated memory.")
        }
    } else {
        return nil, fmt.Errorf("Unable to get size of required memory allocation.")
    }
iamacarpet
  • 417
  • 5
  • 13