dqpwdai095465 2016-05-16 09:39 采纳率: 100%
浏览 67
已采纳

在Go中调用EnumServicesStatusEx,内存分配?

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? :).

  • 写回答

1条回答 默认 最新

  • doujiyun0041 2016-05-20 15:40
    关注

    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
    ", 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.")
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向