gpt4 book ai didi

javascript - TB_GETBUTTON 的结构是否正确? (WinXP) (同时检测进程是64位还是32位)

转载 作者:行者123 更新时间:2023-12-03 12:27:32 25 4
gpt4 key购买 nike

我在这里使用 js-ctypes 在 winxp 上使用 winapi 来获取任务栏上按钮索引 1 处的按钮。但是,这里的这一行使我的代码崩溃: var rez = SendMessage(hToolbar, 0x417/** TB_GETBUTTON **/, 1, aButton.address());.我该如何修复才能不崩溃?

我在这里关注本教程:http://w-shadow.com/blog/2006/10/01/manipulating-taskbar-buttons/

以下是将代码复制粘贴到暂存器:

Components.utils.import("resource://gre/modules/ctypes.jsm");

var user32 = ctypes.open('user32.dll');
var kernel = ctypes.open("kernel32.dll");
var HMODULE = ctypes.uint32_t;
var HWND = ctypes.uint32_t;
var LPCTSTR = ctypes.jschar.ptr;
var LPCSTR = ctypes.char.ptr;
var LoadLibrary = kernel.declare("LoadLibraryW", ctypes.winapi_abi, HMODULE, LPCTSTR);
var GetProcAddress = kernel.declare("GetProcAddress", ctypes.winapi_abi, ctypes.void_t.ptr, HMODULE, LPCSTR);

var hUser = LoadLibrary("user32");

/* http://msdn.microsoft.com/en-us/library/windows/desktop/ms633500%28v=vs.85%29.aspx
*/
var funcPtr0 = GetProcAddress(hUser, "FindWindowExW");
var FindWindowExType = ctypes.FunctionType(ctypes.winapi_abi, ctypes.int32_t, [HWND, HWND, LPCTSTR, LPCTSTR]);
funcPtr0 = ctypes.cast(funcPtr0, FindWindowExType.ptr);
//funcPtr0(0, "Test1", "Test2", 0);

var funcPtr1 = GetProcAddress(hUser, "GetTaskmanWindow");
var GetTaskmanWindowType = ctypes.FunctionType(ctypes.winapi_abi, ctypes.int32_t, []);
funcPtr1 = ctypes.cast(funcPtr1, GetTaskmanWindowType.ptr);

var hHwnd = funcPtr1();
var hToolbar = funcPtr0(hHwnd, 0, 'ToolbarWindow32', null);
Services.wm.getMostRecentWindow(null).alert(hToolbar)

var struct_TBButton = ctypes.StructType('TBButton', [
{'iBitmap': ctypes.int},
{'idCommand': ctypes.int},
{'fbState': ctypes.unsigned_char},
{'fsStyle': ctypes.unsigned_char},
{'bReserved': ctypes.unsigned_char},
{'dwData': ctypes.unsigned_long},
{'iString': ctypes.int}
]);


var SendMessage = user32.declare('SendMessageW', ctypes.winapi_abi, ctypes.uintptr_t,
ctypes.int32_t,
ctypes.unsigned_int,
ctypes.int32_t,
ctypes.voidptr_t
);

var Count = SendMessage(hToolbar, 0x418 /** TB_BUTTONCOUNT **/, 0, ctypes.voidptr_t(0));
Services.wm.getMostRecentWindow(null).alert(Count);

var aButton = new struct_TBButton();
var rez = SendMessage(hToolbar, 0x417 /** TB_GETBUTTON **/, 1, aButton.address());
Services.wm.getMostRecentWindow(null).alert(rez);

最佳答案

我没有时间/动机自己调试这个,但有一个明显的错误可能会导致崩溃:您的 struct_TBButton 定义是错误的。

{'bReserved': ctypes.unsigned_char},

对比

#ifdef _WIN64
BYTE bReserved[6];
#else
#if defined(_WIN32)
BYTE bReserved[2];
#endif
#endif

bReserved 在 32 位进程中应为 2 个字节(无符号字符),在 64 位进程中应为 6 个字节。 (这些东西首先存在,以便数据成员正确对齐指针)。

此外,您的指针类型错误,这会在 64 位进程中产生影响。

将它们放在一起(避免使用 ArrayType 因为我很懒):

var struct_TBButton;
if (ctypes.voidptr_t.size == 4 /* 32-bit */) {
struct_TBButton = ctypes.StructType('TBButton', [
{'iBitmap': ctypes.int},
{'idCommand': ctypes.int},
{'fbState': ctypes.unsigned_char},
{'fsStyle': ctypes.unsigned_char},
{'bReserved': ctypes.unsigned_char},
{'bReserved2': ctypes.unsigned_char},
{'dwData': ctypes.uintptr_t},
{'iString': ctypes.intptr_t}
]);
}
else if (ctypes.voidptr_t.size == 8 /* 64-bit */) {
struct_TBButton = ctypes.StructType('TBButton', [
{'iBitmap': ctypes.int},
{'idCommand': ctypes.int},
{'fbState': ctypes.unsigned_char},
{'fsStyle': ctypes.unsigned_char},
{'bReserved': ctypes.unsigned_char},
{'bReserved2': ctypes.unsigned_char},
{'bReserved3': ctypes.unsigned_char},
{'bReserved4': ctypes.unsigned_char},
{'bReserved5': ctypes.unsigned_char},
{'bReserved6': ctypes.unsigned_char},
{'dwData': ctypes.uintptr_t},
{'iString': ctypes.intptr_t}
]);
}
else {
throw new Error("wut?!");
}

console.log(struct_TBButton.size);
// 20 on 32-bit, 32 on 64-bit if I'm not mistaken

PS:其余的指针类型也是如此。 https://stackoverflow.com/a/18040503/484441

例如HWND WINAPI FindWindowEx() == void* WINAPI FindWindowEx(...) 而不是 uint32_t WINAPI FindWindowEx(...)

关于javascript - TB_GETBUTTON 的结构是否正确? (WinXP) (同时检测进程是64位还是32位),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24115480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com