Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to call QueryServiceConfig? #47

Closed
mprkins opened this issue Apr 5, 2020 · 1 comment
Closed

How to call QueryServiceConfig? #47

mprkins opened this issue Apr 5, 2020 · 1 comment

Comments

@mprkins
Copy link

mprkins commented Apr 5, 2020

Hello,
I'd like to call QueryServiceConfig to get some more info about the running service.

import winim/lean
import strformat except `&`
import strutils

const SERVICE_EXAMPLE:LPSTR = "SysMain"

proc main =
  var sc = OpenSCManager(nil, nil, SERVICE_QUERY_CONFIG  )
  echo fmt"OpenSCMAnager: {GetLastError()}"
  if sc != 0:
    defer: CloseServiceHandle(sc)

  var os = OpenService(sc,SERVICE_EXAMPLE,SERVICE_QUERY_CONFIG )
  echo fmt"OpenService: {GetLastError()} : os: {os}"
  if os != 0:
      defer: CloseServiceHandle(os)

  var 
      bytesNeeded: DWORD
      
  QueryServiceConfig(os,nil,0,&bytesNeeded)
  echo fmt"QueryServiceConfig: {GetLastError()}"

  var buffer = newString(bytesNeeded)
  let lpServiceConfig = cast[LPQUERY_SERVICE_CONFIGW](&buffer)
  let config = cast[ptr UncheckedArray[LPQUERY_SERVICE_CONFIGW]](lpServiceConfig)

  QueryServiceConfig(os,lpServiceConfig,bytesNeeded,&bytesNeeded)

  echo fmt"Let's print the path {bytesNeeded}"
  for i in 0..<bytesNeeded:
    echo fmt"{config[i].lpBinaryPathName}"

when isMainModule:
  main()

I am not sure what I am doing wrong..I am getting error code 6 (ERROR_INVALID_HANDLE) for OpenService...but...I guess I just dont know what I am doing.
)

@khchen
Copy link
Owner

khchen commented Apr 6, 2020

In nim, if starts a new scope, so that defer: CloseServiceHandle(os) is always run after the scope end.

I modified your code:

import winim/lean
import strformat except `&`
import strutils

const SERVICE_EXAMPLE = "SysMain"

proc main =
  var sc = OpenSCManager(nil, nil, SERVICE_QUERY_CONFIG)
  echo fmt"OpenSCMAnager: {GetLastError()}"
  defer:
    if sc != 0: CloseServiceHandle(sc)

  var os = OpenService(sc, SERVICE_EXAMPLE, SERVICE_QUERY_CONFIG)
  echo fmt"OpenService: {GetLastError()} : os: {os}"
  defer:
    if os != 0: CloseServiceHandle(os)

  var bytesNeeded: DWORD

  QueryServiceConfig(os, nil, 0, &bytesNeeded)
  echo fmt"QueryServiceConfig: {GetLastError()}"

  var buffer = newString(bytesNeeded)
  let lpServiceConfig = cast[LPQUERY_SERVICE_CONFIG](&buffer)

  QueryServiceConfig(os, lpServiceConfig, bytesNeeded, &bytesNeeded)

  echo fmt"Let's print the path {bytesNeeded}"
  echo lpServiceConfig.lpBinaryPathName

when isMainModule:
  main()

@khchen khchen closed this as completed Apr 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants