package main

import (
"fmt"
"log"
"syscall"
"unsafe"
) var (
user32 = syscall.MustLoadDLL("user32.dll")
procEnumWindows = user32.MustFindProc("EnumWindows")
procGetWindowTextW = user32.MustFindProc("GetWindowTextW")
) func EnumWindows(enumFunc uintptr, lparam uintptr) (err error) {
r1, _, e1 := syscall.Syscall(procEnumWindows.Addr(), 2, uintptr(enumFunc), uintptr(lparam), 0)
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
} func GetWindowText(hwnd syscall.Handle, str *uint16, maxCount int32) (len int32, err error) {
r0, _, e1 := syscall.Syscall(procGetWindowTextW.Addr(), 3, uintptr(hwnd), uintptr(unsafe.Pointer(str)), uintptr(maxCount))
len = int32(r0)
if len == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
} func FindWindow(title string) (syscall.Handle, error) {
var hwnd syscall.Handle
cb := syscall.NewCallback(func(h syscall.Handle, p uintptr) uintptr {
b := make([]uint16, 200)
_, err := GetWindowText(h, &b[0], int32(len(b)))
if err != nil {
// ignore the error
return 1 // continue enumeration
}
fmt.Printf("%s\n", syscall.UTF16ToString(b)) if syscall.UTF16ToString(b) == title {
// note the window
hwnd = h
return 0 // stop enumeration
}
return 1 // continue enumeration
})
EnumWindows(cb, 0)
if hwnd == 0 {
return 0, fmt.Errorf("No window with title '%s' found", title)
}
return hwnd, nil
} func main() {
//const title = "OpenTTD"
//const title = "FileZilla FTP Client"
const title = "OpenTTD"
h, err := FindWindow(title)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found '%s' window: handle=0x%x\n", title, h)
}

  https://stackoverflow.com/questions/19436860/go-golang-trying-to-get-window-information-via-syscall-as-in-enumwindows-etc

Go/Golang Trying to get window information via syscall. (As in EnumWindows, etc.) - Stack Overflow - Google Chrome
任务管理器
OpenTTD 1.3.0
openttd.exe 属性
filezilla.exe 属性
OfficePowerManagerWindow
game_manager [Y:\dev\game_manager] - C:\Go\src\os\exec\exec.go - GoLand

CandidateWindow
Mode Indicator
Mode Indicator
电池指示器
Network Flyout
Mode Indicator
Mode Indicator
go_win_api [Y:\dev\go_win_api] - ...\m.go - GoLand
OpenTTD 1.3.0
Found 'OpenTTD 1.3.0' window: handle=0x610d6
CandidateWindow
Mode Indicator
Mode Indicator
电池指示器
Network Flyout
Mode Indicator
Mode Indicator
go_win_api [Y:\dev\go_win_api] - ...\m.go - GoLand
OpenTTD 1.3.0
Get Retrun Value Before setForegroundWindow Invoked: %!s(uintptr=1)

  

package main

import (
"fmt"
"log"
"syscall"
"unsafe"
) var (
user32 = syscall.MustLoadDLL("user32.dll")
procEnumWindows = user32.MustFindProc("EnumWindows")
procGetWindowTextW = user32.MustFindProc("GetWindowTextW")
user32_b, _ = syscall.LoadLibrary("user32.dll")
setForegroundWindow, _ = syscall.GetProcAddress(user32_b, "SetForegroundWindow")
) func EnumWindows(enumFunc uintptr, lparam uintptr) (err error) {
r1, _, e1 := syscall.Syscall(procEnumWindows.Addr(), 2, uintptr(enumFunc), uintptr(lparam), 0)
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
} func GetWindowText(hwnd syscall.Handle, str *uint16, maxCount int32) (len int32, err error) {
r0, _, e1 := syscall.Syscall(procGetWindowTextW.Addr(), 3, uintptr(hwnd), uintptr(unsafe.Pointer(str)), uintptr(maxCount))
len = int32(r0)
if len == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
} func FindWindow(title string) (syscall.Handle, error) {
var hwnd syscall.Handle
cb := syscall.NewCallback(func(h syscall.Handle, p uintptr) uintptr {
b := make([]uint16, 200)
_, err := GetWindowText(h, &b[0], int32(len(b)))
if err != nil {
// ignore the error
return 1 // continue enumeration
}
fmt.Printf("%s\n", syscall.UTF16ToString(b)) if syscall.UTF16ToString(b) == title {
// note the window
hwnd = h
return 0 // stop enumeration
}
return 1 // continue enumeration
})
EnumWindows(cb, 0)
if hwnd == 0 {
return 0, fmt.Errorf("No window with title '%s' found", title)
}
return hwnd, nil
} func IntPtr(n int) uintptr {
return uintptr(n)
} func abort(funcname string, err syscall.Errno) {
panic(funcname + " failed: " + err.Error())
} func main() {
//const title = "OpenTTD"
//const title = "FileZilla FTP Client"
const title = "OpenTTD 1.3.0"
h, err := FindWindow(title)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found '%s' window: handle=0x%x\n", title, h) h, err = FindWindow(title)
if err != nil {
log.Fatal(err)
} ret, _, callErr := syscall.Syscall(setForegroundWindow, 1, uintptr(h), IntPtr(0), IntPtr(0))
if callErr != 0 {
abort("Call setForegroundWindow", callErr)
}
fmt.Printf("Get Retrun Value Before setForegroundWindow Invoked: %s\n", ret)
}

  

