6

要求是在当前桌面中打开所有窗口。我正在尝试从 node-ffi 调用 EnumWindows,它将处理程序长度设为 0。

节点参考链接:node-ffi 模块

我的代码片段:

/**
@Name :Parthasarathy Balakrishnan
@Version : V0.1
@Date : 18/03/2013 */

var ffi = require('ffi'),
ref = require('ref'),
int = ref.types.int,
assert = require('assert'),
bindings = require('bindings'),
buffer = require('buffer')

var user32,Kernel32;
var lpEnumFunc;
var invokeCount=0;

/**EnumWindows API CALL
    BOOL WINAPI EnumWindows(_In_ WNDENUMPROC lpEnumFunc,_In_ LPARAM lParam);
    Parameters
    lpEnumFunc [in]
        Type: WNDENUMPROC
        A pointer to an application-defined callback function. For more information, see EnumWindowsProc.
    lParam [in]
        Type: LPARAM
        An application-defined value to be passed to the callback function.
**/
user32 = new ffi.Library('user32', {'EnumWindows':[ 'bool', ['pointer','int32'] ], // BOOL WINAPI EnumWindows(_In_ WNDENUMPROC lpEnumFunc,_In_ LPARAM lParam);
    'GetWindowTextW':[ 'int32', ['pointer','pointer','int32']]});

Kernel32 = new ffi.Library('kernel32', {'GetLastError':['bool', ['pointer','int32'] ]}); // Not required for this demo

lpEnumFunc = ffi.Callback('bool',['pointer','int32'],function (hwnd,lParam){ 
    console.log("------------------START---------------------") 
    console.log(hwnd);
    console.log(ref.getType(hwnd));
    console.log("Is Buffer/Pointer NULL :\t"+ref.isNull(hwnd));
    console.log("lParam :\t"+lParam)
    console.log("EnumWindows Callback handler : \t "+hwnd.length);
    //Pointer implementations-start
    var buf = new Buffer(ref.sizeof.pointer);
    ref.writePointer(buf, 0, hwnd);
    var out = ref.readPointer(buf, 0, hwnd.length)
    for (var i = 0, l = out.length; i < l; i++) {
        console.log(out[i])
    }
    //Pointer implementations-end
    console.log("ref address :\t"+ref.address(hwnd));
    console.log("------------------END---------------------")

    return true;
});

console.log("Calling EnumWindows init");
var bool = user32.EnumWindows(lpEnumFunc,0);
console.log("EnumWindows return value :\t"+bool);
// register the callback function

process.on('uncaughtException', function () {
    console.error('uncaught:', arguments);
});

输出:

Calling EnumWindows init

------------------START---------------------
         { size: 0,
            indirection: 1,
            get: [Function: get],
            set: [Function: set],
            name: 'void',
            ffi_type: }

            Is Buffer/Pointer NULL : false
            lParam : 0
            EnumWindows Callback handler : 0
            ref address : 197424

------------------END---------------------

从输出我得到处理程序大小为 0。

我究竟做错了什么?

4

2 回答 2

4

我不相信任何这段代码,它只是这里的另一个答案,修复然后转换为常规 Javascript 而不是 Coffee Script

var ref = require('ref');
var ffi = require('ffi');

var voidPtr = ref.refType(ref.types.void);
var stringPtr = ref.refType(ref.types.CString);

var user32 = ffi.Library('user32.dll', {
    EnumWindows: ['bool', [voidPtr, 'int32']],
    GetWindowTextA : ['long', ['long', stringPtr, 'long']]
});

windowProc = ffi.Callback('bool', ['long', 'int32'], function(hwnd, lParam) {
  var buf, name, ret;
  buf = new Buffer(255);
  ret = user32.GetWindowTextA(hwnd, buf, 255);
  name = ref.readCString(buf, 0);
  console.log(name);
  return true;
});

user32.EnumWindows(windowProc, 0);
于 2016-06-09T23:33:07.520 回答
2

您似乎将“hwnd”视为指针,认为您可以简单地使用 int 或 long。这对我有用(咖啡脚本):

ref = require 'ref'
ffi = require 'ffi

voidPtr = ref.refType(ref.types.void)
stringPtr = ref.refType(ref.types.CString)

bindings =
    EnumWindows: ['bool', [voidPtr, 'int32']]
    GetWindowTextA : ['long', ['long', stringPtr, 'long']]

user32 = ffi.Library('user32.dll', bindings)

windowProc = ffi.Callback 'bool', ['long', 'int32'], (hwnd, lParam) ->
    buf = new Buffer 255
    ret = libm.GetWindowTextA hwnd, buf, 255
    name = ref.readCString(buf, 0)
    console.log name
    true

user32.EnumWindows windowProc, 0
于 2015-01-24T15:28:22.317 回答