npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

get-last-error

v1.0.0

Published

Calls the windows `GetLastError` function when failed calling other functions with `ffi-napi` library.

Downloads

62

Readme

getLastError

Calls the windows GetLastError function when failed calling other functions with ffi-napi library.

Unfortunately, you cannot use the async mode of the specific function if you want to get the error code, because the ffi-napi library will also call some apis that will clear the error.

To successfully get the error code, please use the sync mode of the specific function.

This project was born as an experiment to learn C++, so consider that there might be bugs, leaks and so on.

Install

Just issue from the command line:

> npm install getLastError

Note that this project contains native addons, which are prebuilt for the x86 and x64 versions and included in the package. If you want to rebuild, use:

  • Microsoft Visual Studio C++ 2019 (maybe lower, but I didn't test)

Getting started

const ffi = require('ffi-napi');
const getLastError = require("getLastError");

const user32 = ffi.Library("user32.dll", {
	LoadIconA: ["size_t"/* HICON */, [
		"size_t", /* HINSTANCE hInstance  */
		"string", /* LPCSTR    lpIconName */
	]],
	LoadImageA: ["size_t"/* HANDLE */, [
		"size_t", /* HINSTANCE hInst  */
		"string", /* LPCSTR    name   */
		"uint32", /* UINT      type   */
		"int32",  /* int       cx     */
		"int32",  /* int       cy     */
		"uint32", /* UINT      fuLoad */
	]],
});

if (0 === user32.LoadIconA(0, "notExists.ico")) {
	console.log(getLastError(16, true));	// 0x715
}

if (0 === user32.LoadImageA(0, "notExists.ico", IMAGE_ICON = 1, 16, 16, (LR_LOADFROMFILE = 0x10) | (LR_LOADTRANSPARENT = 0x20))) {
	console.log(getLastError(16));	// 2
}

// Do NOT use async, because the `ffi-napi` library will also call some apis that will clear the error.
user32.LoadImageA.async(0, "notExists.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE | LR_LOADTRANSPARENT, (err, r) => {
	if (0 === r) {
		console.log("you will never get the correct error code in callback mode:", getLastError(16));
	}
});

API

Syntax

getLastError(base, addPrefix);

Parameters

  • base [optional] {number}. Number base(1 ~ 36) of the return value. Defaults to 10.
  • addPrefix [optional] {boolean}. Adds 0x if base is 16, 0o if 8, and 0b if 2. Defaults to false.

Returns

Type: string|number

The last error code calling windows api, in the specific number base.