这里展示了如何通过 jsctypes 使用不同的函数。它可能不适用于您上面的内容,如果这对您没有帮助,请告诉我,我会看看您的具体情况。
const {Cc, Ci, Cu} = require('chrome');
Cu.import("resource://gre/modules/ctypes.jsm");
/*start getcursorpos*/
var lib = ctypes.open("user32.dll");
/*foreground window stuff*/
var FindWindowA = lib.declare('FindWindowA', ctypes.winapi_abi, ctypes.uint32_t, ctypes.jschar.ptr, ctypes.jschar.ptr)
var GetForegroundWindow = lib.declare('GetForegroundWindow', ctypes.winapi_abi, ctypes.uint32_t)
function doFindWindow() {
var wm = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator);
var title = wm.getMostRecentWindow('navigator:browser').gBrowser.contentDocument.title;
Cu.reportError('title=' + title)
var ret = FindWindowA('', title + ' - Mozilla Firefox');
//var ret = GetForegroundWindow();
Cu.reportError(ret);
}
/*end foreground window stuff*/
/* Declare the signature of the function we are going to call */
const struct_lpPoint = new ctypes.StructType("lpPoint",
[ { "x": ctypes.int },
{ "y": ctypes.int } ]);
var GetCursorPos = lib.declare('GetCursorPos', ctypes.winapi_abi, ctypes.bool, struct_lpPoint.ptr);
function doGetCursorPos() {
var point = new struct_lpPoint;
var ret = GetCursorPos(point.address());
Cu.reportError(ret);
Cu.reportError(point);
}
/*end getcursorpos*/
/*start setcursorpos*/
//var lib = ctypes.open("user32.dll"); //already called on line 4
var SetCursorPos = lib.declare('SetCursorPos', ctypes.winapi_abi, ctypes.bool, ctypes.int, ctypes.int)
function doSetCursorPos() {
var ret = SetCursorPos(10, 10);
}
/*end setcursorpos*/
/*start mouse_event*/
//used to click
//const DWORD = ctypes.uint32_t; //this just shows you that DWORD == ctypes.uint32_t
var mouse_event = lib.declare('mouse_event', ctypes.winapi_abi, ctypes.void_t, ctypes.uint32_t, ctypes.uint32_t, ctypes.uint32_t, ctypes.uint32_t, ctypes.uintptr_t);
const MOUSEEVENTF_LEFTDOWN = 2;
const MOUSEEVENTF_LEFTUP = 4;
function domouse_event() {
var ret = mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
var ret = mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
/*end mouse_event*/
/*start MessageBoxW*/
//var lib = ctypes.open("user32.dll"); //already called on line 4
var MessageBoxW = lib.declare('MessageBoxW', ctypes.winapi_abi, ctypes.int32_t, ctypes.int32_t, ctypes.jschar.ptr, ctypes.jschar.ptr, ctypes.int32_t);
var MB_OK = 0;
function doMessageBoxW() {
var ret = MessageBoxW(0, "Hello world", "title", MB_OK);
}
/*end MessageBoxW*/
exports.main = function (options, callbacks) {
};
exports.onUnload = function (reason) {
lib.close();
}
var { Hotkey } = require("hotkeys");
var showHotKey = Hotkey({
combo: "alt-w",
onPress: function() {
/*setcursor stuff*/
//doSetCursorPos();
//domouse_event();
/*setcursor stuff*/
/*foreground stuff*/
doFindWindow();
/*foreground stuff*/
}
});
我发现关于stackoverflow的其他jsctype主题很有用:* Javascript String to C++ char pointer -JSCTypes中的LPSTR缓冲区
* FF插件:如何在javascript中声明C函数fgets