• nodejs 调用win32 api


    >node -v
    v12.16.1
    
    >npm install -g node-gyp
    >npm i @saleae/ffi
    
    >node test.js
    1
    

    test.js

    const ffi = require("@saleae/ffi");
    
    // Convert JSString to CString
    function TEXT(text) {
      return Buffer.from(`${text}`, "ucs2");
    }
    
    // Import user32
    const user32 = new ffi.Library("user32", {
      // 返回值类型,参数列表类型
      MessageBoxW: ["int32", ["int32", "string", "string", "int32"]],
      SetCursorPos: ["bool", ["int32", "int32"]],
    });
    
    const OK_or_Cancel = user32.MessageBoxW(0, TEXT("Hello from Node.js!"), TEXT("Hello, World!"), 1);
    
    console.log(OK_or_Cancel);
    
    // user32.SetCursorPos(0, 0);
    

    获取窗口句柄

    const ffi = require("@saleae/ffi");
    
    // Convert JSString to CString
    function TEXT(text) {
      return Buffer.from(`${text}`, "ucs2");
    }
    
    // Import user32
    const user32 = new ffi.Library("user32", {
      FindWindowW: ["int32", ["string", "string"]],
    });
    
    var hwnd = user32.FindWindowW(TEXT('Progman'), null);
    console.log(hwnd); // 65814
    console.log(hwnd.toString(16)); // 10116
    


    如果你熟练win32 api 知道自己在做什么,你可以使用 node-windows-x64

    install

    λ npm i node-windows-x64
    

    调用 MessageBoxA 示例

    import { node_windows_x64 as nw } from "node-windows-x64";
    
    nw["user32.messageboxa"](0, "body", "title", 2);
    
  • 相关阅读:
    把Discuz!NT放在虚拟目录下
    表格样式
    页面CSS的问题,不懂中
    临时存储信息
    asp.net2.0事务
    repeater控件分成两列显示
    P2P的NAT研究
    javascript字符串trim的实现
    新浪微博OAUTH2验证
    新浪微博接口在safri下的bug
  • 原文地址:https://www.cnblogs.com/ajanuw/p/12787653.html
Copyright © 2020-2023  润新知