question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Need help. How to use a Custom Struct with EnumWindows?

See original GitHub issue

My code:

const EnumParams = Struct({
	uiMsg:  W.UINT,
	wParam: W.WPARAM,
	lParam: W.LPARAM
});

var EP = new EnumParams;
	
EP.uiMsg = 0;
EP.wParam = 0;
EP.lParam = 42;

const WndEnumProc = ffi.Callback(
    W.BOOL, [W.HWND, ref.refType(EnumParams)],
    (hwnd, ep) => {
        // HERE I AM UNABLE TO USE ep members! How To?
        return 1;
    }
);

user32.EnumWindows( WndEnumProc, EP.ref() );

In the WndEnumProc callback, ep.uiMsg is undefined How to declare the callback, how to call EnumWindows passing a pointer to my custom struct, and finally how to use the menbers in the callback?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
somanuellcommented, Jun 22, 2022

No. I use a 32 bits node.exe, so that’s 4 and 32.

1reaction
somanuellcommented, Jun 22, 2022

First Working Version without an EnumParams allocation for each enumerated window:

const { DStruct: DS, DTypes: W, U } = require('win32-api');
const user32 = U.load(); 
const ffi = require('ffi-napi');
const ref = require('ref-napi');
const StructDi = require('ref-struct-di');
const Struct = StructDi(ref);

const EnumParams = Struct({
	uiMsg:  W.UINT,
	wParam: W.WPARAM,
	lParam: W.LPARAM
});

var EP = new EnumParams;
EP.uiMsg = 0;
EP.wApram = 0;
EP.lParam = 42;

const refType = EP.ref().ref().type;

const WndEnumProc = ffi.Callback(
	W.BOOL,	[W.HWND, W.LPARAM],
	(hwnd, ep) => {
		const buf = Buffer.alloc(4);
		buf.writeInt32LE(ep, 0);
		buf.type = refType;
		const EPObject = buf.deref().deref();
		console.log( 'EPObject.lParam is: ' + EPObject.lParam );
		console.log('First enumerated window is: ' + hwnd);
		return 0;
	}
);

user32.EnumWindows( WndEnumProc, EP.ref().address() );

node.exe testew.js gives:

EPObject.lParam is: 42
First enumerated window is: 196734

There is still a Buffer.alloc(4); at the beginning of the callback…

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - How to use a custom struct with EnumWindows in the ...
With help from the author of win32-api , I now have a working version: const { DStruct: DS, DTypes: W, U } =...
Read more >
Storing the handles recieved by enumwindowproc in a struct
Hello ! I have a question using the EnumWindows function. What I am trying to do: I want to call EnumWindows and subsequently...
Read more >
enumwindows (user32): pinvoke.net
You can do this by: Defining a class to hold your custom data, and; Using a ref to that class in the EnumWindows()...
Read more >
FFI: Converting function/closure to isize and back - help
I have a rust function that uses the windows crate to enumerate all currently open windows. Depending on the window class and title...
Read more >
FFI Semantics - LuaJIT
These conversion rules apply for read accesses to C types: indexing pointers, ... Aggregate types (arrays and structs) accept either a single cdata ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found