func (self *RegKey) GetValueBinary(valueName string) ([]byte, error) { var valueType uint32 var size uint32 err := wrappers.RegQueryValueEx( self.handle, syscall.StringToUTF16Ptr(valueName), nil, &valueType, nil, &size) if err != nil { return nil, NewWindowsError("RegQueryValueEx", err) } if valueType != wrappers.REG_BINARY { // use the same error code as RegGetValue, although that function is not used here in order to maintain // compatibility with older versions of Windows return nil, NewWindowsError("RegQueryValueEx", wrappers.ERROR_UNSUPPORTED_TYPE) } value := make([]byte, size) err = wrappers.RegQueryValueEx( self.handle, syscall.StringToUTF16Ptr(valueName), nil, nil, &value[0], &size) if err != nil { return nil, NewWindowsError("RegQueryValueEx", err) } return value, nil }
func RegGetString(hKey HKEY, subKey string, value string) string { var bufLen uint32 procRegGetValue.Call( uintptr(hKey), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(value))), uintptr(RRF_RT_REG_SZ), 0, 0, uintptr(unsafe.Pointer(&bufLen))) if bufLen == 0 { return "" } buf := make([]uint16, bufLen) ret, _, _ := procRegGetValue.Call( uintptr(hKey), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(value))), uintptr(RRF_RT_REG_SZ), 0, uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&bufLen))) if ret != ERROR_SUCCESS { return "" } return syscall.UTF16ToString(buf) }
func (lv *ListView) onInsertingListViewItem(index int, item *ListViewItem) (err os.Error) { var lvi LVITEM lvi.Mask = LVIF_TEXT lvi.IItem = index texts := item.Texts() if len(texts) > 0 { lvi.PszText = syscall.StringToUTF16Ptr(texts[0]) } i := SendMessage(lv.hWnd, LVM_INSERTITEM, 0, uintptr(unsafe.Pointer(&lvi))) if int(i) == -1 { return newError("ListView.onInsertingListViewItem: Failed to insert item.") } colCount := lv.columns.Len() for colIndex := 1; colIndex < colCount; colIndex++ { lvi.ISubItem = colIndex if colIndex < len(texts) { lvi.PszText = syscall.StringToUTF16Ptr(texts[colIndex]) } else { lvi.PszText = nil } ret := SendMessage(lv.hWnd, LVM_SETITEM, 0, uintptr(unsafe.Pointer(&lvi))) if ret == 0 { return newError("ListView.onInsertingListViewItem: Failed to set sub item.") } } return }
func RegistryKeyUint32(rootKey *RegistryKey, subKeyPath, valueName string) (value uint32, err error) { var hKey win.HKEY if win.RegOpenKeyEx( rootKey.hKey, syscall.StringToUTF16Ptr(subKeyPath), 0, win.KEY_READ, &hKey) != win.ERROR_SUCCESS { return 0, newError("RegistryKeyUint32: Failed to open subkey.") } defer win.RegCloseKey(hKey) bufSize := uint32(4) if win.ERROR_SUCCESS != win.RegQueryValueEx( hKey, syscall.StringToUTF16Ptr(valueName), nil, nil, (*byte)(unsafe.Pointer(&value)), &bufSize) { return 0, newError("RegQueryValueEx") } return }
func GetOperatingSystem() (string, error) { var h syscall.Handle // Default return value ret := "Unknown Operating System" if err := syscall.RegOpenKeyEx(syscall.HKEY_LOCAL_MACHINE, syscall.StringToUTF16Ptr(`SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\`), 0, syscall.KEY_READ, &h); err != nil { return ret, err } defer syscall.RegCloseKey(h) var buf [1 << 10]uint16 var typ uint32 n := uint32(len(buf) * 2) // api expects array of bytes, not uint16 if err := syscall.RegQueryValueEx(h, syscall.StringToUTF16Ptr("ProductName"), nil, &typ, (*byte)(unsafe.Pointer(&buf[0])), &n); err != nil { return ret, err } ret = syscall.UTF16ToString(buf[:]) return ret, nil }
func IsServiceRunning(serviceName string) (bool, error) { // This function requires fewer access rights than using the above classes and can be used to check if a service // is running without administrator access. scmhandle, err := wrappers.OpenSCManager( nil, syscall.StringToUTF16Ptr(wrappers.SERVICES_ACTIVE_DATABASE), wrappers.SC_MANAGER_CONNECT) if err != nil { return false, NewWindowsError("OpenSCManager", err) } defer wrappers.CloseServiceHandle(scmhandle) handle, err := wrappers.OpenService( scmhandle, syscall.StringToUTF16Ptr(serviceName), wrappers.SERVICE_QUERY_STATUS) if err == wrappers.ERROR_SERVICE_DOES_NOT_EXIST { return false, nil } else if err != nil { return false, NewWindowsError("OpenService", err) } defer wrappers.CloseServiceHandle(handle) var status wrappers.SERVICE_STATUS if err := wrappers.QueryServiceStatus(handle, &status); err != nil { return false, NewWindowsError("QueryServiceStatus", err) } return status.CurrentState == wrappers.SERVICE_RUNNING, nil }
func (self *InternetConnection) OpenHTTPRequest(verb string, objectName string, version string, referer string, acceptTypes []string, flags HTTPRequestFlags) (*HTTPRequest, error) { var versionRaw *uint16 if version != "" { versionRaw = syscall.StringToUTF16Ptr(version) } var refererRaw *uint16 if referer != "" { refererRaw = syscall.StringToUTF16Ptr(referer) } var acceptTypesRaw **uint16 if acceptTypes != nil { acceptTypesPtrs := make([]*uint16, len(acceptTypes), len(acceptTypes)+1) for i := range acceptTypes { acceptTypesPtrs[i] = syscall.StringToUTF16Ptr(acceptTypes[i]) } acceptTypesPtrs = append(acceptTypesPtrs, nil) acceptTypesRaw = &acceptTypesPtrs[0] } handle, err := wrappers.HttpOpenRequest( self.handle, syscall.StringToUTF16Ptr(verb), syscall.StringToUTF16Ptr(objectName), versionRaw, refererRaw, acceptTypesRaw, uint32(flags), 0) if err != nil { return nil, NewWindowsError("HttpOpenRequest", err) } return &HTTPRequest{InternetFile{InternetObject{handle: handle}}}, nil }
func getPath() { var root syscall.Handle error := syscall.RegOpenKeyEx(syscall.HKEY_CURRENT_USER, syscall.StringToUTF16Ptr(`Environment`), 0, syscall.KEY_READ, &root) defer syscall.RegCloseKey(root) var buf [1 << 10]uint16 var typ uint32 n := uint32(len(buf)) // api expects array of bytes, not uint16 if syscall.RegQueryValueEx( root, syscall.StringToUTF16Ptr(`Path`), nil, &typ, (*byte)(unsafe.Pointer(&buf[0])), &n) != nil { } path := syscall.UTF16ToString(buf[:]) fmt.Println(path) if error != nil { fmt.Println(error) return } }
func GetLocalAccountByName(accountName string) (SecurityID, string, SecurityIDType, error) { var neededForSid uint32 var neededForDomain uint32 var use int32 wrappers.LookupAccountName( nil, syscall.StringToUTF16Ptr(accountName), nil, &neededForSid, nil, &neededForDomain, &use) sidBuf := make([]byte, neededForSid) sid := (*wrappers.SID)(unsafe.Pointer(&sidBuf[0])) domainBuf := make([]uint16, neededForDomain) err := wrappers.LookupAccountName( nil, syscall.StringToUTF16Ptr(accountName), sid, &neededForSid, &domainBuf[0], &neededForDomain, &use) if err != nil { return SecurityID{}, "", 0, NewWindowsError("LookupAccountName", err) } return SecurityID{sid}, syscall.UTF16ToString(domainBuf), SecurityIDType(use), 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 main() { getPath() return var root syscall.Handle error := syscall.RegOpenKeyEx(syscall.HKEY_CURRENT_USER, syscall.StringToUTF16Ptr(`Control Panel\Accessibility\HighContrast`), 0, syscall.KEY_READ, &root) defer syscall.RegCloseKey(root) var buf [1 << 10]uint16 var typ uint32 n := uint32(len(buf)) // api expects array of bytes, not uint16 if syscall.RegQueryValueEx( root, syscall.StringToUTF16Ptr(`Previous High Contrast Scheme MUI Value`), nil, &typ, (*byte)(unsafe.Pointer(&buf[0])), &n) != nil { } path := syscall.UTF16ToString(buf[:]) fmt.Println(path) if error != nil { fmt.Println(error) return } }
func LoadVariables(etype EnvType) (map[string]string, error) { var hkey HKEY envMap := make(map[string]string) if etype == 0 { RegOpenKeyEx(HKEY_CURRENT_USER, syscall.StringToUTF16Ptr(`Environment`), 0, KEY_READ, &hkey) } else { RegOpenKeyEx(HKEY_LOCAL_MACHINE, syscall.StringToUTF16Ptr(`SYSTEM\CurrentControlSet\Control\Session Manager\Environment`), 0, KEY_READ, &hkey) } for i := 0; ; i++ { var valueLen uint32 = 256 valueBuffer := make([]uint16, 256) var dataLen uint32 = 0 var dataType uint32 = 0 if ERROR_NO_MORE_ITEMS == RegEnumValue(hkey, uint32(i), &valueBuffer[0], &valueLen, nil, &dataType, nil, &dataLen) { break } dataBuffer := make([]uint16, dataLen/2+2) if ERROR_SUCCESS != RegQueryValueEx(hkey, &valueBuffer[0], nil, &dataType, (*byte)(unsafe.Pointer(&dataBuffer[0])), &dataLen) { return nil, errors.New("ERROR2") } envMap[syscall.UTF16ToString(valueBuffer)] = syscall.UTF16ToString(dataBuffer) if syscall.UTF16ToString(valueBuffer) == "Path" { log.Println("Path = ", syscall.UTF16ToString(dataBuffer)) } } return envMap, nil }
func (self *RegKey) GetValueString(valueName string) (string, error) { var valueType uint32 var size uint32 err := wrappers.RegQueryValueEx( self.handle, syscall.StringToUTF16Ptr(valueName), nil, &valueType, nil, &size) if err != nil { return "", NewWindowsError("RegQueryValueEx", err) } if valueType != wrappers.REG_SZ { // use the same error code as RegGetValue, although that function is not used here in order to maintain // compatibility with older versions of Windows return "", NewWindowsError("RegQueryValueEx", wrappers.ERROR_UNSUPPORTED_TYPE) } buf := make([]uint16, size/2) err = wrappers.RegQueryValueEx( self.handle, syscall.StringToUTF16Ptr(valueName), nil, nil, (*byte)(unsafe.Pointer(&buf[0])), &size) if err != nil { return "", NewWindowsError("RegQueryValueEx", err) } return syscall.UTF16ToString(buf), nil }
func (self *RegKey) GetValueQWORD(valueName string) (uint64, error) { var valueType uint32 var size uint32 err := wrappers.RegQueryValueEx( self.handle, syscall.StringToUTF16Ptr(valueName), nil, &valueType, nil, &size) if err != nil { return 0, NewWindowsError("RegQueryValueEx", err) } if valueType != wrappers.REG_QWORD { // use the same error code as RegGetValue, although that function is not used here in order to maintain // compatibility with older versions of Windows return 0, NewWindowsError("RegQueryValueEx", wrappers.ERROR_UNSUPPORTED_TYPE) } var value uint64 err = wrappers.RegQueryValueEx( self.handle, syscall.StringToUTF16Ptr(valueName), nil, nil, (*byte)(unsafe.Pointer(&value)), &size) if err != nil { return 0, NewWindowsError("RegQueryValueEx", err) } return value, nil }
func OpenEventLog(servername string, sourcename string) HANDLE { ret, _, _ := procOpenEventLog.Call( uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(servername))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(sourcename)))) return HANDLE(ret) }
func WndProc(hwnd HWND, msg uint32, wParam, lParam uintptr) (result uintptr) { switch msg { case WM_CREATE: PlaySound(syscall.StringToUTF16Ptr("hellowin.wav"), HWND(0), SND_FILENAME|SND_ASYNC) //r := PlaySound(syscall.StringToUTF16Ptr("SystemStart"), HWND(0), SND_ALIAS ) return 0 case WM_DESTROY: PostQuitMessage(0) return 0 case WM_PAINT: var ps PAINTSTRUCT hdc := BeginPaint(hwnd, &ps) //fmt.Println(hdc) var rect RECT GetClientRect(hwnd, &rect) //fmt.Println(rect) var params DRAWTEXTPARAMS params.CbSize = uint32(unsafe.Sizeof(params)) DrawTextEx( hdc, syscall.StringToUTF16Ptr("Hello, Window"), -1, &rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER, ¶ms) EndPaint(hwnd, &ps) return 0 } return DefWindowProc(hwnd, msg, wParam, lParam) }
func RegGetRaw(hKey HKEY, subKey string, value string) []byte { var bufLen uint32 var valptr unsafe.Pointer if len(value) > 0 { valptr = unsafe.Pointer(syscall.StringToUTF16Ptr(value)) } procRegGetValue.Call( uintptr(hKey), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), uintptr(valptr), uintptr(RRF_RT_ANY), 0, 0, uintptr(unsafe.Pointer(&bufLen))) if bufLen == 0 { return nil } buf := make([]byte, bufLen) ret, _, _ := procRegGetValue.Call( uintptr(hKey), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), uintptr(valptr), uintptr(RRF_RT_ANY), 0, uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&bufLen))) if ret != ERROR_SUCCESS { return nil } return buf }
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 OnPaint(hwnd HWND, msg uint32, wParam, lParam uintptr) (result uintptr) { szHeading := "Mapping Mode Left Right Top Bottom" szUndLine := "------------ ---- ----- --- ------" var ps PAINTSTRUCT hdc := BeginPaint(hwnd, &ps) SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT)) SetMapMode(hdc, MM_ANISOTROPIC) SetWindowExtEx(hdc, 1, 1, nil) SetViewportExtEx(hdc, cxChar, cyChar, nil) TextOut(hdc, 1, 1, syscall.StringToUTF16Ptr(szHeading), int32(len(szHeading))) TextOut(hdc, 1, 2, syscall.StringToUTF16Ptr(szUndLine), int32(len(szUndLine))) Show(hwnd, hdc, 1, 3, MM_TEXT, "TEXT (pixels)") Show(hwnd, hdc, 1, 4, MM_LOMETRIC, "LOMETRIC (.1 mm)") Show(hwnd, hdc, 1, 5, MM_HIMETRIC, "HIMETRIC (.01 mm)") Show(hwnd, hdc, 1, 6, MM_LOENGLISH, "LOENGLISH (.01 in)") Show(hwnd, hdc, 1, 7, MM_HIENGLISH, "HIENGLISH (.001 in)") Show(hwnd, hdc, 1, 8, MM_TWIPS, "TWIPS (1/1440 in)") EndPaint(hwnd, &ps) return 0 }
// RegGetString XXX func RegGetString(hKey uint32, subKey string, value string) (string, uintptr, error) { var bufLen uint32 ret, _, err := RegGetValue.Call( uintptr(hKey), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(value))), uintptr(RRF_RT_REG_SZ), 0, 0, uintptr(unsafe.Pointer(&bufLen))) if ret != ERROR_SUCCESS { return "", ret, err } if bufLen == 0 { return "", ret, nil } buf := make([]uint16, bufLen) ret, _, err = RegGetValue.Call( uintptr(hKey), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(value))), uintptr(RRF_RT_REG_SZ), 0, uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&bufLen))) if ret != ERROR_SUCCESS { return "", ret, err } return syscall.UTF16ToString(buf), ret, nil }
// Login and load user profile. Also, set finalizer on s to logout() above. func (s *LoginInfo) Prepare() error { var err error if s.Username == "" { return nil } ec := tools.ErrorContext("LoginInfo.Prepare") s.HUser, err = win32.LogonUser( syscall.StringToUTF16Ptr(s.Username), syscall.StringToUTF16Ptr("."), syscall.StringToUTF16Ptr(s.Password), win32.LOGON32_LOGON_INTERACTIVE, win32.LOGON32_PROVIDER_DEFAULT) if err != nil { return ec.NewError(err) } s.HProfile, err = loadProfile(s.HUser, s.Username) if err != nil { syscall.CloseHandle(s.HUser) s.HUser = syscall.InvalidHandle return ec.NewError(err) } runtime.SetFinalizer(s, logout) return nil }
// See http://blog.natefinch.com/2012/11/go-win-stuff.html // // This is used by workstationVMwareRoot in order to read some registry data. func readRegString(hive syscall.Handle, subKeyPath, valueName string) (value string, err error) { var h syscall.Handle err = syscall.RegOpenKeyEx(hive, syscall.StringToUTF16Ptr(subKeyPath), 0, syscall.KEY_READ, &h) if err != nil { return } defer syscall.RegCloseKey(h) var typ uint32 var bufSize uint32 err = syscall.RegQueryValueEx( h, syscall.StringToUTF16Ptr(valueName), nil, &typ, nil, &bufSize) if err != nil { return } data := make([]uint16, bufSize/2+1) err = syscall.RegQueryValueEx( h, syscall.StringToUTF16Ptr(valueName), nil, &typ, (*byte)(unsafe.Pointer(&data[0])), &bufSize) if err != nil { return } return syscall.UTF16ToString(data), nil }
func FindWindow(className string, windowName string) unsafe.Pointer { lpClassName := syscall.StringToUTF16Ptr(className) lpWindowName := syscall.StringToUTF16Ptr(windowName) ret, _, _ := procFindWindow.Call(uintptr(unsafe.Pointer(lpClassName)), uintptr(unsafe.Pointer(lpWindowName))) return unsafe.Pointer(ret) }
func RegDeleteKeyValue(hKey HKEY, subKey string, valueName string) (errno int) { ret, _, _ := procRegDeleteKeyValue.Call( uintptr(hKey), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(valueName)))) return int(ret) }
func MessageBox(caption, text string, style uintptr) (result int) { ret, _, _ := syscall.Syscall6(uintptr(messageBox), 6, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(caption))), style, 0, 0) return int(ret) }
func CreateContesterDesktop() (winsta win32.Hwinsta, desk win32.Hdesk, name string, err error) { var origWinsta win32.Hwinsta if origWinsta, err = win32.GetProcessWindowStation(); err != nil { return } runtime.LockOSThread() defer runtime.UnlockOSThread() var origDesktop win32.Hdesk if origDesktop, err = win32.GetThreadDesktop(win32.GetCurrentThreadId()); err != nil { return } if winsta, err = win32.CreateWindowStation( syscall.StringToUTF16Ptr(threadIdName("w")), 0, win32.MAXIMUM_ALLOWED, win32.MakeInheritSa()); err != nil { return } if err = win32.SetProcessWindowStation(winsta); err != nil { win32.CloseWindowStation(winsta) return } var winstaName string if winstaName, err = win32.GetUserObjectName(syscall.Handle(winsta)); err == nil { shortName := threadIdName("c") desk, err = win32.CreateDesktop( syscall.StringToUTF16Ptr(shortName), nil, 0, 0, syscall.GENERIC_ALL, win32.MakeInheritSa()) if err == nil { name = winstaName + "\\" + shortName } } win32.SetProcessWindowStation(origWinsta) win32.SetThreadDesktop(origDesktop) if err != nil { return } everyone, err := syscall.StringToSid("S-1-1-0") if err == nil { if err = win32.AddAceToWindowStation(winsta, everyone); err != nil { log.Error(err) } if err = win32.AddAceToDesktop(desk, everyone); err != nil { log.Error(err) } } else { err = os.NewSyscallError("StringToSid", err) } return }
func MessageBox(hwnd HWND, title, caption string, flags uint) int { ret, _, _ := procMessageBox.Call( uintptr(hwnd), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(caption))), uintptr(flags)) return int(ret) }
func InstallProduct(packagePath string, commandLine string) error { err := wrappers.MsiInstallProduct( syscall.StringToUTF16Ptr(packagePath), syscall.StringToUTF16Ptr(commandLine)) if err != nil { return NewWindowsError("MsiInstallProduct", err) } return nil }
func SetEnvironment(name string, value string) error { err := wrappers.SetEnvironmentVariable( syscall.StringToUTF16Ptr(name), syscall.StringToUTF16Ptr(value)) if err != nil { return NewWindowsError("SetEnvironmentVariable", err) } return nil }
func MsgBox(owner RootWidget, title, message string, style MsgBoxStyle) DialogCommandId { var ownerHWnd HWND if owner != nil { ownerHWnd = owner.Handle() } return DialogCommandId(MessageBox(ownerHWnd, syscall.StringToUTF16Ptr(message), syscall.StringToUTF16Ptr(title), uint(style))) }