How to use the ref-napi.reinterpretUntilZeros function in ref-napi

To help you get started, we’ve selected a few ref-napi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sindresorhus / active-win / lib / windows.js View on Github external
if (ref.isNull(processHandle)) {
		return undefined; // Failed to get process handle
	}

	// Set the path length to more than the Windows extended-length MAX_PATH length
	const pathLengthBytes = 66000;
	// Path length in "characters"
	const pathLengthChars = Math.floor(pathLengthBytes / 2);
	// Allocate a buffer to store the path of the process
	const processFileNameBuffer = Buffer.alloc(pathLengthBytes);
	// Create a buffer containing the allocated size for the path, as a buffer as it must be writable
	const processFileNameSizeBuffer = ref.alloc('uint32', pathLengthChars);
	// Write process file path to buffer
	kernel32.QueryFullProcessImageNameW(processHandle, 0, processFileNameBuffer, processFileNameSizeBuffer);
	// Remove null characters from buffer
	const processFileNameBufferClean = ref.reinterpretUntilZeros(processFileNameBuffer, wchar.size);
	// Get process file path as a string
	const processPath = wchar.toString(processFileNameBufferClean);
	// Get process file name from path
	const processName = path.basename(processPath);

	// Get process memory counters
	const memoryCounters = new ProcessMemoryCounters();
	memoryCounters.cb = ProcessMemoryCounters.size;
	const getProcessMemoryInfoResult = psapi.GetProcessMemoryInfo(processHandle, memoryCounters.ref(), ProcessMemoryCounters.size);

	// Close the "handle" of the process
	kernel32.CloseHandle(processHandle);
	// Create a new instance of Rect, the struct required by the `GetWindowRect` method
	const bounds = new Rect();
	// Get the window bounds and save it into the `bounds` variable
	const getWindowRectResult = user32.GetWindowRect(activeWindowHandle, bounds.ref());
github sindresorhus / active-win / lib / windows.js View on Github external
if (ref.isNull(activeWindowHandle)) {
		return undefined; // Failed to get active window handle
	}

	// Get memory address of the window handle as the "window ID"
	const windowId = ref.address(activeWindowHandle);
	// Get the window text length in "characters" to create the buffer
	const windowTextLength = user32.GetWindowTextLengthW(activeWindowHandle);
	// Allocate a buffer large enough to hold the window text as "Unicode" (UTF-16) characters (using ref-wchar-napi)
	// This assumes using the "Basic Multilingual Plane" of Unicode, only 2 characters per Unicode code point
	// Include some extra bytes for possible null characters
	const windowTextBuffer = Buffer.alloc((windowTextLength * 2) + 4);
	// Write the window text to the buffer (it returns the text size, but it's not used here)
	user32.GetWindowTextW(activeWindowHandle, windowTextBuffer, windowTextLength + 2);
	// Remove trailing null characters
	const windowTextBufferClean = ref.reinterpretUntilZeros(windowTextBuffer, wchar.size);
	// The text as a JavaScript string
	const windowTitle = wchar.toString(windowTextBufferClean);

	// Allocate a buffer to store the process ID
	const processIdBuffer = ref.alloc('uint32');
	// Write the process ID creating the window to the buffer (it returns the thread ID, but it's not used here)
	user32.GetWindowThreadProcessId(activeWindowHandle, processIdBuffer);
	// Get the process ID as a number from the buffer
	const processId = ref.get(processIdBuffer);
	// Get a "handle" of the process
	const processHandle = kernel32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, processId);

	if (ref.isNull(processHandle)) {
		return undefined; // Failed to get process handle
	}