前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Electron中调用DLL

Electron中调用DLL

作者头像
码客说
发布2019-10-21 17:12:51
11.3K0
发布2019-10-21 17:12:51
举报
文章被收录于专栏:码客码客

相关网址

NodeJS地址:https://nodejs.org/en/download/ Electron版本: https://electronjs.org/releases/stable

Edge.js源码: https://github.com/agracio/edge-js electron-edge-js:https://github.com/agracio/electron-edge-js

Node-ffi源码:https://github.com/node-ffi/node-ffi node-win32-api:https://github.com/waitingsong/node-win32-api winmm.dll: https://baike.baidu.com/item/winmm.dll/10962979?fr=aladdin

Edge.js

开源项目 edge 可以帮助我们实现 Node 和 .NET 之间的相互调用 我们最常见就是使用它来调用C#的代码方法或者C#生成的DLL文件的方法 C/C++生成的DLL就要用Node-ffi 因为我是要调用系统的DLL所以主要使用Node-ffi

Node-ffi

node-ffi是一个用于使用纯JavaScript加载和调用动态库的Node.js插件。它可以用来在不编写任何C ++代码的情况下创建与本地DLL库的绑定。同时它负责处理跨JavaScript和C的类型转换。

Node.js Addons相比,此方法有如下优点:

  1. 不需要源代码。
  2. 不需要每次重编译nodeNode.js Addons引用的.node会有文件锁,会对`electron应用热更新造成麻烦。
  3. 不要求开发者编写C代码,但是仍要求开发者具有一定C的知识。

缺点是:

  1. 性能有折损
  2. 类似其他语言的FFI调试,此方法近似黑盒调用, 查错比较困难。

为什么需要使用DLL

  • 需要使用系统 API 操作或扩展应用程序;
  • 需要调用第三方的接口API,特别是与硬件设备进行通信,而这些接口 API 基本上都是通过 C++ 动态链接库(DLL)实现的;
  • 需要调用C++实现的一些复杂算法等。

安装

node-ffi通过Buffer类,在C代码和JS代码之间实现了内存共享,类型转换则是通过refref-arrayref-struct实现。由于node-ffi/ref包含C原生代码,所以安装需要配置Node原生插件编译环境。

代码语言:javascript
复制
# 管理员运行bash/cmd/powershell,否则会提示权限不足
npm install --global --production windows-build-tools
npm install -g node-gyp

根据需要安装对应的库

代码语言:javascript
复制
npm install ffi
npm install ref
npm install ref-array
npm install ref-struct

如果是electron项目,则项目可以安装electron-rebuild插件,能够方便遍历node-modules中所有需要rebuild的库进行重编译。

代码语言:javascript
复制
npm install electron-rebuild

在package.json中配置快捷方式

代码语言:javascript
复制
"scripts": {
    "rebuild": "cd ./node_modules/.bin && electron-rebuild --force --module-dir=../../"
}

之后执行npm run rebuild 操作即可完成electron的重编译。

简单范例

DLL源码

代码语言:javascript
复制
extern "C" int __declspec(dllexport)My_Test(char *a, int b, int c);
extern "C" void __declspec(dllexport)My_Hello(char *a, int b, int c);

调用DLL

代码语言:javascript
复制
import ffi from 'ffi'
// `ffi.Library`用于注册函数,第一个入参为DLL路径,最好为文件绝对路径
const dll = ffi.Library( './test.dll', {
    // My_Test是dll中定义的函数,两者名称需要一致
    // [a, [b,c....]] a是函数出参类型,[b,c]是dll函数的入参类型
    My_Test: ['int', ['string', 'int', 'int']], // 可以用文本表示类型
    My_Hello: [ref.types.void, ['string', ref.types.int, ref.types.int]] // 更推荐用`ref.types.xx`表示类型,方便类型检查,`char*`的特殊缩写下文会说明
})

//同步调用
const result = dll.My_Test('hello', 3, 2)

//异步调用
dll.My_Test.async('hello', 3, 2, (err, result) => {
    if(err) {
        //todo
    }
    return result
})

变量类型

C语言中有4种基础数据类型—-整型 浮点型 指针 聚合类型

基础

整型、字符型都有分有符号和无符号两种。

类型

最小范围

char

0 ~ 127

signed char

-127 ~ 127

unsigned char

0 ~ 256

在不声明unsigned时 默认为signed型

refunsigned会缩写成u, 如 uchar 对应 usigned char

浮点型中有 float double long double

ref库中已经帮我们准备好了基础类型的对应关系。

C++类型

ref对应类型

void

ref.types.void

int8

ref.types.int8

uint8

ref.types.uint8

int16

ref.types.int16

uint16

ref.types.uint16

float

ref.types.float

double

ref.types.double

bool

ref.types.bool

char

ref.types.char

uchar

ref.types.uchar

short

ref.types.short

ushort

ref.types.ushort

int

ref.types.int

uint

ref.types.uint

long

ref.types.long

ulong

ref.types.ulong

DWORD

ref.types.ulong

DWORD为winapi类型,下文会详细说明

更多拓展可以去ref doc

ffi.Library中,既可以通过ref.types.xxx的方式申明类型,也可以通过文本(如uint16)进行申明。

字符型

字符型由char构成,在GBK编码中一个汉字占2个字节,在UTF-8中占用3~4个字节。一个ref.types.char默认一字节。根据所需字符长度创建足够长的内存空间。这时候需要使用ref-array库。

代码语言:javascript
复制
const ref = require('ref')
const refArray = require('ref-array')

const CharArray100 = refArray(ref.types.char, 100) // 申明char[100]类型CharArray100
const bufferValue = Buffer.from('Hello World') // Hello World转换Buffer
// 通过Buffer循环复制, 比较啰嗦
const value1 = new CharArray100()
for (let i = 0, l = bufferValue.length; i < l; i++) {
    value1[i] = bufferValue[i]
}
// 使用ref.alloc初始化类型
const strArray = [...bufferValue] //需要将`Buffer`转换成`Array`
const value2 = ref.alloc(CharArray100, strArray)

在传递中文字符型时,必须预先得知DLL库的编码方式。node默认使用UTF-8编码。若DLL不为UTF-8编码则需要转码,推荐使用iconv-lite

代码语言:javascript
复制
npm install iconv-lite

转码

代码语言:javascript
复制
const iconv = require('iconv-lite')
const cstr = iconv.encode(str, 'gbk')

注意!使用encode转码后cstrBuffer类,可直接作为当作uchar类型

iconv.encode(str.’gbk’)中gbk默认使用的是unsigned char | 0 ~ 256储存。假如C代码需要的是signed char | -127 ~ 127,则需要将buffer中的数据使用int8类型转换。

代码语言:javascript
复制
const Cstring100 = refArray(ref.types.char, 100)
const cString = new Cstring100()js
const uCstr = iconv.encode('农企药丸', 'gbk')
for (let i = 0; i < uCstr.length; i++) {
    cString[i] = uCstr.readInt8(i)
}

C代码为字符数组char[]/char *设置的返回值,通常返回的文本并不是定长,不会完全使用预分配的空间,末尾则会是无用的值。如果是预初始化的值,一般末尾是一大串的0x00,需要手动做trimEnd,如果不是预初始化的值,则末尾不定值,需要C代码明确返回字符串数组的长度returnValueLength

内置简写

ffi中内置了一些简写

代码语言:javascript
复制
ref.types.int => 'int'
ref.refType('int') => 'int*'
char* => 'string'

只建议使用’string’。

字符串虽然在js中被认为是基本类型,但在C语言中是以对象的形式来表示的,所以被认为是引用类型。所以string其实是char* 而不是char

聚合类型

多维数组

遇到定义为多维数组的基本类型 则需要使用ref-array进行创建

C

代码语言:javascript
复制
char cName[50][100] // 创建一个cName变量储存级50个最大长度为100的名字

JS

代码语言:javascript
复制
const ref = require('ref')
const refArray = require('ref-array')

const CName = refArray(refArray(ref.types.char, 100), 50)
const cName = new CName()
结构体

结构体是C中常用的类型,需要用到ref-struct进行创建

C

代码语言:javascript
复制
typedef struct {
    char cTMycher[100];
    int iAge[50];
    char cName[50][100];
    int iNo;
} Class;

typedef struct {
    Class class[4];
} Grade;

JS

代码语言:javascript
复制
const ref = require('ref')
const Struct = require('ref-struct')
const refArray = require('ref-array')

const Class = Struct({  // 注意返回的`Class`是一个类型
    cTMycher: RefArray(ref.types.char, 100),
    iAge: RefArray(ref.types.int, 50),
    cName: RefArray(RefArray(ref.types.char, 100), 50)
})
const Grade = Struct({ // 注意返回的`Grade`是一个类型
    class: RefArray(Class, 4)
})
const grade3 = new Grade() // 新建实例

指针

指针是一个变量,其值为实际变量的地址,即内存位置的直接地址,有些类似于JS中的引用对象。

C语言中使用*来代表指针

例如 int* a 则就是 整数型a变量的指针 , &用于表示取地址

代码语言:javascript
复制
int a=10,
int *p; // 定义一个指向整数型的指针`p`
p=&a // 将变量`a`的地址赋予`p`,即`p`指向`a`

node-ffi实现指针的原理是借助ref,使用Buffer类在C代码和JS代码之间实现了内存共享,让Buffer成为了C语言当中的指针。

注意,一旦引用ref,会修改Bufferprototype,替换和注入一些方法,请参考文档ref文档

代码语言:javascript
复制
const buf = new Buffer(4) // 初始化一个无类型的指针
buf.writeInt32LE(12345, 0) // 写入值12345

console.log(buf.hexAddress()) // 获取地址hexAddress

buf.type = ref.types.int // 设置buf对应的C类型,可以通过修改`type`来实现C的强制类型转换
console.log(buf.deref()) // deref()获取值12345

const pointer = buf.ref() // 获取指针的指针,类型为`int **`

console.log(pointer.deref().deref())  // deref()两次获取值12345

要明确一下两个概念 一个是结构类型,一个是指针类型,通过代码来说明。

代码语言:javascript
复制
// 申明一个类的实例
const grade3 = new Grade() // Grade 是结构类型
// 结构类型对应的指针类型
const GradePointer = ref.refType(Grade) // 结构类型`Grade`对应的指针的类型,即指向Grade
// 获取指向grade3的指针实例
const grade3Pointer = grade3.ref()
// deref()获取指针实例对应的值
console.log(grade3 === grade3Pointer.deref())  // 在JS层并不是同一个对象
console.log(grade3['ref.buffer'].hexAddress() === grade3Pointer.deref()['ref.buffer'].hexAddress()) //但是实际上指向的是同一个内存地址,即所引用值是相同的

可以通过ref.alloc(Object|String type, ? value) → Buffer直接得到一个引用对象

代码语言:javascript
复制
const iAgePointer = ref.alloc(ref.types.int, 18) // 初始化一个指向`int`类的指针,值为18
const grade3Pointer = ref.alloc(Grade) // 初始化一个指向`Grade`类的指针

回调函数

C的回调函数一般是用作入参传入。

代码语言:javascript
复制
const ref = require('ref')
const ffi = require('ffi')

const testDLL = ffi.Library('./testDLL', {
    setCallback: ['int', [
        ffi.Function(ref.types.void,  // ffi.Function申明类型, 用`'pointer'`申明类型也可以
        [ref.types.int, ref.types.CString])]]
})


const uiInfocallback = ffi.Callback(ref.types.void, // ffi.callback返回函数实例
    [ref.types.int, ref.types.CString],
    (resultCount, resultText) => {
        console.log(resultCount)
        console.log(resultText)
    },
)

const result = testDLL.uiInfocallback(uiInfocallback)

注意!如果你的CallBack是在setTimeout中调用,可能存在被GC的BUG

代码语言:javascript
复制
process.on('exit', () => {
    /* eslint-disable-next-line */
    uiInfocallback // keep reference avoid gc
})

代码实例

举个完整引用例子

C

代码语言:javascript
复制
// 头文件
#pragma  once

//#include "../include/MacroDef.h"
#define	CertMaxNumber 10
typedef struct {
	int length[CertMaxNumber];
	char CertGroundId[CertMaxNumber][2];
	char CertDate[CertMaxNumber][2048];
}  CertGroud;

#define DLL_SAMPLE_API  __declspec(dllexport)

extern "C"{
	//读取证书
	DLL_SAMPLE_API  int My_ReadCert(char *pwd, CertGroud *data,int *iCertNumber);
}

JS

代码语言:javascript
复制

const CertGroud = Struct({
    certLen: RefArray(ref.types.int, 10),
    certId: RefArray(RefArray(ref.types.char, 2), 10),
    certData: RefArray(RefArray(ref.types.char, 2048), 10),
    curCrtID: RefArray(RefArray(ref.types.char, 12), 10),
})

const dll = ffi.Library(path.join(staticPath, '/key.dll'), {
    My_ReadCert: ['int', ['string', ref.refType(CertGroud), ref.refType(ref.types.int)]],
})

async function readCert({ ukeyPassword, certNum }) {
    return new Promise(async (resolve) => {
        // ukeyPassword为string类型, c中指代 char*
        ukeyPassword = ukeyPassword.toString()
        // 根据结构体类型 开辟一个新的内存空间
        const certInfo = new CertGroud()
        // 开辟一个int 4字节内存空间
        const _certNum = ref.alloc(ref.types.int)
        // certInfo.ref()作为certInfo的指针传入
        dll.My_ucRMydCert.async(ukeyPassword, certInfo.ref(), _certNum, () => {
            // 清除无效空字段
            let cert = bufferTrim.trimEnd(new Buffer(certInfo.certData[certNum]))
            cert = cert.toString('binary')
            resolve(cert)
        })
    })
}

常见错误

  • Dynamic Linking Error: Win32 error 126

这个错误有三种原因

  1. 通常是传入的DLL路径错误,找不到Dll文件,推荐使用绝对路径。
  2. 如果是在x64的node/electron下引用32位的DLL,也会报这个错,反之亦然。要确保DLL要求的CPU架构和你的运行环境相同。
  3. DLL还有引用其他DLL文件,但是找不到引用的DLL文件,可能是VC依赖库或者多个DLL之间存在依赖关系。
  • Dynamic Linking Error: Win32 error 127:DLL中没有找到对应名称的函数,需要检查头文件定义的函数名是否与DLL调用时写的函数名是否相同。
Path设置

如果你的DLL是多个而且存在相互调用问题,会出现Dynamic Linking Error: Win32 error 126错误3。这是由于默认的进程Path是二进制文件所在目录,即node.exe/electron.exe目录而不是DLL所在目录,导致找不到DLL同目录下的其他引用。可以通过如下方法解决:

代码语言:javascript
复制
//方法一, 调用winapi SetDllDirectoryA设置目录
const ffi = require('ffi')

const kernel32 = ffi.Library("kernel32", {
'SetDllDirectoryA': ["bool", ["string"]]
})
kernel32.SetDllDirectoryA("pathToAdd")

//方法二(推荐),设置Path环境环境
process.env.PATH = `${process.env.PATH}${path.delimiter}${pathToAdd}`
闪崩问题

实际node-ffi调试的时候,很容易出现内存错误闪崩,甚至会出现断点导致崩溃的情况。这个是往往是因为非法内存访问造成,可以通过Windows日志看到错误信息,但是相信我,那并没有什么用。C的内存差错是不是一件简单的事情。

GetLastError

简单说node-ffi通过winapi来调用DLL,这导致GetLastError永远返回0。最简单方法就是自己写个C++ addon来绕开这个问题。

参考Issue GetLastError() always 0 when using Win32 API 参考PR github.com/node-ffi/no…

PVOID返回空,即内存地址FFFFFFFF闪崩

winapi中,经常通过判断返回的pvoid指针是否存在来判断是否成功,但是在node-ffi中,对FFFFFFFF的内存地址deref()会造成程序闪崩。必须迂回采用指针的指针类型进行特判

代码语言:javascript
复制
HDEVNOTIFY
WINAPI
RegisterDeviceNotificationA(
    _In_ HANDLE hRecipient,
    _In_ LPVOID NotificationFilter,
    _In_ DWORD Flags);

HDEVNOTIFY hDevNotify = RegisterDeviceNotificationA(hwnd, &notifyFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
if (!hDevNotify) {
	DWORD le = GetLastError();
	printf("RegisterDeviceNotificationA() failed [Error: %x]\r\n", le);
	return 1;
}

JS

代码语言:javascript
复制
const apiDef = SetupDiGetClassDevsW: [
    W.PVOID_REF, 
    [W.PVOID, W.PCTSTR, W.HWND, W.DWORD]
] // 注意返回类型`W.PVOID_REF`必须设置成pointer,就是不设置type,则node-ffi不会尝试`deref()`
const hDEVINFOPTR = this.setupapi.SetupDiGetClassDevsW(null, typeBuffer, null,
    setupapiConst.DIGCF_PRESENT | setupapiConst.DIGCF_ALLCLASSES
)
const hDEVINFO = winapi.utils.getPtrValue(hDEVINFOPTR, W.PVOID) // getPtrValue特判,如果地址为全`FF`则返回空
if (!hDEVINFO) {
    throw new ErrorWithCode(ErrorType.DEVICE_LIST_ERROR, ErrorCode.GET_HDEVINFO_FAIL)
}

附录

DLL分析工具
  1. Dependency Walker 可以查看DLL链接库的所有信息、以及DLL依赖关系的工具,但是很遗憾不支持WIN10。如果你不是WIN10用户,那么你只需要这一个工具即可,下面工具可以跳过。
  2. Process Monitor 可以查看进程执行时候的各种操作,如IO、注册表访问等。这里用它来监听node/electron进程的IO操作,用于排查Dynamic Linking Error: Win32 error错误原因3,可以查看ffi.Libary时的所有IO请求和对应结果,查看缺少了什么DLL
  3. dumpbin dumpbin.exe为Microsoft COFF二进制文件转换器,它显示有关通用对象文件格式(COFF)二进制文件的信息。可用使用dumpbin检查COFF对象文件、标准COFF对象库、可执行文件和动态链接库等。 通过开始菜单 -> Visual Studio 20XX -> Visual Studio Tools -> VS20XX x86 Native Command Prompt启动。 # 返回DLL头部信息,会说明是32 bit word Machine/64 bit word Machine dumpbin /headers [dll路径] # 返回DLL导出信息,name列表为导出的函数名 dumpbin /exports [dll路径]
自动转换工具

tjfontaine大神提供了一个node-ffi-generate,可以根据头文件,自动生成node-ffi函数申明,注意这个需要Linux环境,简单用KOA包了一层改成了在线模式ffi-online,可以丢到VPS中运行。

WINAPI

winapi存在大量的自定义的变量类型,waitingsong大侠的轮子 node-win32-api中完整翻译了全套windef.h中的类型,而且这个项目采用TS来规定FFI的返回Interface,很值得借鉴。

注意!里面的类型不一定都是对的,相信作者也没有完整的测试过所有变量,实际使用中也遇到过里面类型错误的坑。

转载自: https://juejin.im/post/5b58038d5188251b186bc902

实例-禁用窗口右键菜单

现在使用 ffi 调用 user32.dll 中的 GetSystemMenu 函数来解决这个问题,首先新建一个 user32.js 文件,为了展示 ffi ,我多定义了几个API函数:

代码语言:javascript
复制
const ffi = require('ffi')

exports.User32 = ffi.Library('user32', {
    'GetWindowLongPtrW': ['int', ['int', 'int']],
    'SetWindowLongPtrW': ['int', ['int', 'int', 'long']],
    'GetSystemMenu': ['int', ['int', 'bool']],
    'DestroyWindow': ['bool', ['int']]
});

修改 app.js 文件,首先导入 user32.js:

代码语言:javascript
复制
const user32 = require('./app/scripts/user32').User32

然后修改如下内容:

代码语言:javascript
复制
win.once('ready-to-show', () => {
        let hwnd = win.getNativeWindowHandle() //获取窗口句柄。
        user32.GetSystemMenu(hwnd.readUInt32LE(0), true); //禁用系统菜单.
        win.show()
    })

再运行项目,系统菜单就消失的无影无踪了。

User32.dll中的函数

C#代码

代码语言:javascript
复制
/// <summary>
/// 该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄,以后可以在GDI函数中使用该句柄来在设备上下文环境中绘图。hWnd:设备上下文环境被检索的窗口的句柄
/// </summary>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetDC(IntPtr hWnd);
/// <summary>
/// 函数释放设备上下文环境(DC)供其他应用程序使用。
/// </summary>
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
/// <summary>
/// 该函数返回桌面窗口的句柄。桌面窗口覆盖整个屏幕。
/// </summary>
static public extern IntPtr GetDesktopWindow();
/// <summary>
/// 该函数设置指定窗口的显示状态。
/// </summary>
static public extern bool ShowWindow(IntPtr hWnd, short State);
/// <summary>
/// 通过发送重绘消息 WM_PAINT 给目标窗体来更新目标窗体客户区的无效区域。
/// </summary>
static public extern bool UpdateWindow(IntPtr hWnd);
/// <summary>
/// 该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。
/// </summary>
static public extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// 该函数改变一个子窗口,弹出式窗口式顶层窗口的尺寸,位置和Z序。
/// </summary>
static public extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int Width, int Height, uint flags);
/// <summary>
/// 打开剪切板
/// </summary>
static public extern bool OpenClipboard(IntPtr hWndNewOwner);
/// <summary>
/// 关闭剪切板
/// </summary>
static public extern bool CloseClipboard();
/// <summary>
/// 打开清空</summary>
static public extern bool EmptyClipboard();
/// <summary>
/// 将存放有数据的内存块放入剪切板的资源管理中
/// </summary>
static public extern IntPtr SetClipboardData(uint Format, IntPtr hData);
/// <summary>
/// 在一个矩形中装载指定菜单条目的屏幕坐标信息 
/// </summary>
static public extern bool GetMenuItemRect(IntPtr hWnd, IntPtr hMenu, uint Item, ref RECT rc);

[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
/// <summary>
/// 该函数获得一个指定子窗口的父窗口句柄。
/// </summary>
public static extern IntPtr GetParent(IntPtr hWnd);
/// <summary>
/// 该函数将指定的消息发送到一个或多个窗口。此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回。 
/// </summary>
/// <param name="hWnd">其窗口程序将接收消息的窗口的句柄</param>
/// <param name="msg">指定被发送的消息</param>
/// <param name="wParam">指定附加的消息指定信息</param>
/// <param name="lParam">指定附加的消息指定信息</param>
/// <returns></returns>
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);        
public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref RECT lParam);
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref POINT lParam);       
public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref TBBUTTON lParam);        
public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref TBBUTTONINFO lParam);      
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref REBARBANDINFO lParam);      
public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref TVITEM lParam);       
public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref LVITEM lParam);    
public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref HDITEM lParam);   
public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref HD_HITTESTINFO hti);  
/// <summary>
/// 该函数将一个消息放入(寄送)到与指定窗口创建的线程相联系消息队列里
/// </summary>
public static extern IntPtr PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);
public static extern IntPtr SetWindowsHookEx(int hookid, HookProc pfnhook, IntPtr hinst, int threadid);

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool UnhookWindowsHookEx(IntPtr hhook);

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr CallNextHookEx(IntPtr hhook, int code, IntPtr wparam, IntPtr lparam);
/// <summary>
/// 该函数对指定的窗口设置键盘焦点。
/// </summary>
public static extern IntPtr SetFocus(IntPtr hWnd);
/// <summary>
/// 该函数在指定的矩形里写入格式化文本,根据指定的方法对文本格式化(扩展的制表符,字符对齐、折行等)。
/// </summary>
public extern static int DrawText(IntPtr hdc, string lpString, int nCount, ref RECT lpRect, int uFormat);
/// <summary>
/// 该函数改变指定子窗口的父窗口。
/// </summary>
public extern static IntPtr SetParent(IntPtr hChild, IntPtr hParent);
/// <summary>
/// 获取对话框中子窗口控件的句柄
/// </summary>
public extern static IntPtr GetDlgItem(IntPtr hDlg, int nControlID);
/// <summary>
/// 该函数获取窗口客户区的坐标。
/// </summary>
public extern static int GetClientRect(IntPtr hWnd, ref RECT rc);
/// <summary>
/// 该函数向指定的窗体添加一个矩形,然后窗口客户区域的这一部分将被重新绘制。
/// </summary>
public extern static int InvalidateRect(IntPtr hWnd, IntPtr rect, int bErase);
/// <summary>
/// 该函数产生对其他线程的控制,如果一个线程没有其他消息在其消息队列里。
/// </summary>
public static extern bool WaitMessage();
/// <summary>
/// 该函数为一个消息检查线程消息队列,并将该消息(如果存在)放于指定的结构。
/// </summary>
public static extern bool PeekMessage(ref MSG msg, int hWnd, uint wFilterMin, uint wFilterMax, uint wFlag);
/// <summary>
/// 该函数从调用线程的消息队列里取得一个消息并将其放于指定的结构。此函数可取得与指定窗口联系的消息和由PostThreadMesssge寄送的线程消息。此函数接收一定范围的消息值。
/// </summary>
public static extern bool GetMessage(ref MSG msg, int hWnd, uint wFilterMin, uint wFilterMax);
/// <summary>
/// 该函数将虚拟键消息转换为字符消息。
/// </summary>
public static extern bool TranslateMessage(ref MSG msg);
/// <summary>
/// 该函数调度一个消息给窗口程序。
/// </summary>
public static extern bool DispatchMessage(ref MSG msg);
/// <summary>
/// 该函数从一个与应用事例相关的可执行文件(EXE文件)中载入指定的光标资源.
/// </summary>
public static extern IntPtr LoadCursor(IntPtr hInstance, uint cursor);
/// <summary>
/// 该函数确定光标的形状。
/// </summary>
public static extern IntPtr SetCursor(IntPtr hCursor);
/// <summary>
/// 确定当前焦点位于哪个控件上。
/// </summary>
public static extern IntPtr GetFocus();
/// <summary>
/// 该函数从当前线程中的窗口释放鼠标捕获,并恢复通常的鼠标输入处理。捕获鼠标的窗口接收所有的鼠标输入(无论光标的位置在哪里),除非点击鼠标键时,光标热点在另一个线程的窗口中。
/// </summary>
public static extern bool ReleaseCapture();
/// <summary>
/// 准备指定的窗口来重绘并将绘画相关的信息放到一个PAINTSTRUCT结构中。
/// </summary>
public static extern IntPtr BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
/// <summary>
/// 标记指定窗口的绘画过程结束,每次调用BeginPaint函数之后被请求
/// </summary>
public static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
/// <summary>
/// 半透明窗体
/// </summary>
public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, IntPtr hdcSrc, ref POINT pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
/// <summary>
/// 该函数返回指定窗口的边框矩形的尺寸。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。
/// </summary>
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
/// <summary>
/// 该函数将指定点的用户坐标转换成屏幕坐标。
/// </summary>
public static extern bool ClientToScreen(IntPtr hWnd, ref POINT pt);
/// <summary>
/// 当在指定时间内鼠标指针离开或盘旋在一个窗口上时,此函数寄送消息。
/// </summary>
public static extern bool TrackMouseEvent(ref TRACKMOUSEEVENTS tme);
/// <summary>
/// 
/// </summary>
public static extern bool SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
/// <summary>
/// 该函数检取指定虚拟键的状态。
/// </summary>
public static extern ushort GetKeyState(int virtKey);
/// <summary>
/// 该函数改变指定窗口的位置和尺寸。对于顶层窗口,位置和尺寸是相对于屏幕的左上角的:对于子窗口,位置和尺寸是相对于父窗口客户区的左上角坐标的。
/// </summary>
public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
/// <summary>
/// 该函数获得指定窗口所属的类的类名。
/// </summary>
public static extern int GetClassName(IntPtr hWnd, out STRINGBUFFER ClassName, int nMaxCount);
/// <summary>
/// 该函数改变指定窗口的属性
/// </summary>
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
/// <summary>
/// 该函数检索指定窗口客户区域或整个屏幕的显示设备上下文环境的句柄,在随后的GDI函数中可以使用该句柄在设备上下文环境中绘图。
/// </summary>
public static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hRegion, uint flags);
/// <summary>
/// 获取整个窗口(包括边框、滚动条、标题栏、菜单等)的设备场景 返回值 Long。
/// </summary>
public static extern IntPtr GetWindowDC(IntPtr hWnd);
/// <summary>
/// 该函数用指定的画刷填充矩形,此函数包括矩形的左上边界,但不包括矩形的右下边界。
/// </summary>
public static extern int FillRect(IntPtr hDC, ref RECT rect, IntPtr hBrush);
/// <summary>
/// 该函数返回指定窗口的显示状态以及被恢复的、最大化的和最小化的窗口位置。
/// </summary>
public static extern int GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT wp);
/// <summary>
/// 该函数改变指定窗口的标题栏的文本内容
/// </summary>
public static extern int SetWindowText(IntPtr hWnd, string text);
/// <summary>
/// 该函数将指定窗口的标题条文本(如果存在)拷贝到一个缓存区内。如果指定的窗口是一个控制,则拷贝控制的文本。
/// </summary>
public static extern int GetWindowText(IntPtr hWnd, out STRINGBUFFER text, int maxCount);
/// <summary>
/// 用于得到被定义的系统数据或者系统配置信息.
/// </summary>
static public extern int GetSystemMetrics(int nIndex);
/// <summary>
/// 该函数设置滚动条参数,包括滚动位置的最大值和最小值,页面大小,滚动按钮的位置。
/// </summary>
static public extern int SetScrollInfo(IntPtr hwnd, int bar, ref SCROLLINFO si, int fRedraw);
/// <summary>
/// 该函数显示或隐藏所指定的滚动条。
/// </summary>
public static extern int ShowScrollBar(IntPtr hWnd, int bar, int show);
/// <summary>
/// 该函数可以激活一个或两个滚动条箭头或是使其失效。
/// </summary>
public static extern int EnableScrollBar(IntPtr hWnd, uint flags, uint arrows);
/// <summary>
/// 该函数将指定的窗口设置到Z序的顶部。
/// </summary>
public static extern int BringWindowToTop(IntPtr hWnd);
/// <summary>
/// 该函数滚动指定窗体客户区域的目录。
/// </summary>
static public extern int ScrollWindowEx(IntPtr hWnd, int dx, int dy,ref RECT rcScroll, ref RECT rcClip, IntPtr UpdateRegion, ref RECT rcInvalidated, uint flags);
/// <summary>
/// 该函数确定给定的窗口句柄是否识别一个已存在的窗口。
/// </summary>
public static extern int IsWindow(IntPtr hWnd);
/// <summary>
/// 该函数将256个虚拟键的状态拷贝到指定的缓冲区中。
/// </summary>
public static extern int GetKeyboardState(byte[] pbKeyState);
/// <summary>
/// 该函数将指定的虚拟键码和键盘状态翻译为相应的字符或字符串。该函数使用由给定的键盘布局句柄标识的物理键盘布局和输入语言来翻译代码。
/// </summary>
public static extern int ToAscii(int uVirtKey,int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey,int fuState);

根据进程名称获取窗口句柄

先看看C#代码怎么写

代码语言:javascript
复制
[StructLayout(LayoutKind.Sequential)]
public struct ProcessEntry32
{
    public uint dwSize;
    public uint cntUsage;
    public uint th32ProcessID;
    public IntPtr th32DefaultHeapID;
    public uint th32ModuleID;
    public uint cntThreads;
    public uint th32ParentProcessID;
    public int pcPriClassBase;
    public uint dwFlags;


    [MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]
    public string szExeFile;
}


[DllImport("KERNEL32.DLL")]
public static extern IntPtr CreateToolhelp32Snapshot(uint flags,uint processid);
[DllImport("KERNEL32.DLL")]
public static extern int CloseHandle(IntPtr handle);
[DllImport("KERNEL32.DLL")]
public static extern int Process32First(IntPtr handle,ref   ProcessEntry32 pe);
[DllImport("KERNEL32.DLL")]
public static extern int Process32Next(IntPtr handle,ref   ProcessEntry32 pe);

[DllImport("User32.dll",EntryPoint="SendMessage")]
private static extern int SendMessage(int hWnd,int Msg,int wParam,string lParam);


public IntPtr GetHandleByProcessName(string ProcessName)
{
    List<ProcessEntry32> list=new List<ProcessEntry32>();
    IntPtr handle=CreateToolhelp32Snapshot(0x2,0);
    IntPtr hh=IntPtr.Zero;
    if((int)handle>0)
    {
        ProcessEntry32 pe32=new ProcessEntry32();
        pe32.dwSize=(uint)Marshal.SizeOf(pe32);
        int bMore=Process32First(handle,ref pe32);
        while(bMore==1)
        {
            IntPtr temp=Marshal.AllocHGlobal((int)pe32.dwSize);
            Marshal.StructureToPtr(pe32,temp,true);
            ProcessEntry32 pe=(ProcessEntry32)Marshal.PtrToStructure(temp,typeof(ProcessEntry32));
            Marshal.FreeHGlobal(temp);
            list.Add(pe);
            if(pe.szExeFile==ProcessName)
            {
                bMore=2;
                hh=GetCurrentWindowHandle(pe.th32ProcessID);
                break;
            }
            bMore=Process32Next(handle,ref pe32);
        }
    }
    return hh;
}
public static IntPtr GetCurrentWindowHandle(uint proid)
{
    IntPtr ptrWnd=IntPtr.Zero;
    uint uiPid=proid;
    object objWnd=processWnd[uiPid];
    if(objWnd!=null)
    {
        ptrWnd=(IntPtr)objWnd;
        if(ptrWnd!=IntPtr.Zero&&IsWindow(ptrWnd))  // 从缓存中获取句柄
        {
            return ptrWnd;
        }
        else
        {
            ptrWnd=IntPtr.Zero;
        }
    }
    bool bResult=EnumWindows(new WNDENUMPROC(EnumWindowsProc),uiPid);
    // 枚举窗体返回 false 而且没有错误号时表明获取成功
    if(!bResult&&Marshal.GetLastWin32Error()==0)
    {
        objWnd=processWnd[uiPid];
        if(objWnd!=null)
        {
            ptrWnd=(IntPtr)objWnd;
        }
    }
    return ptrWnd;
}


private static bool EnumWindowsProc(IntPtr hwnd,uint lParam)
{
    uint uiPid=0;
    if(GetParent(hwnd)==IntPtr.Zero)
    {
        GetWindowThreadProcessId(hwnd,ref uiPid);
        if(uiPid==lParam)    // 找到进程相应的主窗体句柄
        {
            processWnd.Add(uiPid,hwnd);   // 把句柄缓存起来
            SetLastError(0);    // 设置无错误
            return false;   // 返回 false 以终止枚举窗体
        }
    }
    return true;
}

调用

代码语言:javascript
复制
IntPtr hh=GetHandleByProcessName("notepad.exe");
if(hh!=IntPtr.Zero)
{
    SendMessage((int)hh,0x000C,0,"A");  //打开记事本,发送字母A
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 相关网址
  • Edge.js
  • Node-ffi
    • 为什么需要使用DLL
      • 安装
        • 简单范例
          • 变量类型
            • 基础
            • 字符型
            • 内置简写
          • 聚合类型
            • 多维数组
            • 结构体
          • 指针
            • 回调函数
              • 代码实例
                • 常见错误
                  • Path设置
                  • 闪崩问题
                  • GetLastError
                  • PVOID返回空,即内存地址FFFFFFFF闪崩
                • 附录
                  • DLL分析工具
                  • 自动转换工具
                  • WINAPI
                • 实例-禁用窗口右键菜单
                • User32.dll中的函数
                • 根据进程名称获取窗口句柄
                相关产品与服务
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档