c#用钩子开发金蝶K3外挂的问题
我有个程序需要在金蝶K3中录入一张凭证保存后,弹出一个本程序的一个窗体。
开发中碰到3个问题想请教:
1.使用全局钩子 dwThreadID 传入0 时,如下程序可以成功安装远程(全局)鼠标钩子,
但在 MouseHookProc钩子子程中 MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
MyMouseHookStruct.hWnd 无法得到鼠标点击下的对象的句柄,能过钩子的lParam 是否能得到鼠标下的对象的句柄?
2.如果 dwThreadID 传入外部程序的线程号时,无法成功安装远程局部鼠标钩子(用来只钩特定程序的鼠标消息),hMouseHook 返回0,
我想应该是SetWindowsHookEx第三个参数的问题,远程局部钩子这个参数应该怎么设?
3.k3使用了 ActiveBar 控件,用spy++只能查到界面上保存按钮及其他按钮所在的功能区的句柄,类名为 ABSActiveBarDockWnd,无法得到里面保存按钮的句柄,
这样用貌似无法通过钩子得到保存按钮被按下了。
//声明鼠标钩子的封送结构类型
[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public POINT pt;
public int hWnd;
public int wHitTestCode;
public int dwExtraInfo;
}
public void Start(int dwThreadID)
{
//安装鼠标钩子
if (hMouseHook == 0)
{
//生成一个HookProc的实例.
MouseHookProcedure = new HookProc(MouseHookProc);
hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), dwThreadID);
//如果装置失败停止钩子
if (hMouseHook == 0)
{
Stop();
throw new Exception("SetWindowsHookEx failed. ");
}
}
}
public void Stop()
{
bool retMouse = true;
if (hMouseHook != 0)
{
retMouse = UnhookWindowsHookEx(hMouseHook);
hMouseHook = 0;
}
//如果卸下钩子失败
if (!(retMouse))
throw new Exception("UnhookWindowsHookEx failed. ");
}
private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
//如果正常运行并且用户要监听鼠标的消息
if ((nCode >= 0) && (OnMouseActivity != null))
{
MouseButtons button = MouseButtons.None;
int clickCount = 0;
switch (wParam)
{
case WM_LBUTTONDOWN:
button = MouseButtons.Left;
clickCount = 1;
break;
case WM_LBUTTONUP:
button = MouseButtons.Left;
clickCount = 1;
break;
case WM_LBUTTONDBLCLK:
button = MouseButtons.Left;
clickCount = 2;
break;
case WM_RBUTTONDOWN:
button = MouseButtons.Right;
clickCount = 1;
break;
case WM_RBUTTONUP:
button = MouseButtons.Right;
clickCount = 1;
break;
case WM_RBUTTONDBLCLK:
button = MouseButtons.Right;
clickCount = 2;
break;
}
//从回调函数中得到鼠标的信息
MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
MouseEventArgs e = new MouseEventArgs(button, clickCount, MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y, 0);
OnMouseActivity(this, e);
}
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}
hook --------------------编程问答-------------------- 你应该 用 findwindow 找到 金蝶 的 句柄 --------------------编程问答-------------------- 金碟窗体的句柄是可以得到的,我第三个问题是打窗体中对应按钮的句柄得不到,只能得到一个功能区的句柄。也就无法得到保存按钮什么被按下了。 --------------------编程问答-------------------- 保存按钮在这个功能区里面,功能区是用第三方控件做的,只能得到这个第三方控件的句柄 --------------------编程问答-------------------- 有没有朋友知道 远程局部钩子怎么调用?
补充:.NET技术 , C#