golang 调用windows API 中文的处理
Go语言发展势头很猛,其实缺点也很多,好在有广大爱好者提供了无数的库,把优点表现得太好了,搞得什么都是拿来就使用,基本完全不理会指针,性能还不错。
最近在windows下使用遇到一个中文的问题,首先要了解的是Golang的编码是utf-8的,而中文windows的API返回时多字节的GBK编码。
下面是利用API 获得进程的示例,代码是网上的,但是使用时出现了,当进程名是中文时出现的乱码问题。
先贴代码。
package utilities import (
"bytes"
"io"
"log"
"net/http"
"sort"
"strconv"
"strings"
"syscall" // "unicode/utf8"
"unsafe" "github.com/axgle/mahonia"
) type ulong int32
type ulong_ptr uintptr type PROCESSENTRY32 struct {
dwSize ulong
cntUsage ulong
th32ProcessID ulong
th32DefaultHeapID ulong_ptr
th32ModuleID ulong
cntThreads ulong
th32ParentProcessID ulong
pcPriClassBase ulong
dwFlags ulong
szExeFile [260]byte
} type ProcessStruct struct {
processName string // 进程名称
processID int // 进程id
} type ProcessStructSlice []ProcessStruct func (a ProcessStructSlice) Len() int { // 重写 Len() 方法
return len(a)
}
func (a ProcessStructSlice) Swap(i, j int) { // 重写 Swap() 方法
a[i], a[j] = a[j], a[i]
}
func (a ProcessStructSlice) Less(i, j int) bool { // 重写 Less() 方法, 从大到小排序
if strings.Compare(a[j].processName, a[i].processName) < 0 {
return true
} else {
return false
}
} func Upayin_process(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
_, err := r.Form["callsys"]
if !err {
io.WriteString(w, "err")
return
} kernel32 := syscall.NewLazyDLL("kernel32.dll")
CreateToolhelp32Snapshot := kernel32.NewProc("CreateToolhelp32Snapshot")
pHandle, _, _ := CreateToolhelp32Snapshot.Call(uintptr(0x2), uintptr(0x0))
if int(pHandle) == -1 {
io.WriteString(w, "get process err")
return
}
var data []ProcessStruct
var buffer bytes.Buffer decoder := mahonia.NewDecoder("gbk") Process32Next := kernel32.NewProc("Process32Next")
for {
var proc PROCESSENTRY32
proc.dwSize = ulong(unsafe.Sizeof(proc))
if rt, _, _ := Process32Next.Call(uintptr(pHandle), uintptr(unsafe.Pointer(&proc))); int(rt) == 1 { len_szExeFile := 0
for _, b := range proc.szExeFile {
if b == 0 {
break
}
len_szExeFile++
}
var bytetest []byte = []byte(proc.szExeFile[:len_szExeFile])
_, newdata, newerr := decoder.Translate(bytetest, true)
if newerr != nil {
log.Println(newerr) } data = append(data, ProcessStruct{
processName: string(newdata),
processID: int(proc.th32ProcessID),
}) } else {
break
}
} CloseHandle := kernel32.NewProc("CloseHandle")
_, _, _ = CloseHandle.Call(pHandle) sort.Sort(ProcessStructSlice(data))
for _, v := range data {
log.Println(v.processName) buffer.WriteString("ProcessName : ")
buffer.WriteString(v.processName)
buffer.WriteString(" ProcessID : ")
buffer.WriteString(strconv.Itoa(v.processID))
buffer.WriteString("\n")
} io.WriteString(w, buffer.String()) }
重要的是
"github.com/axgle/mahonia" //这个库
decoder := mahonia.NewDecoder("gbk")
//gbk转utf8
var bytetest []byte = []byte(proc.szExeFile[:len_szExeFile])
_, newdata, newerr := decoder.Translate(bytetest, true) 其实里面做了判断,并不是单纯的使用utf8.EncodeRune来解决,刚开始我也是直接使用utf8这个库来尝试,没成功. 在这里做个分享,呵呵
golang 调用windows API 中文的处理的更多相关文章
- C#调用windows API的一些方法
使用C#调用windows API(从其它地方总结来的,以备查询) C#调用windows API也可以叫做C#如何直接调用非托管代码,通常有2种方法: 1. 直接调用从 DLL 导出的函数. 2. ...
- C#调用Windows API函数截图
界面如下: 下面放了一个PictureBox 首先是声明函数: //这里是调用 Windows API函数来进行截图 //首先导入库文件 [System.Runtime.InteropServices ...
- 【转】用C#调用Windows API向指定窗口发送
一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.InteropServices; 2.引用需要使用的方法,格式 ...
- C#中调用Windows API的要点 .
介绍 API(Application Programming Interface),我想大家不会陌生,它是我们Windows编程的常客,虽然基于.Net平台的C#有了强大的类库,但是,我们还是不能否认 ...
- c# 判断窗体是否永在最前(TopMost),调用windows API
许多程序都可以把自身的窗体设为最前显示状态,这个可以参考博客c#让窗体永在最前 调用windows api 将窗体设为topmost.那么如何判断桌面上的一个窗体是否为最前显示状态呢,不光是自己的程序 ...
- c#让窗体永在最前 调用windows api 将窗体设为topmost
有时候应用程序需要将一个窗体始终位于屏幕的最前面,即使切换到其它窗体也能看到该窗体,这样的窗体就叫做TopMost窗体. 用C#制作TopMost窗体之前,首先要了解如何声明SetWindowPos函 ...
- 善于 调用Windows API
前一段时间看见别人做的一个自动填写信息并且点击登录的程序,觉得很有意思. 其实就是在程序中调用Windows的API,那么如何调用,下面就做个简单的介绍. 写的简单粗暴, 不喜轻喷. 0.首先引入名称 ...
- C#中调用Windows API时的数据类型对应关系
原文 C#中调用Windows API时的数据类型对应关系 BOOL=System.Int32 BOOLEAN=System.Int32 BYTE=System.UInt16 CHAR=System. ...
- 用C#调用Windows API向指定窗口发送按键消息 z
用C#调用Windows API向指定窗口发送 一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.Interop ...
随机推荐
- 【原】Java学习笔记034 - 网络
package cn.temptation; import java.net.InetAddress; public class Sample01 { public static void main( ...
- 微信小程序客服消息开发实战:实时在手机上接收小程序客服消息通知,以及在手机上回复
在微信小程序开发中,可以非常方便的集成客服功能,只需要一行代码便可以将用户引导至客服会话界面.这行代码就是: <button open-type="contact" bind ...
- Linux下编译安装php7.2
准备工作 所有操作需要在root用户下 安装路径: /usr/local/php 安装PHP 首先要安装如下依赖包 $ yum install -y gcc gcc-c++ make zlib zl ...
- shell打印 菱形
#!/bin/bashread -p "input the length: " n for i in `seq 1 $n`do for ((j=$n;j> ...
- python接口自动化(六)--发送get请求接口(详解)
简介 如果想用python做接口测试,我们首先有不得不了解和学习的模块.它就是第三方模块:Requests. 虽然Python内置的urllib模块,用于访问网络资源.但是,它用起来比较麻烦,而且,缺 ...
- 处理SQL Server中的重复行
如果表中的数据需要基于行中的多个值具有唯一约束,则适合的解决方案将是复合健. 复合主键 使用SQL Server语法创建符合主键非常简单. create table my_parts ( id_par ...
- qml demo分析(rssnews-常见新闻布局)
一.效果展示 今儿来分析一篇常见的ui布局,完全使用qml编写,ui交互效果友好,如图1所示,是一个常见的客户端新闻展示效果,左侧是一个列表,右侧是新闻详情. 图1 新闻效果图 二.源码分析 首先先来 ...
- 程序员也想改 Lottie 动画?是的!
一.前言 Hi,大家好,我是承香墨影! Lottie 是 Airbnb 开源的一套跨平台的完整的动画效果解决方案,用过都说好.完全解耦开发人员和设计师,让设计师设计的动画,在程序中无缝还原,真是一旦拿 ...
- django插入数据库错误:mysql的1267错误
错误信息: django.db.utils.OperationalError: (1267, "Illegal mix of collations (latin1_swedish_ci,IM ...
- springboot~rabbitmq自己通过UI手动发布队列需要注意的地方
springboot里发布队列消息 为了兼容性和可读性更好,我们一般使用json字符串做为数据载体. public void decreaseCallMonitor(CallMonitorInfo c ...