https://github.com/golang/go/wiki/WindowsDLLs

查看窗口名 调用dll setForegroundWindow的更多相关文章

  1. php调用dll经验小结

    最近做一个网站,需要频繁使用远程数据,数据接口已经做好.在做转换的时候遇到了性能上的问题:开始打算用php来实现转换,苦苦查了数天,都没有找到直接操作字节的方法.虽然可以使用 pack() 方法将各个 ...

  2. 摘: VS2010 C++ 调用 DLL (C++编写)

    一.为什么需要dll 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代码具有通用性,就可将它构造成相对独立的功能模块并在之后的项目中重复使用. 比较常见的例子是各种应用程序框架,如ATL. ...

  3. C#程序实现动态调用DLL的研究(转)

    摘 要:在<csdn开发高手>2004年第03期中的<化功大法——将DLL嵌入EXE>一文,介绍了如何把一个动态链接库作为一个资源嵌入到可执行文件,在可执行文件运行时,自动从资 ...

  4. 在VC中创建并调用DLL

    转自:http://express.ruanko.com/ruanko-express_45/technologyexchange6.html 一.DLL简介 1.什么是DLL? 动态链接库英文为DL ...

  5. 在 C++Builder 工程里调用 DLL 函数

    调用 Visual C++ DLL 给 C++Builder 程序员提出了一些独特的挑战.在我们试图解决 Visual C++ 生成的 DLL 之前,回顾一下如何调用一个 C++Builder 创建的 ...

  6. VB6.0调用DLL

    目录 第1章 VB6.0调用DLL    1 1 VC++编写DLL    1 1.1 使用__stdcall    1 1.2 使用 .DEF 文件    1 2 简单数据类型    2 2.1 传 ...

  7. 在VS2012中采用C++中调用DLL中的函数 (4)

    这两天因为需要用到VS2012来生成一个DLL代码,但是之前并没有用过DLL相关的内容,从昨天开始尝试调试DLL的文件调用,起初笔者在网络上找到了3片采用VSXXX版本进行调试的例子,相关的内容见本人 ...

  8. 在C++中调用DLL中的函数 (3)

    1.dll的优点 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代码具有通用性,就可将它构造成相对独立的功能模块并在之后的项目中重复使用.比较常见的例子是各种应用程序框架,ATL.MFC等 ...

  9. 在C++中调用DLL中的函数 (2)

    应用程序使用DLL可以采用两种方式: 一种是隐式链接,另一种是显式链接.在使用DLL之前首先要知道DLL中函数的结构信息. Visual C++6.0在VC\bin目录下提供了一个名为Dumpbin. ...

随机推荐

  1. wpf窗体项目 生成dll类库文件

    我想把一个wpf应用程序的输出类型由windows应用程序改为类库该怎么做,直接在项目属性里改的话报错为 库项目文件无法指定applicationdefinition属性 wpf窗体项目运行之后bin ...

  2. VueJs(15)--- Webstorm+Chrome 调试Vue项目

    Webstorm+Chrome 调试Vue项目 前言 在项目开发中,Debug模式是非常有必要的,后端对于IDEA工具而言Debug模式非常方便,但前端WebStorm而言如果启用Debug模式是需要 ...

  3. Java 从 Redis中取出的Json字符串 带斜杠的问题解决方案

    Java 从 Redis中取出的Json字符串 带斜杠的问题: { "code": 200, "message": "成功", " ...

  4. Redis基础篇(六)数据同步:主从复制

    Redis具有高可靠性,体现在两方面: 一是数据尽量少丢失,通过前面介绍的持久化方式AOF和RDB,在宕机时可以恢复数据. 二是服务尽量少中断,通过副本冗余来实现. 今天我们学习的就是通过主从复制实现 ...

  5. svg基础--基本语法与标签

    svg系列–基础 这里会总结svg的基础知识和一些经典的案例. svg简介 SVG(Scalable Vector Graphics)is an XML-based Language for crea ...

  6. IDEA本地运行Hadoop程序配置环境变量

    1.首先到github上下载hadoop-common-2.2.0-bin-master 2.解压放到自定义目录下 再将hadoop.dll文件复制到windows/System32目录下 3.配置环 ...

  7. TurtleBot3使用课程-第四节(北京智能佳)

    目录 1.机器学习 2 1.1 机器学习一 2 1.1.1 目标 2 1.1.2 操作环境 2 1.1.3 设置 2 1.1.4运行(它需要超过几秒取决于PC) 3 1.1.5运行屏幕 3 1.1.6 ...

  8. 第二章节 BJROBOT IMU 自动校正 【ROS全开源阿克曼转向智能网联无人驾驶车】

    1.把小车平放在地板上,用资料里的虚拟机,打开一个终端 ssh 过去主控端启动roslaunch znjrobot bringup.launch . 2.再打开一个终端,ssh 过去主控端,在 ~/c ...

  9. saltstack批量管理文件和计划任务

    简介 saltstack是由thomas Hatch于创建的一个开源项目,设计初衷是为了实现一个快速的远程执行系统.用来管理你的基础架构,可轻松管理成千上万台服务器. 关于saltstack更多功能本 ...

  10. String被final修饰

    源码: