### Initializing Repository with npm Source: https://github.com/waitingsong/node-win32-api/blob/main/docs/index.html This command initializes the project repository, typically used for setting up development environments, installing root dependencies, or running initial build scripts as defined in the project's package.json. ```bash npm run repo:init ``` -------------------------------- ### Installing win32-api Package with npm Source: https://github.com/waitingsong/node-win32-api/blob/main/packages/win32-api/README.zh-CN.md This command installs the `win32-api` package from the npm registry, making it available for use in a Node.js project. It's the standard way to add the library as a runtime dependency to your application. ```sh npm install win32-api ``` -------------------------------- ### Installing win32-api with npm Source: https://github.com/waitingsong/node-win32-api/blob/main/README.md This command installs the `win32-api` package using npm, making it available for use in Node.js projects. It is the standard way to add the library as a dependency to your project. ```sh npm install win32-api ``` -------------------------------- ### Installing win32-api Package with npm Source: https://github.com/waitingsong/node-win32-api/blob/main/docs/index.html This command installs the 'win32-api' package from the npm registry into your Node.js project. It adds the library as a dependency, allowing you to import and use its Win32 API definitions. ```bash npm install win32-api ``` -------------------------------- ### Initializing Project Dependencies with npm Source: https://github.com/waitingsong/node-win32-api/blob/main/packages/win32-api/README.zh-CN.md This command runs the `bootstrap` script defined in the `package.json` to initialize project dependencies, typically used in monorepos managed by tools like Lerna. It prepares the project for development by installing all necessary packages across different sub-packages. ```sh npm run bootstrap ``` -------------------------------- ### Installing win32-api with npm Source: https://github.com/waitingsong/node-win32-api/blob/main/packages/win32-api/README.md This command installs the `win32-api` package using npm, making the Windows API FFI definitions available for use in a Node.js project. It is the primary method for integrating the library into a development environment. ```Shell npm install win32-api ``` -------------------------------- ### Installing win32-def package with npm Source: https://github.com/waitingsong/node-win32-api/blob/main/packages/win32-def/README.md This command installs the `win32-def` package, which provides essential Windows API definitions, into your Node.js project. It is a prerequisite for defining and calling native Win32 functions using the koffi FFI library. ```sh npm install win32-def ``` -------------------------------- ### Installing win32-api Package with npm Source: https://github.com/waitingsong/node-win32-api/blob/main/README.zh-CN.md This command installs the 'win32-api' package, making the Windows API interfaces available for use in your Node.js project. ```sh npm install win32-api ``` -------------------------------- ### Defining and Using Structures with ref-struct-di Source: https://github.com/waitingsong/node-win32-api/blob/main/docs/index.html This example showcases an alternative approach to defining and using C-style structures with `ref-struct-di`. This library allows for dependency injection of `ref-napi`, providing more flexibility. It demonstrates creating and populating a `POINT` structure similar to `ref-struct`. ```TypeScript // struct usage with ref-struct-di import * as ref from 'ref-napi' import * as StructDi from 'ref-struct-di' import { DModel as M, DStruct as DS } from 'win32-api' const Struct = StructDi(ref) const point: M.POINT_Struct = new Struct(DS.POINT)() point.x = 100 point.y = 200 console.log(point) ``` -------------------------------- ### Calling Win32 API Functions Asynchronously with TypeScript Source: https://github.com/waitingsong/node-win32-api/blob/main/packages/win32-def/README.md This example demonstrates how to load the defined Win32 API functions and make an asynchronous call. It uses `load` from `win32-def` to get an instance of the Win32 API library and then calls `GetCursorPos_Async` to retrieve the current cursor coordinates, asserting the success of the operation. ```ts import { load } from 'win32-def' import { POINT_Factory } from 'win32-def/struct' const lib = load(options) const { payload: pos } = POINT_Factory() const res = await lib.GetCursorPos_Async(pos) assert(res > 0) console.info({ res, pos }) assert(pos.x >= 0 && pos.y >= 0) ``` -------------------------------- ### Importing and Using Win32 API Functions in TypeScript Source: https://github.com/waitingsong/node-win32-api/blob/main/packages/win32-api/README.md This TypeScript example demonstrates how to import specific Win32 API functions like `FindWindow` and `GetDefaultPrinter` from the `win32-api/util` module. It shows asynchronous calls to retrieve the default printer name and find a Notepad window using `FindWindowEx` after spawning the application. ```TypeScript import { FindWindow, GetDefaultPrinter, } from 'win32-api/util' // Retrieves the printer name of the default printer for the current user on the local computer const printerName = await GetDefaultPrinter() const child = spawn('notepad.exe') const hWnd = await FindWindowEx(0, 0, 'Notepad', null) ``` -------------------------------- ### Defining Win32 API Methods with Auto-Created Structures (TypeScript) Source: https://github.com/waitingsong/node-win32-api/blob/main/CHANGES.v22.md This snippet demonstrates how to define Win32 API methods using the 'win32-api' library, leveraging 'koffi' for FFI. It shows the definition of methods like ClientToScreen, EnumDisplayDevicesW, and GetCursorPos, highlighting that complex structures like LPPOINT and LPDISPLAY_DEVICEW are automatically created when the library is loaded, simplifying the FFI setup. ```TypeScript import * as D from '##/index.def.js' import * as S from '##/index.struct.js' import * as T from '##/index.types.js' class DefWin { static ClientToScreen = [D.BOOL, [D.HWND, `_Inout_ ${S.LPPOINT}`]] // S.LPPOINT == 'POINT*' static EnumDisplayDevicesW = [D.BOOL, [D.LPCWSTR, D.DWORD, `_Inout_ ${S.LPDISPLAY_DEVICEW}`, D.DWORD]] // S.LPDISPLAY_DEVICEW == 'DISPLAY_DEVICEW*' static GetCursorPos = [D.BOOL, [`_Out_ ${S.LPPOINT}`]] } ``` -------------------------------- ### Calling Win32 API Functions in TypeScript Source: https://github.com/waitingsong/node-win32-api/blob/main/README.md This TypeScript example demonstrates how to import and use `win32-api` utility functions. It shows asynchronous calls to retrieve the default printer name using `GetDefaultPrinter()` and to find a window handle for an application like Notepad using `FindWindowEx()`, illustrating direct interaction with the Windows API. ```ts import { FindWindow, GetDefaultPrinter, } from 'win32-api/util' // Retrieves the printer name of the default printer for the current user on the local computer const printerName = await GetDefaultPrinter() const child = spawn('notepad.exe') const hWnd = await FindWindowEx(0, 0, 'Notepad', null) ``` -------------------------------- ### Calling Win32 API with ref-napi and win32-api Source: https://github.com/waitingsong/node-win32-api/blob/main/docs/index.html This example illustrates how to load a Windows DLL (kernel32.dll) using `win32-api`, allocate memory for a pointer using `ref.alloc` with predefined Windows data types, and then call a native Win32 API function (`GetModuleHandleExW`) to retrieve a module handle. It also shows how to read the returned handle value from the buffer. ```TypeScript import * as ref from 'ref-napi' import { K, DTypes as W } from 'win32-api' const knl32 = K.load() const lpszClass = Buffer.from('guard64\0', 'ucs2') const hInstanceBuffer = ref.alloc(W.HANDLE_PVOID) const hInstanceAddr = ref.address(hInstanceBuffer) knl32.GetModuleHandleExW(0, lpszClass, hInstanceAddr) // console.log(hInstanceBuffer) console.log(hInstanceBuffer.readInt32LE(0)) // -> 1621360640 (60A40000) console.log(hInstanceBuffer.readBigUInt64LE()) // -> 140734814748672n (7FFF60A40000) ``` -------------------------------- ### Using win32-api for Windows API Calls in TypeScript Source: https://github.com/waitingsong/node-win32-api/blob/main/README.zh-CN.md This TypeScript snippet demonstrates how to import specific Windows API functions like FindWindow, GetDefaultPrinter, and FindWindowEx from 'win32-api/util'. It shows examples of retrieving the default printer name and finding a Notepad window by its class name. ```ts import { FindWindow, GetDefaultPrinter, } from 'win32-api/util' // 获取当前电脑当前用户默认打印机名 const printerName = await GetDefaultPrinter() const child = spawn('notepad.exe') const hWnd = await FindWindowEx(0, 0, 'Notepad', null) ``` -------------------------------- ### Finding and Setting Window Title with Win32 API in Node.js Source: https://github.com/waitingsong/node-win32-api/blob/main/docs/index.html This Node.js (TypeScript) example demonstrates how to use the 'win32-api' library to interact with Windows native functions. It first loads the Kernel32 and User32 DLL APIs, then finds a running Calculator window by its title using 'FindWindowExW', and subsequently changes its title using 'SetWindowTextW'. This requires a calculator program to be running manually beforehand. ```typescript // **Find calc's hWnd, need running a calculator program manually at first** /** * exposed modules: * C, Comctl32 for Comctl32 from lib/comctl32/api * K, Kernel32 for kernel32 from lib/kernel32/api * U, User32 for user32 from lib/user32/api */ import { K, U } from 'win32-api' import * as ref from 'ref-napi' const knl32 = K.load() const user32 = U.load() // load all apis defined in lib/{dll}/api from user32.dll // const user32 = U.load(['FindWindowExW']) // load only one api defined in lib/{dll}/api from user32.dll const title = 'Calculator\0' // null-terminated string // const title = '计算器\0' // null-terminated string 字符串必须以\0即null结尾! const lpszWindow = Buffer.from(title, 'ucs2') const hWnd = user32.FindWindowExW(0, 0, null, lpszWindow) if (typeof hWnd === 'number' && hWnd > 0 || typeof hWnd === 'bigint' && hWnd > 0 || typeof hWnd === 'string' && hWnd.length > 0 ) { console.log('buf: ', hWnd) // Change title of the Calculator const res = user32.SetWindowTextW(hWnd, Buffer.from('Node-Calculator\0', 'ucs2')) if ( ! res) { console.log('SetWindowTextW failed') } else { console.log('window title changed') } } ``` -------------------------------- ### Asynchronously Find and Set Window Title in Node.js Source: https://github.com/waitingsong/node-win32-api/blob/main/docs/index.html This snippet demonstrates how to asynchronously find a running calculator window by its class name ('CalcFrame') and then change its title to 'Node-Calculator' using the 'win32-api' library. It verifies the title change by retrieving the new title. This example requires a calculator program to be running manually before execution. ```typescript // **Find calc's hWnd, need running a calculator program manually at first** import { U } from 'win32-api' import * as ref from 'ref-napi' const u32 = U.load(['FindWindowExW', 'SetWindowTextW']) const lpszClass = Buffer.from('CalcFrame\0', 'ucs2') u32.FindWindowExW.async(0, 0, lpszClass, null, (err, hWnd) => { if (err) { throw err } if (typeof hWnd === 'number' && hWnd > 0 || typeof hWnd === 'bigint' && hWnd > 0 || typeof hWnd === 'string' && hWnd.length > 0 ) { const title = 'Node-Calculator' // Change title of the Calculator u32.SetWindowTextW.async(hWnd, Buffer.from(title + '\0', 'ucs2'), err2 => { if (err2) { throw err2 } const buf = Buffer.alloc(title.length * 2) u32.GetWindowTextW.async(hWnd, buf, buf.byteLength, err3 => { if (err3) { throw err3 } const str = buf.toString('ucs2').replace(/\0+$/, '') if (str !== title) { throw new Error(`title should be changed to ${title}, bug got ${str}`) } }) }) } else { throw new Error('FindWindowExW() failed') } }) ``` -------------------------------- ### Defining Winspool API Methods with Multiple Choice Parameters (TypeScript) Source: https://github.com/waitingsong/node-win32-api/blob/main/CHANGES.v22.md This example illustrates the implementation of multiple choice parameters for a single API argument, specifically for the 'GetPrinterW' method. It shows how to define an array of possible structure types for an output parameter, requiring the 'as const' assertion. The snippet also demonstrates the corresponding synchronous and asynchronous type declarations for the method, emphasizing the need for a dedicated mapper function to handle the multiple choices at runtime. ```TypeScript import * as D from 'win32-def/def' import * as S from 'win32-def/struct' import * as T from 'win32-def/types' export class DefWinspool implements T.LibDefBase { static GetPrinterW = [D.BOOL, [ D.HANDLE, D.DWORD, // multiple choice instead of `_Out_ ${D.LPBYTE}`, [ `_Out_ ${S.PPRINTER_INFO_1}`, `_Out_ ${S.PPRINTER_INFO_4}`, `_Out_ ${S.PPRINTER_INFO_5}`, `_Out_ ${S.PPRINTER_INFO_6}`, `_Out_ ${S.PPRINTER_INFO_8}`, `_Out_ ${S.PPRINTER_INFO_9}`, ], D.DWORD, `_Out_ ${D.LPDWORD}`, ]] as const // `as const` is required for multipleChoice } export class Winspool implements T.LibDef2Type { GetPrinterW: ( hPrinter: T.HANDLE, Level: T.DWORD, pPrinter: S.PRINTER_INFO_X_Type, // multiple choice cbBuf: T.DWORD, pcbNeeded: T.LPDWORD, ) => T.BOOL // You should declare async one only for generics GetPrinterW_Async: ( hPrinter: T.HANDLE, Level: T.DWORD, pPrinter: S.PRINTER_INFO_X_Type, // multiple choice cbBuf: T.DWORD, pcbNeeded: T.LPDWORD, ) => Promise } ``` -------------------------------- ### Initializing Project Dependencies with npm Source: https://github.com/waitingsong/node-win32-api/blob/main/README.zh-CN.md This command is used to set up the project's dependencies, typically in a monorepo structure managed by Lerna, ensuring all packages are linked and ready for development. ```sh npm run bootstrap ``` -------------------------------- ### Dynamically Loading Search Script for Local Access Source: https://github.com/waitingsong/node-win32-api/blob/main/docs/globals.html This JavaScript snippet checks if the documentation is being accessed via the 'file:' protocol. If so, it dynamically writes a script tag to load 'search.js', enabling search functionality when browsing the documentation locally. ```JavaScript if (location.protocol == 'file:') document.write('