func HideConsole() { getConsoleWindow := syscall.NewLazyDLL("kernel32.dll").NewProc("GetConsoleWindow") showWindow := syscall.NewLazyDLL("user32.dll").NewProc("ShowWindow") if getConsoleWindow.Find() == nil && showWindow.Find() == nil { hwnd, _, _ := getConsoleWindow.Call() if hwnd != 0 { showWindow.Call(hwnd, 0) } } }
// ChangeConsoleVisibility Change console visibility func ChangeConsoleVisibility(visibility bool) { var getConsoleWindows = syscall.NewLazyDLL("kernel32.dll").NewProc("GetConsoleWindow") var showWindow = syscall.NewLazyDLL("user32.dll").NewProc("ShowWindow") if hwnd, _, _ := showWindow.Call(); hwnd != 0 { if visibility { sw.Call(hwnd, syscall.SW_RESTORE) } else { sw.Call(hwnd, syscall.SW_HIDE) } } }
func getch() byte { modkernel32 := syscall.NewLazyDLL("kernel32.dll") procReadConsole := modkernel32.NewProc("ReadConsoleW") procGetConsoleMode := modkernel32.NewProc("GetConsoleMode") procSetConsoleMode := modkernel32.NewProc("SetConsoleMode") var mode uint32 pMode := &mode procGetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(unsafe.Pointer(pMode))) var echoMode, lineMode uint32 echoMode = 4 lineMode = 2 var newMode uint32 newMode = mode ^ (echoMode | lineMode) procSetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(newMode)) line := make([]uint16, 1) pLine := &line[0] var n uint16 procReadConsole.Call(uintptr(syscall.Stdin), uintptr(unsafe.Pointer(pLine)), uintptr(len(line)), uintptr(unsafe.Pointer(&n))) // For some reason n returned seems to big by 2 (Null terminated maybe?) if n > 2 { n -= 2 } b := []byte(string(utf16.Decode(line[:n]))) procSetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(mode)) return b[0] }
func CloseTCPEntry(row *MIB_TCPROW2) error { row.dwState = 12 if err, _, _ := syscall.NewLazyDLL("Iphlpapi.dll").NewProc("SetTcpEntry").Call(uintptr(unsafe.Pointer(row))); err != 0 { return syscall.Errno(err) } return nil }
func getch() byte { modkernel32 := syscall.NewLazyDLL("kernel32.dll") procReadConsole := modkernel32.NewProc("ReadConsoleW") procGetConsoleMode := modkernel32.NewProc("GetConsoleMode") procSetConsoleMode := modkernel32.NewProc("SetConsoleMode") var mode uint32 pMode := &mode procGetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(unsafe.Pointer(pMode))) var echoMode, lineMode uint32 echoMode = 4 lineMode = 2 var newMode uint32 newMode = mode ^ (echoMode | lineMode) procSetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(newMode)) line := make([]uint16, 1) pLine := &line[0] var n uint16 procReadConsole.Call(uintptr(syscall.Stdin), uintptr(unsafe.Pointer(pLine)), uintptr(len(line)), uintptr(unsafe.Pointer(&n))) b := []byte(string(utf16.Decode(line))) procSetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(mode)) if len(b) > 0 { return b[0] } else { return 13 } }
func getch() (byte, error) { modkernel32 := syscall.NewLazyDLL("kernel32.dll") procReadConsole := modkernel32.NewProc("ReadConsoleW") procGetConsoleMode := modkernel32.NewProc("GetConsoleMode") procSetConsoleMode := modkernel32.NewProc("SetConsoleMode") var mode uint32 pMode := &mode procGetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(unsafe.Pointer(pMode))) var echoMode, lineMode uint32 echoMode = 4 lineMode = 2 var newMode uint32 newMode = mode &^ (echoMode | lineMode) procSetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(newMode)) defer procSetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(mode)) line := make([]uint16, 1) pLine := &line[0] var n uint16 procReadConsole.Call(uintptr(syscall.Stdin), uintptr(unsafe.Pointer(pLine)), uintptr(len(line)), uintptr(unsafe.Pointer(&n))) b := []byte(string(utf16.Decode(line))) // Not sure how this could happen, but it did for someone if len(b) > 0 { return b[0], nil } else { return 13, errors.New("Read error") } }
/* Declare imported function so that we can actually use it. */ func main() { addbasic := syscall.NewLazyDLL("add_basic.dll") procAdd := addbasic.NewProc("Add") val, _, _ := procAdd.Call(6, 23) fmt.Println(val) }
func isRunning() bool { kernel32 := syscall.NewLazyDLL("kernel32.dll") CreateToolhelp32Snapshot := kernel32.NewProc("CreateToolhelp32Snapshot") pHandle, _, _ := CreateToolhelp32Snapshot.Call(uintptr(0x2), uintptr(0x0)) if int(pHandle) == -1 { return false } Process32Next := kernel32.NewProc("Process32Next") run := false for { var proc PROCESSENTRY32 proc.dwSize = ulong(unsafe.Sizeof(proc)) if rt, _, _ := Process32Next.Call(uintptr(pHandle), uintptr(unsafe.Pointer(&proc))); int(rt) == 1 { pName := string(proc.szExeFile[0:]) if compareProcess != "" && pName[0:len(compareProcess)] == compareProcess { run = true } //fmt.Println("ProcessName : ",pName+"sssschrome.exe","j"); //fmt.Println("ProcessID : "+strconv.Itoa(int(proc.th32ProcessID))); } else { break } } CloseHandle := kernel32.NewProc("CloseHandle") _, _, _ = CloseHandle.Call(pHandle) return run }
func TestAtomicMmap(t *testing.T) { // Test that atomic operations work on "external" memory. Previously they crashed (#16206). // Also do a sanity correctness check: under race detector atomic operations // are implemented inside of race runtime. kernel32 := syscall.NewLazyDLL("kernel32.dll") VirtualAlloc := kernel32.NewProc("VirtualAlloc") VirtualFree := kernel32.NewProc("VirtualFree") const ( MEM_COMMIT = 0x00001000 MEM_RESERVE = 0x00002000 MEM_RELEASE = 0x8000 PAGE_READWRITE = 0x04 ) mem, _, err := syscall.Syscall6(VirtualAlloc.Addr(), 4, 0, 1<<20, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE, 0, 0) if err != 0 { t.Fatalf("VirtualAlloc failed: %v", err) } defer syscall.Syscall(VirtualFree.Addr(), 3, mem, 1<<20, MEM_RELEASE) a := (*uint64)(unsafe.Pointer(mem)) if *a != 0 { t.Fatalf("bad atomic value: %v, want 0", *a) } atomic.AddUint64(a, 1) if *a != 1 { t.Fatalf("bad atomic value: %v, want 1", *a) } atomic.AddUint64(a, 1) if *a != 2 { t.Fatalf("bad atomic value: %v, want 2", *a) } }
// Hide console on windows without removing it unlike -H windowsgui. func HideConsole(hide bool) { var k32 = syscall.NewLazyDLL("kernel32.dll") var cw = k32.NewProc("GetConsoleWindow") var u32 = syscall.NewLazyDLL("user32.dll") var sw = u32.NewProc("ShowWindow") hwnd, _, _ := cw.Call() if hwnd == 0 { return } if hide { var SW_HIDE uintptr = 0 sw.Call(hwnd, SW_HIDE) } else { var SW_RESTORE uintptr = 9 sw.Call(hwnd, SW_RESTORE) } }
func init() { hapi := syscall.NewLazyDLL("Wlanapi.dll") hWlanOpenHandle = hapi.NewProc("WlanOpenHandle") hWlanCloseHandle = hapi.NewProc("WlanCloseHandle") hWlanEnumInterfaces = hapi.NewProc("WlanEnumInterfaces") hWlanGetAvailableNetworkList = hapi.NewProc("WlanGetAvailableNetworkList") hWlanGetNetworkBssList = hapi.NewProc("WlanGetNetworkBssList") hWlanFreeMemory = hapi.NewProc("WlanFreeMemory") }
func initialize() { secur32dll := syscall.NewLazyDLL("secur32.dll") initSecurityInterface := secur32dll.NewProc("InitSecurityInterfaceW") ptr, _, _ := initSecurityInterface.Call() sec_fn = (*SecurityFunctionTable)(unsafe.Pointer(ptr)) initialized = true }
// setColorTemp changes the device gamma curve colors to reflect the specified color temperature. func setColorTemp(gammar, gammag, gammab float64) { user32 := syscall.NewLazyDLL("User32.dll") gdi32 := syscall.NewLazyDLL("Gdi32.dll") procGetDC := user32.NewProc("GetDC") procSetDeviceGammaRamp := gdi32.NewProc("SetDeviceGammaRamp") var gamma [3][256]uint16 for i := 0; i < 256; i++ { g := 65535.0 * float64(i) / float64(256) gamma[0][i] = uint16(g * gammar) gamma[1][i] = uint16(g * gammag) gamma[2][i] = uint16(g * gammab) } hdc, _, _ := procGetDC.Call(uintptr(0)) procSetDeviceGammaRamp.Call(hdc, uintptr(unsafe.Pointer(&gamma))) }
/**call exe*/ func callEXEByShell(path string) { var hand uintptr = uintptr(0) var operator uintptr = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("open"))) var fpath uintptr = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))) var param uintptr = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("/S"))) var dirpath uintptr = uintptr(0) var ncmd uintptr = uintptr(1) shell32 := syscall.NewLazyDLL("shell32.dll") ShellExecuteW := shell32.NewProc("ShellExecuteW") _, _, _ = ShellExecuteW.Call(hand, operator, fpath, param, dirpath, ncmd) }
func MessageBox(title, text string) int { var mod = syscall.NewLazyDLL("user32.dll") var proc = mod.NewProc("MessageBoxW") var MB_YESNO = 0x00000004 ret, _, _ := proc.Call(0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))), uintptr(MB_YESNO)) return int(ret) }
func init() { kernel32 := syscall.NewLazyDLL("kernel32.dll") procGetStdHandle = kernel32.NewProc("GetStdHandle") hStdout, _, _ = procGetStdHandle.Call(uintptr(std_output_handle)) initScreenInfo = getConsoleScreenBufferInfo(hStdout) syscall.LoadDLL("") }
func (c Console) ColorPrint(s string, i int) { kernel32 := syscall.NewLazyDLL("kernel32.dll") proc := kernel32.NewProc("SetConsoleTextAttribute") handle, _, _ := proc.Call(uintptr(syscall.Stdout), uintptr(i)) //12 Red light fmt.Print(s) handle, _, _ = proc.Call(uintptr(syscall.Stdout), uintptr(7)) //White dark CloseHandle := kernel32.NewProc("CloseHandle") CloseHandle.Call(handle) }
func hideFile(path string) error { kernel32 := syscall.NewLazyDLL("kernel32.dll") setFileAttributes := kernel32.NewProc("SetFileAttributesW") r1, _, err := setFileAttributes.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))), 2) if r1 == 0 { return err } else { return nil } }
func execdir() string { var ( kernel32 = syscall.NewLazyDLL("kernel32") procGetModuleFileName = kernel32.NewProc("GetModuleFileNameW") ) var wpath [syscall.MAX_PATH]uint16 r1, _, err := procGetModuleFileName.Call(0, uintptr(unsafe.Pointer(&wpath[0])), uintptr(len(wpath))) if r1 == 0 { log.Fatal(err) } return syscall.UTF16ToString(wpath[:]) }
func NewKernel() *Kernel { k := &Kernel{} kernel32 := syscall.NewLazyDLL("kernel32.dll") v := reflect.ValueOf(k).Elem() t := v.Type() for i := 0; i < t.NumField(); i++ { name := t.Field(i).Name f := kernel32.NewProc(name) v.Field(i).Set(reflect.ValueOf(k.Wrap(f))) } return k }
func main() { var mod = syscall.NewLazyDLL("user32.dll") var proc = mod.NewProc("MessageBoxW") var MB_YESNOCANCEL = 0x00000003 ret, _, _ := proc.Call(0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("Done Title"))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("This test is Done."))), uintptr(MB_YESNOCANCEL)) fmt.Printf("Return: %d\n", ret) }
func signalDaemonDump(pid int) { modkernel32 := syscall.NewLazyDLL("kernel32.dll") procOpenEvent := modkernel32.NewProc("OpenEventW") procPulseEvent := modkernel32.NewProc("PulseEvent") ev := "Global\\docker-daemon-" + strconv.Itoa(pid) h2, _ := openEvent(0x0002, false, ev, procOpenEvent) if h2 == 0 { return } pulseEvent(h2, procPulseEvent) }
// netstat -ano | findstr 202.89.233.104 func getTCPTable() *MIB_TCPTABLE2 { getTCPTable2 := syscall.NewLazyDLL("Iphlpapi.dll").NewProc("GetTcpTable2") var n uint32 if err, _, _ := getTCPTable2.Call(uintptr(unsafe.Pointer(&MIB_TCPTABLE2{})), uintptr(unsafe.Pointer(&n)), 1); syscall.Errno(err) != syscall.ERROR_INSUFFICIENT_BUFFER { common.Error("Error calling GetTcpTable2: %v", syscall.Errno(err)) } b := make([]byte, n) if err, _, _ := getTCPTable2.Call(uintptr(unsafe.Pointer(&b[0])), uintptr(unsafe.Pointer(&n)), 1); err != 0 { common.Error("Error calling GetTcpTable2: %v", syscall.Errno(err)) } table := newTCPTable(NewClassReader(b)) return table }
func CallCFunc(input_path, output_path string) int { dll32 := syscall.NewLazyDLL("dump_analyze_dll.dll") println("call dll:", dll32.Name) g := dll32.NewProc("test1") func2 := dll32.NewProc("analyze") //cstr := C.CString("string from golang") //defer C.free(unsafe.Pointer(cstr)) g.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("string from golang")))) //func2.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("L:\\workspace\\dump_analyze\\Debug\\BAK"))), // uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("L:\\workspace\\result")))) re, _, _ := func2.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(input_path))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(output_path)))) return int(re) }
func main() { var mod = syscall.NewLazyDLL("kernel32.dll") var proc = mod.NewProc("IsProcessorFeaturePresent") r1, r2, _ := proc.Call(uintptr(PF_VIRT_FIRMWARE_ENABLED)) if r1 != 0 { fmt.Printf("Err: %s", syscall.Errno(r1)) } resultPtr := unsafe.Pointer(r2) hasVTX := false if *resultPtr != 0 { hasVTX = true } fmt.Printf("VTX Enabled: %v", hasVTX) }
func osMemStatus() (Physical, Virtual MemoryStats) { modkernel32 := syscall.NewLazyDLL("kernel32.dll") GlobalMemoryStatusEx := modkernel32.NewProc("GlobalMemoryStatusEx") var stats memoryStatusEx stats.dwLength = uint32(unsafe.Sizeof(stats)) pStats := &stats GlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(pStats))) Physical.Free = stats.ullAvailPhys Physical.Total = stats.ullTotalPhys Physical.Used = Physical.Total - Physical.Free Virtual.Free = stats.ullAvailVirtual Virtual.Total = stats.ullTotalVirtual Virtual.Used = Virtual.Total - Virtual.Free return }
// getFileSystemType obtains the type of a file system through GetVolumeInformation // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364993(v=vs.85).aspx func getFileSystemType(drive string) (fsType string, hr error) { var ( modkernel32 = syscall.NewLazyDLL("kernel32.dll") procGetVolumeInformation = modkernel32.NewProc("GetVolumeInformationW") buf = make([]uint16, 255) size = syscall.MAX_PATH + 1 ) if len(drive) != 1 { hr = errors.New("getFileSystemType must be called with a drive letter") return } drive += `:\` n := uintptr(unsafe.Pointer(nil)) r0, _, _ := syscall.Syscall9(procGetVolumeInformation.Addr(), 8, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(drive))), n, n, n, n, n, uintptr(unsafe.Pointer(&buf[0])), uintptr(size), 0) if int32(r0) < 0 { hr = syscall.Errno(win32FromHresult(r0)) } fsType = syscall.UTF16ToString(buf) return }
func main() { var hand uintptr = uintptr(0) var operator uintptr = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("open"))) var fpath uintptr = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("D:\\Program Files (x86)\\KoGou\\KGMusic\\KuGou.exe"))) var param uintptr = uintptr(0) var dirpath uintptr = uintptr(0) var ncmd uintptr = uintptr(1) shell32 := syscall.NewLazyDLL("shell32.dll") ShellExecuteW := shell32.NewProc("ShellExecuteW") _, _, _ = ShellExecuteW.Call(hand, operator, fpath, param, dirpath, ncmd) }
func initCommonControls() (err error) { var icc struct { dwSize uint32 dwICC uint32 } err = forceCommonControls6() if err != nil { return fmt.Errorf("error forcing Common Controls version 6 (or newer): %v", err) } icc.dwSize = uint32(unsafe.Sizeof(icc)) icc.dwICC = _ICC_PROGRESS_CLASS comctl32 = syscall.NewLazyDLL("comctl32.dll") r1, _, err := comctl32.NewProc("InitCommonControlsEx").Call(uintptr(unsafe.Pointer(&icc))) if r1 == _FALSE { // failure return fmt.Errorf("error initializing Common Controls (comctl32.dll); Windows last error: %v", err) } return nil }
// PhysicalMemoryBytes returns the total amount of host memory. func PhysicalMemoryBytes() (uint64, error) { // https://msdn.microsoft.com/en-us/library/windows/desktop/cc300158(v=vs.85).aspx // http://stackoverflow.com/questions/30743070/query-total-physical-memory-in-windows-with-golang mod := syscall.NewLazyDLL("kernel32.dll") proc := mod.NewProc("GetPhysicallyInstalledSystemMemory") var memkb uint64 ret, _, err := proc.Call(uintptr(unsafe.Pointer(&memkb))) // return value TRUE(1) succeeds, FAILED(0) fails if ret != 1 { return 0, err } return memkb * 1024, nil }