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.

Can't use FindWindow from user32 library

See original GitHub issue

Hi

I’m trying to call FindWindow from user32.dll, i got working perfectly an example that shows a MessageBox but with this code nodejs crashed and I can’t figure out why

var user32 = new FFI.Library('user32', {
    'FindWindow': [
      'pointer', [ 'string', 'string']
    ]
    });

    var process = user32.FindWindow(NULL,"Calculadora")
    console.log("Process ID", process)

Error:

Error: Dynamic Symbol Retrieval Error: "FindWindow": Controlador no v�lido.
    at DynamicLibrary.get (C:\Program Files (x86)\nodejs\node_modules\node-ffi\lib\dynamic_library.js:81:11)
    at C:\Program Files (x86)\nodejs\node_modules\node-ffi\lib\library.js:19:21
    at Array.forEach (native)
    at new Library (C:\Program Files (x86)\nodejs\node_modules\node-ffi\lib\library.js:18:24)
    at C:\Program Files (x86)\nodejs\diablo.js:18:18
    at callbacks (C:\Program Files (x86)\nodejs\node_modules\express\lib\router\index.js:272:11)
    at param (C:\Program Files (x86)\nodejs\node_modules\express\lib\router\index.js:246:11)
    at pass (C:\Program Files (x86)\nodejs\node_modules\express\lib\router\index.js:253:5)
    at Router._dispatch (C:\Program Files (x86)\nodejs\node_modules\express\lib\router\index.js:280:4)
    at Object.handle (C:\Program Files (x86)\nodejs\node_modules\express\lib\router\index.js:45:10)

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
SkaceKamencommented, Mar 15, 2017

For anyone coming from google (like me): utf-16 requires 2 NULLs at the end, so correct implementation is (updated to work with newer ffi):

var lpctstr = {
    name: 'lpctstr',
    indirection: 1,
    size: ref.sizeof.pointer,
    get: function(buffer, offset) {
        var _buf = buffer.readPointer(offset);
        if(_buf.isNull()) {
            return null;
        }
        return _buf.readCString(0);
    },
    set: function(buffer, offset, value) {
        var _buf = new Buffer(Buffer.byteLength(value, 'ucs2') + 2)
        _buf.write(value, 'ucs2')
        _buf[_buf.length - 2] = 0
        _buf[_buf.length - 1] = 0
        return buffer.writePointer(_buf, offset)
    },
    ffi_type: ffi.types.CString.ffi_type
}
0reactions
TooTallNatecommented, May 29, 2012

Well with node-ffi v1.0, you can define a Ucs2String type and define your ffi’d functions with that as an argument type. That way you could pass in regular JS strings to the function and node-ffi would create the proper Buffer with the string written to it.

Something like this should work:

var Ucs2String = {
  indirection: 1,
  size: ref.sizeof.pointer,
  get: function (buffer, offset) {
    var _buf = buffer.readPointer(offset)
    // TODO: a *real* way to detect the end of the ucs2 string. this is a band-aid...
    return _buf.reinterpret(10000).toString('ucs2')
  },
  set: function (buffer, offset, string) {
    var _buf = new Buffer(Buffer.byteLength(string, 'ucs2') + 1)
    _buf.write(string, 'ucs2')
    _buf[_buf.length - 1] = 0
    return buffer.writePointer(_buf, offset)
  }
}

var user32 = ffi.Library('user32', {
  'FindWindowW': [
    'pointer', [ Ucs2String, Ucs2String ]
  ]
})

user32.FindWindowW(null, 'Calculadora')
Read more comments on GitHub >

github_iconTop Results From Across the Web

windows - FindWindow() not working - Stack Overflow
I want to do this using FindFindow form the user32.dll but i cant get it run. Weird Thing is that even if i...
Read more >
FindWindowA function (winuser.h) - Win32 - Microsoft Learn
Retrieves a handle to the top-level window whose class name and window name match the specified strings. This function does not search child ......
Read more >
unable to call windows API "FindWindow" and errors in using ...
I want to use the windows api of "FindWindow" to get the handle of an opened window. The following is the code. ;...
Read more >
FindWindow API function does not work if only the class name ...
If you try to use the FindWindow API function, passing only the class name and an ... Declare Function FindWindow Lib "user32" Alias...
Read more >
Windows API Call - FindWindow Not Working - Xojo Forum
Thank you! Declare Function FindWindow Lib “user32” Alias “FindWindowA” (strClassName As CString, strWindowName As CString) As Integer Var ...
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