StackExchange / wmi

WMI for Go
http://godoc.org/github.com/StackExchange/wmi
MIT License
433 stars 173 forks source link

Issue with string field in WMI class, if no value results in error "string": unsupported type (<nil>) #28

Closed RajGupta-Continuum closed 7 years ago

RajGupta-Continuum commented 7 years ago

I am retrieving OS Info using StackExchange/wmi package and getting error on windows machine where there is no service pack. Error is "wmi: cannot load field "CSDVersion" into a "string": unsupported type ()". So issue with CSDVersion which is string field and if there is no value then returning error, https://github.com/StackExchange/wmi/blob/master/wmi.go#L395.

To fix this we should move to next field instead of returning error. Let me know your thought on this.

Below is go code snippet.

type Win32_OperatingSystem struct { Caption string Version string CSDVersion string OSArchitecture string Manufacturer string SerialNumber string InstallDate time.Time }

// GetOSInfo returns OS information using wmi func GetOSInfo() (Win32_OperatingSystem, error) { var dst []Win32_OperatingSystem q := wmi.CreateQuery(&dst, "") err := wmi.Query(q, &dst) if err != nil { return Win32_OperatingSystem{}, err } return dst[0], nil }

kylevanek commented 7 years ago

This has been an annoying issue for me as well. I ended up just commenting out the return starting at line 395. Doing this just leaves the value to its default.

Would be great if we could provide a flag that would enable/disable this behavior.

RajGupta-Continuum commented 7 years ago

I found solution to this problem after going through commits history which already exists. Need to change value type to pointer type.

e.g CSDVersion string => CSDVersion *string

https://github.com/StackExchange/wmi/commit/17a72a6b667903532b0b40ba1b8041981f4a9642

so closing this issue.

@kylevanek, FYI.