func initCache() { if iphlpapi, err := syscall.LoadDLL("Iphlpapi.dll"); err == nil { if p, err := iphlpapi.FindProc("NotifyAddrChange"); err == nil { notifyAddrChange = p } } if advapi32, err := syscall.LoadDLL("Advapi32.dll"); err == nil { if p, err := advapi32.FindProc("RegNotifyChangeKeyValue"); err == nil { regNotifyChangeKeyValue = p } } cacheLock.Lock() defer cacheLock.Unlock() notifyCh = make(chan error) if regNotifyChangeKeyValue != nil && notifyAddrChange != nil { useLookup = true go notifyIpChange(notifyCh) go notifyRegChange(registry.CURRENT_USER, PathInternetSettings, notifyCh) } cache = make(map[string]*url.URL) go func() { for _ = range notifyCh { invalidateCache() } }() }
func getAllWindowsLocaleFrom(sysCall string) (string, error) { dll, err := syscall.LoadDLL("kernel32") if err != nil { return "", errors.New("Could not find kernel32 dll") } proc, err := dll.FindProc(sysCall) if err != nil { return "", err } locale, _, dllError := proc.Call() if locale == 0 { return "", errors.New(COULD_NOT_DETECT_PACKAGE_ERROR_MESSAGE + ":\n" + dllError.Error()) } proc, err = dll.FindProc("GetLocaleInfoW") if err != nil { return "", err } langBuf := make([]uint16, LOCALE_SISO_NAME_MAX_LENGTH) r, _, dllError := proc.Call(locale, uintptr(LOCALE_SISO639LANGNAME), uintptr(unsafe.Pointer(&langBuf[0])), uintptr(LOCALE_SISO_NAME_MAX_LENGTH)) if r == 0 { err = errors.New(COULD_NOT_DETECT_PACKAGE_ERROR_MESSAGE + ":\n" + dllError.Error()) return "", err } countryBuf := make([]uint16, LOCALE_SISO_NAME_MAX_LENGTH) r, _, dllError = proc.Call(locale, uintptr(LOCALE_SISO3166CTRYNAME), uintptr(unsafe.Pointer(&countryBuf[0])), uintptr(LOCALE_SISO_NAME_MAX_LENGTH)) if r == 0 { err = errors.New(COULD_NOT_DETECT_PACKAGE_ERROR_MESSAGE + ":\n" + dllError.Error()) return "", err } return syscall.UTF16ToString(langBuf) + "-" + syscall.UTF16ToString(countryBuf), nil }
func terminateProc(proc string) error { dll, err := syscall.LoadDLL("kernel32.dll") if err != nil { return err } defer dll.Release() pid := procs[proc].cmd.Process.Pid f, err := dll.FindProc("SetConsoleCtrlHandler") if err != nil { return err } r1, _, err := f.Call(0, 1) if r1 == 0 { return err } f, err = dll.FindProc("GenerateConsoleCtrlEvent") if err != nil { return err } r1, _, err = f.Call(syscall.CTRL_BREAK_EVENT, uintptr(pid)) if r1 == 0 { return err } return nil }
// SaferDLLLoading sets DLL load path to be safer. func SaferDLLLoading() error { kernel32, err := syscall.LoadDLL("kernel32.dll") if err != nil { return err } procSetDllDirectoryW, err := kernel32.FindProc("SetDllDirectoryW") if err != nil { return err } var zero uint16 r1, _, e1 := syscall.Syscall(procSetDllDirectoryW.Addr(), 1, uintptr(unsafe.Pointer(&zero)), 0, 0) procSetDefaultDllDirectories, err := kernel32.FindProc("SetDefaultDllDirectories") if err == nil && procSetDefaultDllDirectories.Addr() != 0 { r1, _, e1 = syscall.Syscall(procSetDefaultDllDirectories.Addr(), 1, loadLibrarySearchSystem32, 0, 0) if r1 == 0 { return e1 } } else { return errors.New("SetDefaultDllDirectories not found - please install KB2533623 for safer DLL loading") } return nil }
// LoadDLL loads a DLL file into memory. It panics if the file is not found. func LoadDLL(name string) DLL { lib, err := syscall.LoadDLL(name) if err != nil { panic(err) } return DLL{lib} }
func GetDLL(t *testing.T, name string) *DLL { d, e := syscall.LoadDLL(name) if e != nil { t.Fatal(e) } return &DLL{DLL: d, t: t} }
func GetFilesystemRoots() ([]string, error) { kernel32, err := syscall.LoadDLL("kernel32.dll") if err != nil { return nil, err } getLogicalDriveStringsHandle, err := kernel32.FindProc("GetLogicalDriveStringsA") if err != nil { return nil, err } buffer := [1024]byte{} bufferSize := uint32(len(buffer)) hr, _, _ := getLogicalDriveStringsHandle.Call(uintptr(unsafe.Pointer(&bufferSize)), uintptr(unsafe.Pointer(&buffer))) if hr == 0 { return nil, fmt.Errorf("Syscall failed") } var drives []string parts := bytes.Split(buffer[:], []byte{0}) for _, part := range parts { if len(part) == 0 { break } drives = append(drives, string(part)) } return drives, nil }
func getWindowsLocale() (locale string, err error) { dll, err := syscall.LoadDLL("kernel32") if err != nil { return "", errors.New("Could not find kernel32 dll") } proc, err := dll.FindProc("GetVersion") if err != nil { return "", err } v, _, _ := proc.Call() windowsVersion := byte(v) isVistaOrGreater := (windowsVersion >= 6) if isVistaOrGreater { locale, err = getWindowsLocaleFrom("GetUserDefaultLocaleName") if err != nil { locale, err = getWindowsLocaleFrom("GetSystemDefaultLocaleName") } } else if !isVistaOrGreater { locale, err = getAllWindowsLocaleFrom("GetUserDefaultLCID") if err != nil { locale, err = getAllWindowsLocaleFrom("GetSystemDefaultLCID") } } else { panic(v) } return }
func loadDLL(name string) *cutoDLL { dll, err := syscall.LoadDLL(name) if err != nil { panic(err) } cutoDll := new(cutoDLL) cutoDll.dll = dll return cutoDll }
/* 控制台彩色输出,1深蓝,2深绿,3深青,10绿色,11青色,12红色 The console color output,1 deep blue,2 deep green,3 deep cyan,10 green,11 cyan,12 red */ func ColorEdit(i int) { kernel32, _ := syscall.LoadDLL("kernel32.dll") //调用dll,call dll defer kernel32.Release() //延迟释放,delay release /* 找到dll中的方法,并返回此方法 find the method in the dll and return the method */ proc, _ := kernel32.FindProc("SetConsoleTextAttribute") proc.Call(uintptr(syscall.Stdout), uintptr(i)) //执行此方法,carry out the method }
// LoadDLL loads specified DLL using syscall.LoadDLL (i.e. LoadLibraryW) // and returns an error if any occurred during the syscall.LoadDLL // Calling LoadDLL twice or on already loaded DLL has no effect. func (w *W32s) LoadDLL(name string) error { if _, ok := w.dlls[name]; !ok { dll, err := syscall.LoadDLL(name) if err != nil { return err } w.dlls[name] = newDll(dll, w.encoding) } return nil }
func load_qtdrv_proc() (uintptr, error) { lib, err := syscall.LoadDLL("qtdrv.ui.dll") if err != nil { return 0, err } proc, err := lib.FindProc("qtdrv") if err != nil { return 0, err } return proc.Addr(), nil }
func init() { kernel32 := syscall.NewLazyDLL("kernel32.dll") procGetStdHandle = kernel32.NewProc("GetStdHandle") hStdout, _, _ = procGetStdHandle.Call(uintptr(std_output_handle)) initScreenInfo = getConsoleScreenBufferInfo(hStdout) syscall.LoadDLL("") }
func sendCtrlBreak(pid int) { d, e := syscall.LoadDLL("kernel32.dll") if e != nil { return } p, e := d.FindProc("GenerateConsoleCtrlEvent") if e != nil { return } p.Call(syscall.CTRL_BREAK_EVENT, uintptr(pid)) }
func loadProc() *syscall.Proc { dll, err := syscall.LoadDLL("kernel32.dll") if err != nil { panic(err) } proc, err := dll.FindProc("GetModuleFileNameW") if err != nil { panic(err) } return proc }
func init() { dll, err := syscall.LoadDLL("vpsc.dll") if err != nil { log.Println("could not load vpsc.dll") return } removeOverlaps, err = dll.FindProc("remove_overlaps") if err != nil { log.Println("remove_overlaps missing from vpsc.dll") } }
func GetString() (string, error) { dll, err := syscall.LoadDLL("kernel32.dll") if err != nil { return "", errors.New(fmt.Sprintf("Error loading kernel32.dll: %v", err)) } p, err := dll.FindProc("GetVersion") if err != nil { return "", errors.New(fmt.Sprintf("Error finding GetVersion procedure: %v", err)) } // The error is always non-nil v, _, _ := p.Call() return fmt.Sprintf("%d.%d.%d", byte(v), byte(v>>8), uint16(v>>16)), nil }
func terminateProc(proc string) error { dll, err := syscall.LoadDLL("kernel32.dll") if err != nil { return err } f, err := dll.FindProc("GenerateConsoleCtrlEvent") if err != nil { return err } pid := procs[proc].cmd.Process.Pid f.Call(syscall.CTRL_C_EVENT, uintptr(pid)) return nil }
func sendCtrlBreak(t *testing.T, pid int) { d, e := syscall.LoadDLL("kernel32.dll") if e != nil { t.Fatalf("LoadDLL: %v\n", e) } p, e := d.FindProc("GenerateConsoleCtrlEvent") if e != nil { t.Fatalf("FindProc: %v\n", e) } r, _, e := p.Call(syscall.CTRL_BREAK_EVENT, uintptr(pid)) if r == 0 { t.Fatalf("GenerateConsoleCtrlEvent: %v\n", e) } }
func sendCtrlBreak(t *testing.T) { d, e := syscall.LoadDLL("kernel32.dll") if e != nil { t.Fatalf("LoadDLL: %v\n", e) } p, e := d.FindProc("GenerateConsoleCtrlEvent") if e != nil { t.Fatalf("FindProc: %v\n", e) } r, _, e := p.Call(0, 0) if r == 0 { t.Fatalf("GenerateConsoleCtrlEvent: %v\n", e) } }
// danke windows für die extra wurst func sendInterruptSignal(p *os.Process) error { d, e := syscall.LoadDLL("kernel32.dll") if e != nil { return e } proc, e := d.FindProc("GenerateConsoleCtrlEvent") if e != nil { return e } r, _, e := proc.Call(syscall.CTRL_BREAK_EVENT, uintptr(p.Pid)) if r == 0 { return e } return nil }
func loadDLL(name string) (dll *syscall.DLL, err error) { if dlls == nil { dlls = make(map[string]*syscall.DLL) } dll, exists := dlls[name] if !exists { dll, err = syscall.LoadDLL(name) if err != nil { return } dlls[name] = dll } return }
func load_dll() { dll, er := syscall.LoadDLL(DllName) if er != nil { println(er.Error()) println("WARNING: Consensus verificatrion disabled") return } bitcoinconsensus_verify_script, er = dll.FindProc(ProcName) if er != nil { println(er.Error()) println("WARNING: Consensus verificatrion disabled") return } fmt.Println("Using", DllName, "to ensure consensus rules") use_consensus_lib = true }
// loadAndFindFromDll finds a procedure in the given DLL. Note we do NOT do lazy loading as // go is particularly unfriendly in the case of a mismatch. By that - it panics // if a function can't be found. By explicitly loading, we can control error // handling gracefully without the daemon terminating. func loadAndFindFromDll(dllName, procedure string) (dll *syscall.DLL, proc *syscall.Proc, err error) { dll, err = syscall.LoadDLL(dllName) if err != nil { err = fmt.Errorf("Failed to load %s - error %s", dllName, err) logrus.Error(err) return } proc, err = dll.FindProc(procedure) if err != nil { err = fmt.Errorf("Failed to find %s in %s", procedure, dllName) logrus.Error(err) return } return }
// SelfPath returns the path of the executable for the currently running // process. func SelfPath() (string, error) { kernel32, err := syscall.LoadDLL("kernel32.dll") if err != nil { return "", err } sysproc, err := kernel32.FindProc("GetModuleFileNameW") if err != nil { return "", err } b := make([]uint16, syscall.MAX_PATH) r, _, err := sysproc.Call(0, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b))) n := uint32(r) if n == 0 { return "", err } return string(utf16.Decode(b[0:n])), nil }
func interruptProcess(t *testing.T) { dll, err := syscall.LoadDLL("kernel32.dll") if err != nil { t.Fatalf("LoadDLL(\"kernel32.dll\") err: %s", err) } p, err := dll.FindProc("GenerateConsoleCtrlEvent") if err != nil { t.Fatalf("FindProc(\"GenerateConsoleCtrlEvent\") err: %s", err) } // Send the CTRL_BREAK_EVENT to a console process group that shares // the console associated with the calling process. // https://msdn.microsoft.com/en-us/library/windows/desktop/ms683155(v=vs.85).aspx pid := os.Getpid() r1, _, err := p.Call(syscall.CTRL_BREAK_EVENT, uintptr(pid)) if r1 == 0 { t.Fatalf("Call(CTRL_BREAK_EVENT, %d) err: %s", pid, err) } }
func getAllWindowsLocaleFrom(sysCall string) (string, error) { dll, err := syscall.LoadDLL("kernel32") if err != nil { return "", errors.New("Could not find kernel32 dll") } proc, err := dll.FindProc(sysCall) if err != nil { return "", err } locale, _, dllError := proc.Call() if locale == 0 { return "", errors.New(COULD_NOT_DETECT_PACKAGE_ERROR_MESSAGE + ":\n" + dllError.Error()) } return SUPPORTED_LOCALES[locale], nil }
func registerETWProvider() error { mu.Lock() defer mu.Unlock() if refCount == 0 { var err error if win32Lib, err = syscall.LoadDLL("Advapi32.dll"); err != nil { return err } if err = callEventRegister(); err != nil { win32Lib.Release() win32Lib = nil return err } } refCount++ return nil }
func setSizes() { kernel32, err := syscall.LoadDLL("kernel32.dll") if nil != err { abort("loadLibrary", err) } defer kernel32.Release() set, err := kernel32.FindProc("SetSystemFileCacheSize") if nil != err { abort("GetProcAddress", err) } var lpFlags uint32 lpFlags = FILE_CACHE_MAX_HARD_ENABLE max := *MAX * uint64(1000) * uint64(1000) min := *MIN * uint64(1000) * uint64(1000) res, _, err := set.Call(uintptr(min), uintptr(max), uintptr(lpFlags)) if res == 0 { abort("SetSystemFileCacheSize", err) } }
func init() { if membind_use_wrapper { return } dll, er := syscall.LoadDLL("kernel32.dll") if er != nil { return } funcGlobalAlloc, _ = dll.FindProc("GlobalAlloc") funcGlobalFree, _ = dll.FindProc("GlobalFree") if funcGlobalAlloc == nil || funcGlobalFree == nil { return } println("Using kernel32.dll for qdb memory bindings") _heap_alloc = win_HeapAlloc _heap_free = win_HeapFree _heap_store = win_AllocPtr membind_use_wrapper = true }