func toShort(path string) (string, error) { p := syscall.StringToUTF16(path) b := p // GetShortPathName says we can reuse buffer n, err := syscall.GetShortPathName(&p[0], &b[0], uint32(len(b))) if err != nil { return "", err } if n > uint32(len(b)) { b = make([]uint16, n) n, err = syscall.GetShortPathName(&p[0], &b[0], uint32(len(b))) if err != nil { return "", err } } return syscall.UTF16ToString(b), nil }
func getTuntapComponentId() (string, error) { adapters, err := registry.OpenKey(registry.LOCAL_MACHINE, ADAPTER_KEY, registry.READ) if err != nil { return "", err } var i uint32 for ; i < 1000; i++ { var name_length uint32 = TAPWIN32_MAX_REG_SIZE buf := make([]uint16, name_length) if err = syscall.RegEnumKeyEx( syscall.Handle(adapters), i, &buf[0], &name_length, nil, nil, nil, nil); err != nil { return "", err } key_name := syscall.UTF16ToString(buf[:]) adapter, err := registry.OpenKey(adapters, key_name, registry.READ) if err != nil { return "", err } name := syscall.StringToUTF16("ComponentId") name2 := syscall.StringToUTF16("NetCfgInstanceId") var valtype uint32 var component_id = make([]byte, TAPWIN32_MAX_REG_SIZE) var componentLen = uint32(len(component_id)) if err = syscall.RegQueryValueEx( syscall.Handle(adapter), &name[0], nil, &valtype, &component_id[0], &componentLen); err != nil { return "", err } if unicodeTostring(component_id) == TUNTAP_COMPONENT_ID { var valtype uint32 var netCfgInstanceId = make([]byte, TAPWIN32_MAX_REG_SIZE) var netCfgInstanceIdLen = uint32(len(netCfgInstanceId)) if err = syscall.RegQueryValueEx( syscall.Handle(adapter), &name2[0], nil, &valtype, &netCfgInstanceId[0], &netCfgInstanceIdLen); err != nil { return "", err } fmt.Println("Device:", unicodeTostring(netCfgInstanceId)) return unicodeTostring(netCfgInstanceId), nil } } return "", errors.New("not found component id") }
func (p *Printer) StartDocument(name, datatype string) error { d := DOC_INFO_1{ DocName: &(syscall.StringToUTF16(name))[0], OutputFile: nil, Datatype: &(syscall.StringToUTF16(datatype))[0], } return StartDocPrinter(p.h, 1, &d) }
func (ni *NotifyIcon) showMessage(title, info string, iconType uint32) error { nid := ni.notifyIconData() nid.UFlags = NIF_INFO nid.DwInfoFlags = iconType copy(nid.SzInfoTitle[:], syscall.StringToUTF16(title)) copy(nid.SzInfo[:], syscall.StringToUTF16(info)) if !Shell_NotifyIcon(NIM_MODIFY, nid) { return newError("Shell_NotifyIcon") } return nil }
// if not existing, creating new one. func EditVariable(etype EnvType, varName string, varValue string) error { var rootkey HKEY var subkey string if etype == 0 { rootkey = HKEY_CURRENT_USER subkey = USR_SUBKEY } else { rootkey = HKEY_LOCAL_MACHINE subkey = SYS_SUBKEY } var mykey HKEY if ret := RegOpenKeyEx(rootkey, syscall.StringToUTF16Ptr(subkey), 0, KEY_WRITE, &mykey); ret != ERROR_SUCCESS { return errors.New(fmt.Sprintf("EditVariable error, RegOpenKeyEx = %d", ret)) } dataType := REG_SZ if strings.Index(varValue, "%") != -1 { dataType = REG_EXPAND_SZ } if ret := RegSetValueEx(mykey, syscall.StringToUTF16Ptr(varName), 0, uint64(dataType), (*byte)(unsafe.Pointer(syscall.StringToUTF16Ptr(varValue))), // In Bytes. uint32(len(syscall.StringToUTF16(varValue))*2)); ret != ERROR_SUCCESS { return errors.New(fmt.Sprintf("EditVariable error, RegSetValueEx = %d", ret)) } return nil }
func (this *Font) createForDPI(dpi int) w32.HFONT { var lf w32.LOGFONT lf.Height = -w32.MulDiv(this.pointSize, dpi, 72) if this.style&FontBold > 0 { lf.Weight = w32.FW_BOLD } else { lf.Weight = w32.FW_NORMAL } if this.style&FontItalic > 0 { lf.Italic = 1 } if this.style&FontUnderline > 0 { lf.Underline = 1 } if this.style&FontStrikeOut > 0 { lf.StrikeOut = 1 } lf.CharSet = w32.DEFAULT_CHARSET lf.OutPrecision = w32.OUT_TT_PRECIS lf.ClipPrecision = w32.CLIP_DEFAULT_PRECIS lf.Quality = w32.CLEARTYPE_QUALITY lf.PitchAndFamily = w32.VARIABLE_PITCH | w32.FF_SWISS src := syscall.StringToUTF16(this.family) dest := lf.FaceName[:] copy(dest, src) return w32.CreateFontIndirect(&lf) }
func (k *Key) setString(name string, value string, valtype uint32) error { buf := syscall.StringToUTF16(value) return winapi.RegSetValueEx( k.Handle, syscall.StringToUTF16Ptr(name), 0, valtype, (*byte)(unsafe.Pointer(&buf[0])), uint32(len(buf)*2)) }
func (f *Font) createForDPI(dpi int) HFONT { var lf LOGFONT lf.LfHeight = -MulDiv(int32(f.pointSize), int32(dpi), 72) if f.style&FontBold > 0 { lf.LfWeight = FW_BOLD } else { lf.LfWeight = FW_NORMAL } if f.style&FontItalic > 0 { lf.LfItalic = 1 } if f.style&FontUnderline > 0 { lf.LfUnderline = 1 } if f.style&FontStrikeOut > 0 { lf.LfStrikeOut = 1 } lf.LfCharSet = DEFAULT_CHARSET lf.LfOutPrecision = OUT_TT_PRECIS lf.LfClipPrecision = CLIP_DEFAULT_PRECIS lf.LfQuality = CLEARTYPE_QUALITY lf.LfPitchAndFamily = VARIABLE_PITCH | FF_SWISS src := syscall.StringToUTF16(f.family) dest := lf.LfFaceName[:] copy(dest, src) return CreateFontIndirect(&lf) }
// Remove removes the named file or directory. // If there is an error, it will be of type *PathError. func Remove(name string) error { p := &syscall.StringToUTF16(name)[0] // Go file interface forces us to know whether // name is a file or directory. Try both. e := syscall.DeleteFile(p) if e == nil { return nil } e1 := syscall.RemoveDirectory(p) if e1 == nil { return nil } // Both failed: figure out which error to return. if e1 != e { a, e2 := syscall.GetFileAttributes(p) if e2 != nil { e = e2 } else { if a&syscall.FILE_ATTRIBUTE_DIRECTORY != 0 { e = e1 } } } return &PathError{"remove", name, e} }
func (wb *WidgetBase) calculateTextSizeImpl(text string) Size { hdc := GetDC(wb.hWnd) if hdc == 0 { newError("GetDC failed") return Size{} } defer ReleaseDC(wb.hWnd, hdc) hFontOld := SelectObject(hdc, HGDIOBJ(wb.Font().handleForDPI(0))) defer SelectObject(hdc, hFontOld) var size Size lines := strings.Split(text, "\n") for _, line := range lines { var s SIZE str := syscall.StringToUTF16(strings.TrimRight(line, "\r ")) if !GetTextExtentPoint32(hdc, &str[0], int32(len(str)-1), &s) { newError("GetTextExtentPoint32 failed") return Size{} } size.Width = maxi(size.Width, int(s.CX)) size.Height += int(s.CY) } return size }
func (lb *ListBox) calculateMaxItemTextWidth() int { hdc := win.GetDC(lb.hWnd) if hdc == 0 { newError("GetDC failed") return -1 } defer win.ReleaseDC(lb.hWnd, hdc) hFontOld := win.SelectObject(hdc, win.HGDIOBJ(lb.Font().handleForDPI(0))) defer win.SelectObject(hdc, hFontOld) var maxWidth int if lb.model == nil { return -1 } count := lb.model.ItemCount() for i := 0; i < count; i++ { item := lb.itemString(i) var s win.SIZE str := syscall.StringToUTF16(item) if !win.GetTextExtentPoint32(hdc, &str[0], int32(len(str)-1), &s) { newError("GetTextExtentPoint32 failed") return -1 } maxWidth = maxi(maxWidth, int(s.CX)) } return maxWidth }
// Use to write string value to windows registry the HKEY in the next definition HHLM, HKCU, HKCC, HKCR, HKU func WriteStringReg(hkey, path, name, val string) (err error) { var handle syscall.Handle switch hkey { case "HKLM": err = syscall.RegOpenKeyEx(syscall.HKEY_LOCAL_MACHINE, syscall.StringToUTF16Ptr(""), 0, syscall.KEY_CREATE_SUB_KEY, &handle) case "HKCC": err = syscall.RegOpenKeyEx(syscall.HKEY_CURRENT_CONFIG, syscall.StringToUTF16Ptr(""), 0, syscall.KEY_CREATE_SUB_KEY, &handle) case "HKCR": err = syscall.RegOpenKeyEx(syscall.HKEY_CLASSES_ROOT, syscall.StringToUTF16Ptr(""), 0, syscall.KEY_CREATE_SUB_KEY, &handle) case "HKCU": err = syscall.RegOpenKeyEx(syscall.HKEY_CURRENT_USER, syscall.StringToUTF16Ptr(""), 0, syscall.KEY_CREATE_SUB_KEY, &handle) case "HKU": err = syscall.RegOpenKeyEx(syscall.HKEY_USERS, syscall.StringToUTF16Ptr(""), 0, syscall.KEY_CREATE_SUB_KEY, &handle) default: err = errors.New("Unknown HKEY: " + hkey) return } if err != nil { return } defer syscall.RegCloseKey(handle) var d uint32 err = winapi.RegCreateKeyEx(handle, syscall.StringToUTF16Ptr(path), 0, nil, winapi.REG_OPTION_NON_VOLATILE, syscall.KEY_ALL_ACCESS, nil, &handle, &d) if err != nil { return } buf := syscall.StringToUTF16(val) err = winapi.RegSetValueEx(handle, syscall.StringToUTF16Ptr(name), 0, syscall.REG_SZ, (*byte)(unsafe.Pointer(&buf[0])), uint32(len(buf)*2)) return }
func (w *Window) AddTrayIcon(ico Icon, tip string) { if w.trayIconData != nil { return } w.trayIconData = &w32.NOTIFYICONDATA{ /* Size */ uint32(unsafe.Sizeof(*w.trayIconData)), /* Wnd */ w.handle, /* ID */ 1, /* Flags */ w32.NIF_MESSAGE | w32.NIF_ICON | w32.NIF_TIP | w32.NIF_SHOWTIP, /* CallbackMessage */ WM_TRAYICON, /* Icon */ w32.HICON(ico), /* Tip */ [128]uint16{}, /* State */ 0, /* StateMask */ 0, /* Info */ [256]uint16{}, /* TimeoutOrVersion */ w32.NOTIFYICON_VERSION_4, /* InfoTitle */ [64]uint16{}, /* InfoFlags */ 0, /* Item */ w32.GUID{}, /* BalloonIcon */ 0, } tipbuf := syscall.StringToUTF16(tip) copy(w.trayIconData.Tip[:], tipbuf) w32.Shell_NotifyIcon(w32.NIM_ADD, w.trayIconData) }
func (cb *ComboBox) calculateMaxItemTextWidth() int { hdc := GetDC(cb.hWnd) if hdc == 0 { newError("GetDC failed") return -1 } defer ReleaseDC(cb.hWnd, hdc) hFontOld := SelectObject(hdc, HGDIOBJ(cb.Font().handleForDPI(0))) defer SelectObject(hdc, hFontOld) var maxWidth int count := cb.model.ItemCount() for i := 0; i < count; i++ { var s SIZE str := syscall.StringToUTF16(cb.itemString(i)) if !GetTextExtentPoint32(hdc, &str[0], int32(len(str)-1), &s) { newError("GetTextExtentPoint32 failed") return -1 } maxWidth = maxi(maxWidth, int(s.CX)) } return maxWidth }
func genOFN(parent Controller, title, filter string, filterIndex uint, initialDir string, buf []uint16) *w32.OPENFILENAME { var ofn w32.OPENFILENAME ofn.StructSize = uint32(unsafe.Sizeof(ofn)) ofn.Owner = parent.Handle() if filter != "" { filterBuf := make([]uint16, len(filter)+1) copy(filterBuf, syscall.StringToUTF16(filter)) // Replace '|' with the expcted '\0' for i, c := range filterBuf { if byte(c) == '|' { filterBuf[i] = uint16(0) } } ofn.Filter = &filterBuf[0] ofn.FilterIndex = uint32(filterIndex) } ofn.File = &buf[0] ofn.MaxFile = uint32(len(buf)) if initialDir != "" { ofn.InitialDir = syscall.StringToUTF16Ptr(initialDir) } if title != "" { ofn.Title = syscall.StringToUTF16Ptr(title) } ofn.Flags = w32.OFN_FILEMUSTEXIST return &ofn }
func open(path string, flag int, perm os.FileMode) (*os.File, error) { if path == "" { return nil, fmt.Errorf("cannot open empty filename") } var access uint32 switch flag { case syscall.O_RDONLY: access = syscall.GENERIC_READ case syscall.O_WRONLY: access = syscall.GENERIC_WRITE case syscall.O_RDWR: access = syscall.GENERIC_READ | syscall.GENERIC_WRITE case syscall.O_WRONLY | syscall.O_CREAT: access = syscall.GENERIC_ALL default: panic(fmt.Errorf("flag %v is not supported", flag)) } fd, err := syscall.CreateFile(&(syscall.StringToUTF16(path)[0]), access, syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE|syscall.FILE_SHARE_DELETE, nil, syscall.OPEN_ALWAYS, syscall.FILE_ATTRIBUTE_NORMAL, 0) if err != nil { return nil, err } return os.NewFile(uintptr(fd), path), nil }
func MakeDoubleNullTerminatedLpstr(items ...string) *uint16 { chars := []uint16{} for _, s := range items { chars = append(chars, syscall.StringToUTF16(s)...) } chars = append(chars, 0) return &chars[0] }
func GetAllMediaIDs(printerName string, port string) ([]int, error) { //port, err := GetPrinterPort(printerName) res, err := DeviceCapabilities(&(syscall.StringToUTF16(printerName))[0], &(syscall.StringToUTF16(port))[0], DC_PAPERS, nil, nil) var numSizes int = int(res) if err != nil || numSizes <= 0 { return nil, err } buf := make([]uint16, numSizes) _, err = DeviceCapabilities(&(syscall.StringToUTF16(printerName))[0], &(syscall.StringToUTF16(port))[0], DC_PAPERS, &buf[0], nil) if err != nil { return nil, err } mediaIDs := make([]int, numSizes) for i, mediaID := range buf { mediaIDs[i] = int(mediaID) } return mediaIDs, nil }
func Open(name string) (*Printer, error) { var p Printer // TODO: implement pDefault parameter err := OpenPrinter(&(syscall.StringToUTF16(name))[0], &p.h, 0) if err != nil { return nil, err } return &p, nil }
func GetAllResolutions(printerName string, port string) ([]int, error) { //port, err := GetPrinterPort(printerName) res, err := DeviceCapabilities(&(syscall.StringToUTF16(printerName))[0], &(syscall.StringToUTF16(port))[0], DC_ENUMRESOLUTIONS, nil, nil) var nResolutions int = int(res) if err != nil || nResolutions <= 0 { return nil, err } buf := make([]uint, nResolutions*2) _, err = DeviceCapabilities(&(syscall.StringToUTF16(printerName))[0], &(syscall.StringToUTF16(port))[0], DC_ENUMRESOLUTIONS, (*uint16)(unsafe.Pointer(&buf[0])), nil) if err != nil { return nil, err } resolutions := make([]int, nResolutions*2) for i, resolution := range buf { resolutions[i] = int(resolution) } return resolutions, nil }
func GetAllMediaSizes(printerName string, port string) ([]int, error) { //port, err := GetPrinterPort(printerName) res, err := DeviceCapabilities(&(syscall.StringToUTF16(printerName))[0], &(syscall.StringToUTF16(port))[0], DC_PAPERSIZE, nil, nil) var nPapers int = int(res) if err != nil || nPapers <= 0 { return nil, err } buf := make([]Point, nPapers) _, err = DeviceCapabilities(&(syscall.StringToUTF16(printerName))[0], &(syscall.StringToUTF16(port))[0], DC_PAPERSIZE, (*uint16)(unsafe.Pointer(&buf[0])), nil) if err != nil { return nil, err } mediaSizes := make([]int, nPapers*2) for i, mediaID := range buf { mediaSizes[i*2] = int(mediaID.X) mediaSizes[i*2+1] = int(mediaID.Y) } return mediaSizes, nil }
func (dlg *FileDialog) show(owner RootWidget, fun func(ofn *OPENFILENAME) bool) (accepted bool, err error) { ofn := new(OPENFILENAME) ofn.LStructSize = uint32(unsafe.Sizeof(*ofn)) if owner != nil { ofn.HwndOwner = owner.Handle() } filter := make([]uint16, len(dlg.Filter)+2) copy(filter, syscall.StringToUTF16(dlg.Filter)) // Replace '|' with the expected '\0'. for i, c := range filter { if byte(c) == '|' { filter[i] = uint16(0) } } ofn.LpstrFilter = &filter[0] ofn.NFilterIndex = uint32(dlg.FilterIndex) filePath := make([]uint16, 1024) copy(filePath, syscall.StringToUTF16(dlg.FilePath)) ofn.LpstrFile = &filePath[0] ofn.NMaxFile = uint32(len(filePath)) ofn.LpstrInitialDir = syscall.StringToUTF16Ptr(dlg.InitialDirPath) ofn.LpstrTitle = syscall.StringToUTF16Ptr(dlg.Title) ofn.Flags = OFN_FILEMUSTEXIST if !fun(ofn) { errno := CommDlgExtendedError() if errno != 0 { err = newError(fmt.Sprintf("Error %d", errno)) } return } dlg.FilePath = syscall.UTF16ToString(filePath) accepted = true return }
func MakeFileMutex(filename string) *FileMutex { if filename == "" { return &FileMutex{fd: INVALID_FILE_HANDLE} } fd, err := syscall.CreateFile(&(syscall.StringToUTF16(filename)[0]), syscall.GENERIC_READ|syscall.GENERIC_WRITE, syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE, nil, syscall.OPEN_ALWAYS, syscall.FILE_ATTRIBUTE_NORMAL, 0) if err != nil { panic(err) } return &FileMutex{fd: fd} }
func (tp *TabPage) tcItem() *win.TCITEM { text := syscall.StringToUTF16(tp.Title()) item := &win.TCITEM{ Mask: win.TCIF_TEXT, PszText: &text[0], CchTextMax: int32(len(text)), } return item }
func GetAllMediaNames(printerName string, port string) ([]string, error) { //port, err := GetPrinterPort(printerName) paperNameLength := 64 res, err := DeviceCapabilities(&(syscall.StringToUTF16(printerName))[0], &(syscall.StringToUTF16(port))[0], DC_PAPERNAMES, nil, nil) var nReturned int = int(res) if err != nil || nReturned <= 0 { return nil, err } buf := make([]uint16, nReturned*paperNameLength) _, err = DeviceCapabilities(&(syscall.StringToUTF16(printerName))[0], &(syscall.StringToUTF16(port))[0], DC_PAPERNAMES, &buf[0], nil) if err != nil { return nil, err } names := make([]string, 0) for i := 0; i < nReturned; i++ { name := syscall.UTF16ToString(buf[i*paperNameLength : (i+1)*paperNameLength]) names = append(names, name) } return names, err }
func (l *locker) Lock(lockfilename string) error { if l.fd != INVALID_FILE_HANDLE { return ErrFailedToAcquireLock } var flags uint32 if l.nonblock { flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY } else { flags = LOCKFILE_EXCLUSIVE_LOCK } if lockfilename == "" { return ErrLockFileEmpty } fd, err := syscall.CreateFile(&(syscall.StringToUTF16(lockfilename)[0]), syscall.GENERIC_READ|syscall.GENERIC_WRITE, syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE, nil, syscall.OPEN_ALWAYS, syscall.FILE_ATTRIBUTE_NORMAL, 0) if err != nil { return fmt.Errorf("setlock: fatal: unable to open %s: temporary failure", lockfilename) } if fd == INVALID_FILE_HANDLE { return ErrFailedToAcquireLock } defer func() { // Close this descriptor if we failed to lock if l.fd == INVALID_FILE_HANDLE { // l.fd is not set, I guess we didn't suceed syscall.CloseHandle(fd) } }() var ol syscall.Overlapped var mu sync.RWMutex mu.Lock() defer mu.Unlock() r1, _, _ := syscall.Syscall6( procLockFileEx.Addr(), 6, uintptr(fd), // handle uintptr(flags), uintptr(0), // reserved uintptr(1), // locklow uintptr(0), // lockhigh uintptr(unsafe.Pointer(&ol)), ) if r1 == 0 { return ErrFailedToAcquireLock } l.fd = fd return nil }
func set(hkey HKEY, path, key, value string) { v := syscall.StringToUTF16(value) with(hkey, path, func(subkey HKEY) { RegSetValueEx.Call( uintptr(subkey), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(key))), 0, uintptr(REG_EXPAND_SZ), uintptr(unsafe.Pointer(&v[0])), uintptr(len(v)*2), ) }) }
func (tw *TabWidget) tcitemFromPage(page *TabPage) *win.TCITEM { imageIndex, _ := tw.imageIndex(page.image) text := syscall.StringToUTF16(page.title) item := &win.TCITEM{ Mask: win.TCIF_IMAGE | win.TCIF_TEXT, IImage: imageIndex, PszText: &text[0], CchTextMax: int32(len(text)), } return item }
func ListToEnvironmentBlock(list *[]string) *uint16 { if list == nil { return nil } size := 1 for _, v := range *list { size += len(syscall.StringToUTF16(v)) } result := make([]uint16, size) tail := 0 for _, v := range *list { uline := syscall.StringToUTF16(v) copy(result[tail:], uline) tail += len(uline) } result[tail] = 0 return &result[0] }
func (p *PrinterInfo) hDevNames() HGLOBAL { driverName := syscall.StringToUTF16(p.driverName) printerName := syscall.StringToUTF16(UTF16PtrToString(p.printerNamePtr())) outputPort := syscall.StringToUTF16(p.outputPort) driverLen := len(driverName) + 1 printerLen := len(printerName) + 1 portLen := len(outputPort) + 1 var dn DEVNAMES hDevNames := GlobalAlloc(GHND, uintptr(unsafe.Sizeof(dn)+(driverLen+printerLen+portLen)*2)) devNames := (*DEVNAMES)(GlobalLock(hDevNames)) defer GlobalUnlock(hDevNames) devNames.WDriverOffset = uint16(unsafe.Sizeof(*devNames) / 2) devNames.WDeviceOffset = devNames.WDriverOffset + uint16(driverLen) devNames.WOutputOffset = devNames.WDeviceOffset + uint16(printerLen) MoveMemory(unsafe.Pointer(uintptr(unsafe.Pointer(devNames))+uintptr(devNames.WDriverOffset)), unsafe.Pointer(&driverName[0]), uintptr(driverLen*2)) MoveMemory(unsafe.Pointer(uintptr(unsafe.Pointer(devNames))+uintptr(devNames.WDeviceOffset)), unsafe.Pointer(&printerName[0]), uintptr(printerLen*2)) MoveMemory(unsafe.Pointer(uintptr(unsafe.Pointer(devNames))+uintptr(devNames.WOutputOffset)), unsafe.Pointer(&outputPort[0]), uintptr(portLen*2)) return hDevNames }