func Invoke(this *ole.IDispatch, dispid int, riid *ole.GUID, lcid int, flags int16, dispparams *ole.DISPPARAMS, result *ole.VARIANT, pexcepinfo *ole.EXCEPINFO, nerr *uint) uintptr { switch dispid { case 0: log.Println("DataArrival") winsock := (*EventReceiver)(unsafe.Pointer(this)).host var data ole.VARIANT ole.VariantInit(&data) oleutil.CallMethod(winsock, "GetData", &data) array := (*ole.SAFEARRAY)(unsafe.Pointer(uintptr(data.Val))) s := ole.BytePtrToString((*byte)(unsafe.Pointer(uintptr(array.PvData)))) println() println(s) println() case 1: log.Println("Connected") winsock := (*EventReceiver)(unsafe.Pointer(this)).host oleutil.CallMethod(winsock, "SendData", "GET / HTTP/1.0\r\n\r\n") case 3: log.Println("SendProgress") case 4: log.Println("SendComplete") case 5: log.Println("Close") this.Release() case 6: log.Fatal("Error") default: log.Println(dispid) } return ole.E_NOTIMPL }
func main() { ole.CoInitialize(0) unknown, _ := oleutil.CreateObject("Microsoft.XMLHTTP") xmlhttp, _ := unknown.QueryInterface(ole.IID_IDispatch) oleutil.CallMethod(xmlhttp, "open", "GET", "http://rss.slashdot.org/Slashdot/slashdot", false) oleutil.CallMethod(xmlhttp, "send", nil) state := -1 for state != 4 { state = int(oleutil.MustGetProperty(xmlhttp, "readyState").Val) time.Sleep(10000000) } responseXml := oleutil.MustGetProperty(xmlhttp, "responseXml").ToIDispatch() items := oleutil.MustCallMethod(responseXml, "selectNodes", "rdf:RDF/item").ToIDispatch() length := int(oleutil.MustGetProperty(items, "length").Val) for n := 0; n < length; n++ { item := oleutil.MustGetProperty(items, "item", n).ToIDispatch() title := oleutil.MustCallMethod(item, "selectSingleNode", "title").ToIDispatch() println(oleutil.MustGetProperty(title, "text").ToString()) link := oleutil.MustCallMethod(item, "selectSingleNode", "link").ToIDispatch() println(" " + oleutil.MustGetProperty(link, "text").ToString()) title.Release() link.Release() item.Release() } items.Release() xmlhttp.Release() }
func getFriendlyName(portname string) string { // this method panics a lot and i'm not sure why, just catch // the panic and return empty list defer func() { if e := recover(); e != nil { // e is the interface{} typed-value we passed to panic() log.Println("Got panic: ", e) // Prints "Whoops: boom!" } }() var friendlyName string // init COM, oh yeah ole.CoInitialize(0) defer ole.CoUninitialize() unknown, _ := oleutil.CreateObject("WbemScripting.SWbemLocator") defer unknown.Release() wmi, _ := unknown.QueryInterface(ole.IID_IDispatch) defer wmi.Release() // service is a SWbemServices serviceRaw, _ := oleutil.CallMethod(wmi, "ConnectServer") service := serviceRaw.ToIDispatch() defer service.Release() // result is a SWBemObjectSet //pname := syscall.StringToUTF16("SELECT * FROM Win32_PnPEntity where Name like '%" + "COM35" + "%'") pname := "SELECT * FROM Win32_PnPEntity where Name like '%" + portname + "%'" resultRaw, _ := oleutil.CallMethod(service, "ExecQuery", pname) result := resultRaw.ToIDispatch() defer result.Release() countVar, _ := oleutil.GetProperty(result, "Count") count := int(countVar.Val) for i := 0; i < count; i++ { // item is a SWbemObject, but really a Win32_Process itemRaw, _ := oleutil.CallMethod(result, "ItemIndex", i) item := itemRaw.ToIDispatch() defer item.Release() asString, _ := oleutil.GetProperty(item, "Name") println(asString.ToString()) friendlyName = asString.ToString() } return friendlyName }
func main() { ole.CoInitialize(0) unknown, _ := oleutil.CreateObject("Agent.Control.1") agent, _ := unknown.QueryInterface(ole.IID_IDispatch) oleutil.PutProperty(agent, "Connected", true) characters := oleutil.MustGetProperty(agent, "Characters").ToIDispatch() oleutil.CallMethod(characters, "Load", "Merlin", "c:\\windows\\msagent\\chars\\Merlin.acs") character := oleutil.MustCallMethod(characters, "Character", "Merlin").ToIDispatch() oleutil.CallMethod(character, "Show") oleutil.CallMethod(character, "Speak", "こんにちわ世界") time.Sleep(4000000000) }
func QueryWmiFields(query string, fields []string) ([]map[string]string, error) { if len(fields) == 1 && fields[0] == "*" { logging.Errorf("`select * ` not supported, need to address fields explicitly.") return nil, fmt.Errorf("`select * ` not supported, need to address fields explicitly.") } resultRaw, err := oleutil.CallMethod(wmi_service, "ExecQuery", query) if err != nil { logging.Error("ExecQuery Failed: ", err) return nil, fmt.Errorf("ExecQuery Failed: %v", err) } result := resultRaw.ToIDispatch() defer result.Release() countVar, err := oleutil.GetProperty(result, "Count") if err != nil { logging.Errorf("Get result count Failed: %v", err) return nil, fmt.Errorf("Get result count Failed: %v", err) } count := int(countVar.Val) resultMap := []map[string]string{} for i := 0; i < count; i++ { itemMap := make(map[string]string) itemRaw, err := oleutil.CallMethod(result, "ItemIndex", i) if err != nil { return nil, fmt.Errorf("ItemIndex Failed: %v", err) } item := itemRaw.ToIDispatch() defer item.Release() for _, field := range fields { asString, err := oleutil.GetProperty(item, field) if err == nil { itemMap[field] = fmt.Sprintf("%v", asString.Value()) } else { fmt.Println(err) } } resultMap = append(resultMap, itemMap) logging.Tracef("wmi query result: %+v", itemMap) } logging.Tracef("wmi query result count: %d", len(resultMap)) return resultMap, nil }
func (c *win_wmi_collector) query(query string, fields []string) ([]map[string]string, error) { if c.service != nil { resultRaw, err := oleutil.CallMethod(c.service, "ExecQuery", query) if err != nil { logging.Error("ExecQuery Failed: ", err) return nil, fmt.Errorf("ExecQuery Failed: %v", err) } result := resultRaw.ToIDispatch() defer result.Release() countVar, err := oleutil.GetProperty(result, "Count") if err != nil { logging.Error("Get result count Failed: ", err) return nil, fmt.Errorf("Get result count Failed: %v", err) } count := int(countVar.Val) resultMap := []map[string]string{} for i := 0; i < count; i++ { itemMap := make(map[string]string) itemRaw, err := oleutil.CallMethod(result, "ItemIndex", i) if err != nil { return nil, fmt.Errorf("ItemIndex Failed: %v", err) } item := itemRaw.ToIDispatch() defer item.Release() for _, field := range fields { asString, err := oleutil.GetProperty(item, field) if err == nil { itemMap[field] = fmt.Sprintf("%v", asString.Value()) } else { logging.Errorf("cannot find field in SWbemObject: %v", err) } } resultMap = append(resultMap, itemMap) logging.Tracef("wmi query result: %+v", itemMap) } logging.Tracef("wmi query result count: %d", len(resultMap)) return resultMap, nil } else { logging.Error("win_wmi_collector c.service is nil") return nil, fmt.Errorf("win_wmi_collector c.service is nil") } }
func (pt *PowerPoint) open(inFile string) (err error) { ole.CoInitialize(0) var unknown *ole.IUnknown unknown, err = oleutil.CreateObject("PowerPoint.Application") if err != nil { return } pt.app, err = unknown.QueryInterface(ole.IID_IDispatch) if err != nil { return } _, err = oleutil.PutProperty(pt.app, "DisplayAlerts", 1) if err != nil { return } pt.presentations, err = oleutil.GetProperty(pt.app, "Presentations") if err != nil { return } pt.ppt, err = oleutil.CallMethod(pt.presentations.ToIDispatch(), "Open", inFile, -1, 0, 0) if err != nil { return } return }
func main() { ole.CoInitialize(0) unknown, _ := oleutil.CreateObject("{248DD896-BB45-11CF-9ABC-0080C7E7B78D}") winsock, _ := unknown.QueryInterface(ole.IID_IDispatch) iid, _ := ole.CLSIDFromString("{248DD893-BB45-11CF-9ABC-0080C7E7B78D}") dest := &EventReceiver{} dest.lpVtbl = &EventReceiverVtbl{} dest.lpVtbl.pQueryInterface = syscall.NewCallback(QueryInterface) dest.lpVtbl.pAddRef = syscall.NewCallback(AddRef) dest.lpVtbl.pRelease = syscall.NewCallback(Release) dest.lpVtbl.pGetTypeInfoCount = syscall.NewCallback(GetTypeInfoCount) dest.lpVtbl.pGetTypeInfo = syscall.NewCallback(GetTypeInfo) dest.lpVtbl.pGetIDsOfNames = syscall.NewCallback(GetIDsOfNames) dest.lpVtbl.pInvoke = syscall.NewCallback(Invoke) dest.host = winsock oleutil.ConnectObject(winsock, iid, (*ole.IUnknown)(unsafe.Pointer(dest))) _, err := oleutil.CallMethod(winsock, "Connect", "127.0.0.1", 80) if err != nil { log.Fatal(err) } var m ole.Msg for dest.ref != 0 { ole.GetMessage(&m, 0, 0, 0) ole.DispatchMessage(&m) } }
func (rc *AdodbRows) Close() error { _, err := oleutil.CallMethod(rc.rc, "Close") if err != nil { return err } return nil }
func (tx *AdodbTx) Commit() error { _, err := oleutil.CallMethod(tx.c.db, "CommitTrans") if err != nil { return err } return nil }
func (tx *AdodbTx) Rollback() error { _, err := oleutil.CallMethod(tx.c.db, "Rollback") if err != nil { return err } return nil }
func (c *AdodbConn) Begin() (driver.Tx, error) { _, err := oleutil.CallMethod(c.db, "BeginTrans") if err != nil { return nil, err } return &AdodbTx{c}, nil }
func (c *win_wmi_collector) connect() (err error) { // use COINIT_MULTITHREADED model ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED) unknown, err := oleutil.CreateObject("WbemScripting.SWbemLocator") if err != nil { logging.Critical("oleutil.CreateObject Failed: ", err) return err } defer unknown.Release() wmi, err := unknown.QueryInterface(ole.IID_IDispatch) if err != nil { logging.Critical("QueryInterface Failed: ", err) return err } defer wmi.Release() serviceRaw, err := oleutil.CallMethod(wmi, "ConnectServer") if err != nil { logging.Critical("Connect to Server Failed", err) return err } c.service = serviceRaw.ToIDispatch() return nil }
func (c *AdodbConn) Close() error { _, err := oleutil.CallMethod(c.db, "Close") if err != nil { return err } c.db = nil ole.CoUninitialize() return nil }
func (s *AdodbStmt) Query(args []driver.Value) (driver.Rows, error) { if err := s.bind(args); err != nil { return nil, err } rc, err := oleutil.CallMethod(s.s, "Execute") if err != nil { return nil, err } return &AdodbRows{s, rc.ToIDispatch(), -1, nil}, nil }
func (s *AdodbStmt) Exec(args []driver.Value) (driver.Result, error) { if err := s.bind(args); err != nil { return nil, err } _, err := oleutil.CallMethod(s.s, "Execute") if err != nil { return nil, err } return driver.ResultNoRows, nil }
func main() { ole.CoInitialize(0) unknown, _ := oleutil.CreateObject("Excel.Application") excel, _ := unknown.QueryInterface(ole.IID_IDispatch) oleutil.PutProperty(excel, "Visible", true) workbooks := oleutil.MustGetProperty(excel, "Workbooks").ToIDispatch() workbook := oleutil.MustCallMethod(workbooks, "Add", nil).ToIDispatch() worksheet := oleutil.MustGetProperty(workbook, "Worksheets", 1).ToIDispatch() cell := oleutil.MustGetProperty(worksheet, "Cells", 1, 1).ToIDispatch() oleutil.PutProperty(cell, "Value", 12345) time.Sleep(2000000000) oleutil.PutProperty(workbook, "Saved", true) oleutil.CallMethod(workbook, "Close", false) oleutil.CallMethod(excel, "Quit") excel.Release() ole.CoUninitialize() }
func (s *AdodbStmt) bind(args []driver.Value) error { if s.b != nil { for i, v := range args { var b string = "?" if len(s.b) < i { b = s.b[i] } unknown, err := oleutil.CallMethod(s.s, "CreateParameter", b, 12, 1) if err != nil { return err } param := unknown.ToIDispatch() defer param.Release() _, err = oleutil.PutProperty(param, "Value", v) if err != nil { return err } _, err = oleutil.CallMethod(s.ps, "Append", param) if err != nil { return err } } } else { for i, v := range args { var varval ole.VARIANT varval.VT = ole.VT_I4 varval.Val = int64(i) val, err := oleutil.CallMethod(s.ps, "Item", &varval) if err != nil { return err } item := val.ToIDispatch() defer item.Release() _, err = oleutil.PutProperty(item, "Value", v) if err != nil { return err } } } return nil }
func createMdb(f string) error { unk, err := oleutil.CreateObject("ADOX.Catalog") if err != nil { return err } cat, err := unk.QueryInterface(ole.IID_IDispatch) if err != nil { return err } _, err = oleutil.CallMethod(cat, "Create", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+f+";") if err != nil { return err } return nil }
func (s *AdodbStmt) NumInput() int { if s.b != nil { return len(s.b) } _, err := oleutil.CallMethod(s.ps, "Refresh") if err != nil { return -1 } val, err := oleutil.GetProperty(s.ps, "Count") if err != nil { return -1 } c := int(val.Val) return c }
func (d *AdodbDriver) Open(dsn string) (driver.Conn, error) { ole.CoInitialize(0) unknown, err := oleutil.CreateObject("ADODB.Connection") if err != nil { return nil, err } db, err := unknown.QueryInterface(ole.IID_IDispatch) if err != nil { return nil, err } _, err = oleutil.CallMethod(db, "Open", dsn) if err != nil { return nil, err } return &AdodbConn{db}, nil }
func main() { ole.CoInitialize(0) unknown, _ := oleutil.CreateObject("InternetExplorer.Application") ie, _ := unknown.QueryInterface(ole.IID_IDispatch) oleutil.CallMethod(ie, "Navigate", "http://www.google.com") oleutil.PutProperty(ie, "Visible", true) for { if oleutil.MustGetProperty(ie, "Busy").Val == 0 { break } } time.Sleep(1e9) document := oleutil.MustGetProperty(ie, "document").ToIDispatch() window := oleutil.MustGetProperty(document, "parentWindow").ToIDispatch() // set 'golang' to text box. oleutil.MustCallMethod(window, "eval", "document.getElementsByName('q')[0].value = 'golang'") // click btnG. btnG := oleutil.MustCallMethod(window, "eval", "document.getElementsByName('btnG')[0]").ToIDispatch() oleutil.MustCallMethod(btnG, "click") }
func (wd *Word) open(inFile string) (err error) { ole.CoInitialize(0) var unknown *ole.IUnknown unknown, err = oleutil.CreateObject("Word.Application") if err != nil { return } wd.app, err = unknown.QueryInterface(ole.IID_IDispatch) if err != nil { return } _, err = oleutil.PutProperty(wd.app, "Visible", false) if err != nil { return } _, err = oleutil.PutProperty(wd.app, "DisplayAlerts", 0) if err != nil { return } wd.documents, err = oleutil.GetProperty(wd.app, "Documents") if err != nil { return } wd.doc, err = oleutil.CallMethod(wd.documents.ToIDispatch(), "Open", inFile) if err != nil { return } return }
func (wd *Word) Export(inFile, outDir string) (outFile string, err error) { outFile = filepath.Join(outDir, filepath.Base(inFile+".pdf")) defer func() { if err != nil { outFile = "" } wd.close() }() err = wd.open(inFile) if err != nil { return } _, err = oleutil.CallMethod(wd.doc.ToIDispatch(), "ExportAsFixedFormat", outFile, 17) if err != nil { return } return }
func (pt *PowerPoint) Export(inFile, outDir string) (outFile string, err error) { outFile = filepath.Join(outDir, filepath.Base(inFile+".pdf")) defer func() { if err != nil { outFile = "" } pt.close() }() err = pt.open(inFile) if err != nil { return } _, err = oleutil.CallMethod(pt.ppt.ToIDispatch(), "SaveAs", outFile, 32) if err != nil { return } return }
func init() { ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED) unknown, err := oleutil.CreateObject("WbemScripting.SWbemLocator") if err != nil { logging.Criticalf("oleutil.CreateObject Failed: %v", err) panic(err) } defer unknown.Release() wmi, err := unknown.QueryInterface(ole.IID_IDispatch) if err != nil { logging.Criticalf("QueryInterface Failed: %v", err) panic(err) } defer wmi.Release() serviceRaw, err := oleutil.CallMethod(wmi, "ConnectServer") if err != nil { logging.Criticalf("Connect to Server Failed: %v", err) panic(err) } wmi_service = serviceRaw.ToIDispatch() }
func (rc *AdodbRows) Columns() []string { if rc.nc != len(rc.cols) { unknown, err := oleutil.GetProperty(rc.rc, "Fields") if err != nil { return []string{} } fields := unknown.ToIDispatch() defer fields.Release() val, err := oleutil.GetProperty(fields, "Count") if err != nil { return []string{} } rc.nc = int(val.Val) rc.cols = make([]string, rc.nc) for i := 0; i < rc.nc; i++ { var varval ole.VARIANT varval.VT = ole.VT_I4 varval.Val = int64(i) val, err := oleutil.CallMethod(fields, "Item", &varval) if err != nil { return []string{} } item := val.ToIDispatch() if err != nil { return []string{} } name, err := oleutil.GetProperty(item, "Name") if err != nil { return []string{} } rc.cols[i] = name.ToString() item.Release() } } return rc.cols }
func (c *AdodbConn) exec(cmd string) error { _, err := oleutil.CallMethod(c.db, "Execute", cmd) return err }
func (rc *AdodbRows) Next(dest []driver.Value) error { unknown, err := oleutil.GetProperty(rc.rc, "EOF") if err != nil { return io.EOF } if unknown.Val != 0 { return io.EOF } unknown, err = oleutil.GetProperty(rc.rc, "Fields") if err != nil { return err } fields := unknown.ToIDispatch() defer fields.Release() for i := range dest { var varval ole.VARIANT varval.VT = ole.VT_I4 varval.Val = int64(i) val, err := oleutil.CallMethod(fields, "Item", &varval) if err != nil { return err } field := val.ToIDispatch() defer field.Release() typ, err := oleutil.GetProperty(field, "Type") if err != nil { return err } val, err = oleutil.GetProperty(field, "Value") if err != nil { return err } sc, err := oleutil.GetProperty(field, "NumericScale") field.Release() if val.VT == 1 /* VT_NULL */ { dest[i] = nil continue } switch typ.Val { case 0: // ADEMPTY dest[i] = nil case 2: // ADSMALLINT dest[i] = int64(int16(val.Val)) case 3: // ADINTEGER dest[i] = int64(int32(val.Val)) case 4: // ADSINGLE dest[i] = float64(math.Float32frombits(uint32(val.Val))) case 5: // ADDOUBLE dest[i] = math.Float64frombits(uint64(val.Val)) case 6: // ADCURRENCY dest[i] = float64(val.Val) / 10000 case 7: // ADDATE // see http://blogs.msdn.com/b/ericlippert/archive/2003/09/16/eric-s-complete-guide-to-vt-date.aspx d, t := math.Modf(math.Float64frombits(uint64(val.Val))) t = math.Abs(t) dest[i] = time.Date(1899, 12, 30+int(d), 0, 0, int(t*86400), 0, time.Local) case 8: // ADBSTR dest[i] = val.ToString() case 9: // ADIDISPATCH dest[i] = val.ToIDispatch() case 10: // ADERROR // TODO case 11: // ADBOOLEAN dest[i] = val.Val != 0 case 12: // ADVARIANT dest[i] = val case 13: // ADIUNKNOWN dest[i] = val.ToIUnknown() case 14: // ADDECIMAL dest[i] = float64(val.Val) case 16: // ADTINYINT dest[i] = int8(val.Val) case 17: // ADUNSIGNEDTINYINT dest[i] = uint8(val.Val) case 18: // ADUNSIGNEDSMALLINT dest[i] = uint16(val.Val) case 19: // ADUNSIGNEDINT dest[i] = uint32(val.Val) case 20: // ADBIGINT dest[i] = big.NewInt(val.Val) case 21: // ADUNSIGNEDBIGINT // TODO case 72: // ADGUID dest[i] = val.ToString() case 128: // ADBINARY sa := (*ole.SAFEARRAY)(unsafe.Pointer(uintptr(val.Val))) dest[i] = (*[1 << 30]byte)(unsafe.Pointer(uintptr(sa.Data)))[0:sa.Bounds.Elements] case 129: // ADCHAR dest[i] = val.ToString() //uint8(val.Val) case 130: // ADWCHAR dest[i] = val.ToString() //uint16(val.Val) case 131: // ADNUMERIC sub := math.Pow(10, float64(sc.Val)) dest[i] = float64(float64(val.Val) / sub) case 132: // ADUSERDEFINED dest[i] = uintptr(val.Val) case 133: // ADDBDATE // see http://blogs.msdn.com/b/ericlippert/archive/2003/09/16/eric-s-complete-guide-to-vt-date.aspx d := math.Float64frombits(uint64(val.Val)) dest[i] = time.Date(1899, 12, 30+int(d), 0, 0, 0, 0, time.Local) case 134: // ADDBTIME t := math.Float64frombits(uint64(val.Val)) dest[i] = time.Date(0, 1, 1, 0, 0, int(t*86400), 0, time.Local) case 135: // ADDBTIMESTAMP d, t := math.Modf(math.Float64frombits(uint64(val.Val))) t = math.Abs(t) dest[i] = time.Date(1899, 12, 30+int(d), 0, 0, int(t*86400), 0, time.Local) case 136: // ADCHAPTER dest[i] = val.ToString() case 200: // ADVARCHAR dest[i] = val.ToString() case 201: // ADLONGVARCHAR dest[i] = val.ToString() case 202: // ADVARWCHAR dest[i] = val.ToString() case 203: // ADLONGVARWCHAR dest[i] = val.ToString() case 204: // ADVARBINARY // TODO case 205: // ADLONGVARBINARY sa := (*ole.SAFEARRAY)(unsafe.Pointer(uintptr(val.Val))) dest[i] = (*[1 << 30]byte)(unsafe.Pointer(uintptr(sa.Data)))[0:sa.Bounds.Elements] } } _, err = oleutil.CallMethod(rc.rc, "MoveNext") if err != nil { return err } return nil }
func getListViaWmiPnpEntity() ([]OsSerialPort, os.SyscallError) { //log.Println("Doing getListViaWmiPnpEntity()") // this method panics a lot and i'm not sure why, just catch // the panic and return empty list defer func() { if e := recover(); e != nil { // e is the interface{} typed-value we passed to panic() log.Println("Got panic: ", e) // Prints "Whoops: boom!" } }() var err os.SyscallError //var friendlyName string // init COM, oh yeah ole.CoInitialize(0) defer ole.CoUninitialize() unknown, _ := oleutil.CreateObject("WbemScripting.SWbemLocator") defer unknown.Release() wmi, _ := unknown.QueryInterface(ole.IID_IDispatch) defer wmi.Release() // service is a SWbemServices serviceRaw, _ := oleutil.CallMethod(wmi, "ConnectServer") service := serviceRaw.ToIDispatch() defer service.Release() // result is a SWBemObjectSet //pname := syscall.StringToUTF16("SELECT * FROM Win32_PnPEntity where Name like '%" + "COM35" + "%'") pname := "SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0 and Name like '%(COM%'" //pname := "SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0" resultRaw, err2 := oleutil.CallMethod(service, "ExecQuery", pname) //log.Println("Got result from oleutil.CallMethod") if err2 != nil { // we got back an error or empty list log.Printf("Got an error back from oleutil.CallMethod. err:%v", err2) return nil, err } result := resultRaw.ToIDispatch() defer result.Release() countVar, _ := oleutil.GetProperty(result, "Count") count := int(countVar.Val) list := make([]OsSerialPort, count) for i := 0; i < count; i++ { // items we're looping thru look like below and // thus we can query for any of these names /* __GENUS : 2 __CLASS : Win32_PnPEntity __SUPERCLASS : CIM_LogicalDevice __DYNASTY : CIM_ManagedSystemElement __RELPATH : Win32_PnPEntity.DeviceID="USB\\VID_1D50&PID_606D&MI_02\\6&2F09EA14&0&0002" __PROPERTY_COUNT : 24 __DERIVATION : {CIM_LogicalDevice, CIM_LogicalElement, CIM_ManagedSystemElement} __SERVER : JOHN-ATIV __NAMESPACE : root\cimv2 __PATH : \\JOHN-ATIV\root\cimv2:Win32_PnPEntity.DeviceID="USB\\VID_1D50&PID_606D&MI_02\\6&2F09EA14 &0&0002" Availability : Caption : TinyG v2 (Data Channel) (COM12) ClassGuid : {4d36e978-e325-11ce-bfc1-08002be10318} CompatibleID : {USB\Class_02&SubClass_02&Prot_01, USB\Class_02&SubClass_02, USB\Class_02} ConfigManagerErrorCode : 0 ConfigManagerUserConfig : False CreationClassName : Win32_PnPEntity Description : TinyG v2 (Data Channel) DeviceID : USB\VID_1D50&PID_606D&MI_02\6&2F09EA14&0&0002 ErrorCleared : ErrorDescription : HardwareID : {USB\VID_1D50&PID_606D&REV_0097&MI_02, USB\VID_1D50&PID_606D&MI_02} InstallDate : LastErrorCode : Manufacturer : Synthetos (www.synthetos.com) Name : TinyG v2 (Data Channel) (COM12) PNPDeviceID : USB\VID_1D50&PID_606D&MI_02\6&2F09EA14&0&0002 PowerManagementCapabilities : PowerManagementSupported : Service : usbser Status : OK StatusInfo : SystemCreationClassName : Win32_ComputerSystem SystemName : JOHN-ATIV PSComputerName : JOHN-ATIV */ // item is a SWbemObject, but really a Win32_Process itemRaw, _ := oleutil.CallMethod(result, "ItemIndex", i) item := itemRaw.ToIDispatch() defer item.Release() asString, _ := oleutil.GetProperty(item, "Name") //log.Println(asString.ToString()) // get the com port //if false { s := strings.Split(asString.ToString(), "(COM")[1] s = "COM" + s s = strings.Split(s, ")")[0] list[i].Name = s //} // get the deviceid so we can figure out related ports // it will look similar to // USB\VID_1D50&PID_606D&MI_00\6&2F09EA14&0&0000 deviceIdStr, _ := oleutil.GetProperty(item, "DeviceID") devIdItems := strings.Split(deviceIdStr.ToString(), "&") log.Printf("DeviceId elements:%v", devIdItems) if len(devIdItems) > 3 { list[i].SerialNumber = devIdItems[3] list[i].IdProduct = strings.Replace(devIdItems[1], "PID_", "", 1) list[i].IdVendor = strings.Replace(devIdItems[0], "USB\\VID_", "", 1) } else { list[i].SerialNumber = deviceIdStr.ToString() pidMatch := regexp.MustCompile("PID_(\\d+)").FindAllStringSubmatch(deviceIdStr.ToString(), -1) if len(pidMatch) > 0 { if len(pidMatch[0]) > 1 { list[i].IdProduct = pidMatch[0][1] } } vidMatch := regexp.MustCompile("VID_(\\d+)").FindAllStringSubmatch(deviceIdStr.ToString(), -1) if len(vidMatch) > 0 { if len(vidMatch[0]) > 1 { list[i].IdVendor = vidMatch[0][1] } } } list[i].IdVendor = "0x" + list[i].IdVendor list[i].IdProduct = "0x" + list[i].IdProduct manufStr, _ := oleutil.GetProperty(item, "Manufacturer") list[i].Manufacturer = manufStr.ToString() descStr, _ := oleutil.GetProperty(item, "Description") list[i].Product = descStr.ToString() //classStr, _ := oleutil.GetProperty(item, "CreationClassName") //list[i].DeviceClass = classStr.ToString() } return list, err }