Giter Site home page Giter Site logo

windows-ffi's Introduction

Windows FFI

NodeJS wrappers around Windows p-invoke/dll functions in user32, gdi32, etc. (based on node-ffi-napi)

Installation

  • 1) Install ffi-napi in your project: npm i ffi-napi
    • 1.1) You may need to install the windows-build-tools first for ffi-napi to be able to build: npm install --global --production windows-build-tools
  • 2) Install: npm i windows-ffi
    • 2.1) If you're using windows-ffi in symlinked mode (ie. you probably don't need this step!), supply the parent project's copy of the node-ffi libraries using the init code below:
       // Make sure the code below runs before windows-ffi is imported! (example approach: https://stackoverflow.com/a/42817956)
       import ffi from "ffi-napi";
       import ref from "ref-napi";
       import refStructDI from "ref-struct-di";
       import {SupplyConfig} from "windows-ffi/Dist/@UserSupplied/PreImportConfig.js";
      
       SupplyConfig({ffi, ref, refStructDI});
      

Usage

While this library is meant to gradually expand to cover more of the Windows API, it will never be 100%, because it's a "develop as you need it" sort of endeavor.

Some of the most common and/or noteworthy functionalities are shown below.

Capture image-data from a region on-screen

import {VRect, CaptureScreenshot, GetForegroundWindowHandle} from "windows-ffi";

// First capture a screenshot of a section of the screen.
const screenshot = CaptureScreenshot({
	windowHandle: GetForegroundWindowHandle(), // comment to screenshot all windows
	rectToCapture: new VRect(0, 0, 800, 600),
});

// The image-data is now stored in the `screenshot.buffer` Buffer object.
// Access it directly (and cheaply) using the helper functions on `screenshot`.
for (let x = 0; x < 800; x++) {
	console.log(`Pixel color at [${x}, 0] is:`, screenshot.GetPixel(x, 0).ToHex_RGB());
}

Performance:

  • Full-screen (2560x1440), all windows (ie. "desktop" window): ~80ms
  • Full-screen (2560x1440), single window: ~20ms
  • Region (700x200), all windows (ie. "desktop" window): ~50ms
  • Region (700x200), single window: ~4ms

Tasks

General:

  • Create an accompanying library for Windows-specific functionalities that do not require the ffi-napi dependency. (I've had pains with ffi-related building during NodeJS updates, so things that can be done without that dependency is beneficial to bundle separately)

windows-ffi's People

Contributors

jojos38 avatar venryx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

windows-ffi's Issues

VRect

I don’t understand what I'm doing wrong, I seem to have done everything according to the instructions, but I get an error:
import {VRect, CaptureScreenshot, GetForegroundWindowHandle} from "windows-ffi";
^^^^^
SyntaxError: The requested module 'windows-ffi' does not provide an export named 'VRect'
←[90m at ModuleJob._instantiate (node:internal/modules/esm/module_job:127:21)←[39m
←[90m at async ModuleJob.run (node:internal/modules/esm/module_job:191:5)←[39m
at async Promise.all (index 0)
←[90m at async ESMLoader.import (node:internal/modules/esm/loader:331:24)←[39m
←[90m at async loadESM (node:internal/process/esm_loader:88:5)←[39m
←[90m at async handleMainPromise (node:internal/modules/run_main:65:12)←[39m

custom window handle not work?

Hi, first of all thanks for this repo, much needed given not a lot of resources online and I am not familiar with c/c++..
I have encountered some issue though: when I try the following it works perfectly

	const screenshot = CaptureScreenshot({
		windowHandle: user32.GetDesktopWindow()
	});

However when I try windowHandle: GetForegroundWindowHandle() or windowHandle: user32.FindWindowA(null, "Calculator") it generates blank image, the width/height info are correct but the pixels output from screenshot.GetPixel(x, y).ToHex_RGB() are mostly 000000, I say mostly because I have tried to use it on my Outlook Inbox window, it showed a tiny portion of the window application.

Have you encountered anything like it? I appreciate any pointers/directions, thanks!

screenshot.buffer as .bmp file cannot be opened

code

import {VRect, CaptureScreenshot, GetForegroundWindowHandle} from 'windows-ffi';
import fs from 'fs';
import path from 'path';

async function main() {
    // First capture a screenshot of a section of the screen.
    const screenshot = CaptureScreenshot({
        log: true,
        windowHandle: GetForegroundWindowHandle(), // comment to screenshot all windows
        rectToCapture: new VRect(0, 0, 800, 600)
    });
    console.log('screenshot', screenshot);
    const filePath = path.join('capture.bmp');

    fs.writeFileSync(filePath, screenshot.buffer);
}
main();

result:

.bmp file cannot be opened

CaptureScreen give me always the same ghost image

Hello

I'm at one problem to use your package for my dev.
The problem is that when i try to CaptureScreenshot the foregroung window
Either i get a dark image (when vscode is focused), either i get the same image from my fullscreen game (i can teleport to another place in my game, the screenshot will still be as the first one)
Its seems there is a problem somewhere but i don't know what could cause this

I also check with window detective, nothing is blocking the screenshot for both the game and vscode app

Here is the code used to reproduce the bug

import * as fs from 'fs';

import { Screenshot, VRect } from 'windows-ffi';

import { CaptureScreenshot, GetForegroundWindowHandle } from 'windows-ffi';

import sharp from 'sharp';


let start: number = Date.now();

// First capture a screenshot of a section of the screen.
const screenshot: Screenshot = CaptureScreenshot({
  windowHandle: GetForegroundWindowHandle(), // comment to screenshot all windows
  rectToCapture: new VRect(0, 0, 2560, 1440),
  log: true,
});

console.log(Date.now() - start);
start = Date.now();
const buffer: sharp.Sharp = await sharp(Buffer.from(screenshot.buffer), {
  raw: {
    width: 2560,
    height: 1440,
    channels: 4,
  },
});
fs.writeFileSync('./debug/ffi.jpeg', await buffer.jpeg().toBuffer());
console.log(Date.now() - start);

perhaps i need another/older user32 dll ?

Node version : v19.7.0
Windows 11

Thanks in advance for your help

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.