CallWindowProcA这个函数第一个参数应该怎么写?

lao_5 2017-09-12 08:26:46
CallWindowProcA(
WNDPROC lpPrevWndFunc,
HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam)


SetWindowsHookEx()做一个全局键盘钩子,想用一个键位的按下与抬起的时候执行某个实际的动作,测试用GetAsyncKeyState()是不行的,应该被屏蔽掉了。之前就是用的GetAsyncKeyState()来判断按下弹起,执行鼠标移动到某点,如下这样:

int Hotkey_Bind=VK_LSHIFT;
if(GetAsyncKeyState(Hotkey_Bind)&0x8000)
{
SetCursorPos(Mx, My);
}



我把CallWindowProcA()写在这里:
LRESULT CALLBACK LowLevelkeyboardProc(
int nCode,
WPARAM wParam, // message identifier
LRESULT lParam //keyboard
)
{
if (g_hWnd/*程序窗口句柄*/!=NULL&&nCode==HC_ACTION)
{
::SendMessage(g_hWnd,WM_MYMSG,wParam,lParam);
if (wParam==0x10)
{
CallWindowProcA(ShiftDown,g_hWnd,NULL,NULL,NULL);
/*问题就出在自己想写一个ShiftDown函数,文化不够,一直写不出来*/
}
}
return CallNextHookEx(hhkkeyboard/*SetWindowsHookEx返回*/,nCode,wParam,lParam);
}


结果我在写ShiftDown函数的时候彻底卡壳了,回调函数,我从来就是空白的...
或者请大家帮忙我看看,在设钩子以后怎么做,可以类似上面调用GetAsyncKeyState()的效果呢?
...全文
1984 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
schlafenhamster 2017-09-15
  • 打赏
  • 举报
回复
应该被屏蔽了 , are you sure?
lao_5 2017-09-15
  • 打赏
  • 举报
回复
引用 3 楼 oyljerry 的回复:
实现一个回调函数指针,然后把它做参数传递过去
版大,求您帮忙...
lao_5 2017-09-15
  • 打赏
  • 举报
回复
引用 15 楼 schlafenhamster 的回复:
应该被屏蔽了 , are you sure?
yes,sir
lao_5 2017-09-14
  • 打赏
  • 举报
回复
引用 13 楼 schlafenhamster 的回复:
if(GetKeyState(VK_SHIFT) & 0x80)


schlafenhamster 2017-09-14
  • 打赏
  • 举报
回复
if(GetKeyState(VK_SHIFT) & 0x80)
lao_5 2017-09-14
  • 打赏
  • 举报
回复
引用 10 楼 schlafenhamster 的回复:
LRESULT CALLBACK MyKeyProc( int code, // hook code WPARAM wParam, // virtual-key code LPARAM lParam // keystroke-message information ) { DWORD stay=GetKeyState(VK_HOME); if (stay==0) { TCHAR Buf[20]; wsprintf(Buf,"%x",stay); MessageBox(0,Buf,"",0); if (IsWindowVisible(g_hDlg)) { ShowWindow(g_hDlg,SW_HIDE); } else { ShowWindow(g_hDlg,SW_SHOW); } } // return 1; } // void SetHook() { //HWND hGame=FindWindow("Afx:00400000:b:00010003:01900011:00000000",NULL); g_hGame=FindWindow(0,"GameMFC"); if(g_hGame != 0) { g_threadId=GetWindowThreadProcessId(g_hGame,NULL); g_Hook=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)MyKeyProc,g_hInstance,g_threadId); } } (HOOKPROC)MyKeyProc 就是回调函数 它是 插入到 hook 链中的 ,不需要 用 CallWindowProc
感谢您的及时回复。 我的目的是,用SetWindowsHookEx设一个全局钩子, 钩子处理函数的时候,写一个变量进去shiftkey,值为1弹出信息框,否则,没有反应。大致就是这个意思... 辛苦您了!
if (shiftkey==1)
	{
		AfxMessageBox(_T("成功了"));
	}
lao_5 2017-09-14
  • 打赏
  • 举报
回复
引用 10 楼 schlafenhamster 的回复:
LRESULT CALLBACK MyKeyProc( int code, // hook code WPARAM wParam, // virtual-key code LPARAM lParam // keystroke-message information ) { DWORD stay=GetKeyState(VK_HOME); if (stay==0) { TCHAR Buf[20]; wsprintf(Buf,"%x",stay); MessageBox(0,Buf,"",0); if (IsWindowVisible(g_hDlg)) { ShowWindow(g_hDlg,SW_HIDE); } else { ShowWindow(g_hDlg,SW_SHOW); } } // return 1; } // void SetHook() { //HWND hGame=FindWindow("Afx:00400000:b:00010003:01900011:00000000",NULL); g_hGame=FindWindow(0,"GameMFC"); if(g_hGame != 0) { g_threadId=GetWindowThreadProcessId(g_hGame,NULL); g_Hook=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)MyKeyProc,g_hInstance,g_threadId); } } (HOOKPROC)MyKeyProc 就是回调函数 它是 插入到 hook 链中的 ,不需要 用 CallWindowProc
不会弄...基础太差了。我初步测试了一下,SetWindowsHookEx没有作用...
HWND g_hDlg,g_hGame;
DWORD g_threadId;
HHOOK g_Hook = NULL;
HINSTANCE g_hInstance = NULL;
 LRESULT CALLBACK MyKeyProc(
    int code,       // hook code
    WPARAM wParam,  // virtual-key code
    LPARAM lParam   // keystroke-message information
    )
{
    DWORD stay=GetKeyState(VK_HOME);
 
    if (stay==0)
    {
TCHAR Buf[20];
wsprintf(Buf,"%x",stay);
MessageBox(0,Buf,"",0);
if (IsWindowVisible(g_hDlg))
{
ShowWindow(g_hDlg,SW_HIDE);
} 
else
{
ShowWindow(g_hDlg,SW_SHOW);
}
    }
//
    return 1;
}
//
void SetHook()
{    //HWND hGame=FindWindow("Afx:00400000:b:00010003:01900011:00000000",NULL);
g_hGame=FindWindow(0,"Blood_war");
if(g_hGame != 0)
{
g_threadId=GetWindowThreadProcessId(g_hGame,NULL);
g_Hook=SetWindowsHookEx(13,(HOOKPROC)MyKeyProc,g_hInstance,g_threadId);
}
}
schlafenhamster 2017-09-14
  • 打赏
  • 举报
回复
LRESULT CALLBACK MyKeyProc( int code, // hook code WPARAM wParam, // virtual-key code LPARAM lParam // keystroke-message information ) { DWORD stay=GetKeyState(VK_HOME); if (stay==0) { TCHAR Buf[20]; wsprintf(Buf,"%x",stay); MessageBox(0,Buf,"",0); if (IsWindowVisible(g_hDlg)) { ShowWindow(g_hDlg,SW_HIDE); } else { ShowWindow(g_hDlg,SW_SHOW); } } // return 1; } // void SetHook() { //HWND hGame=FindWindow("Afx:00400000:b:00010003:01900011:00000000",NULL); g_hGame=FindWindow(0,"GameMFC"); if(g_hGame != 0) { g_threadId=GetWindowThreadProcessId(g_hGame,NULL); g_Hook=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)MyKeyProc,g_hInstance,g_threadId); } } (HOOKPROC)MyKeyProc 就是回调函数 它是 插入到 hook 链中的 ,不需要 用 CallWindowProc
lao_5 2017-09-14
  • 打赏
  • 举报
回复
引用 2 楼 zgl7903 的回复:
ShiftDown 是怎么声明,怎么写的?
CallWindowProc 调用还应该传入 message, wParam, lParam 参数吧

而且你的参数似乎也用的不对


LRESULT CALLBACK LowLevelKeyboardProc(
int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // pointer to structure with message data
);

Parameters
nCode
Specifies a code the hook procedure uses to determine how to process the message. This parameter can be one of the following values: Value Meaning
HC_ACTION The wParam and lParam parameters contain information about a keyboard message.


If nCode is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and should return the value returned by CallNextHookEx.

wParam
Specifies the identifier of the keyboard message. This parameter can be one of the following messages:WM_KEYDOWN,WM_KEYUP,WM_SYSKEYDOWN, orWM_SYSKEYUP.
lParam
Pointer to the KBDLLHOOKSTRUCT structure.



钩子已经测试成功了,问题就出在怎么在按下shift键后,执行一个鼠标的移动。
我的思路是模仿VB,写一个叫ShiftDown的子程,结果这个子程写不出来。也许是VC根本不能套用VB的思路。
lao_5 2017-09-14
  • 打赏
  • 举报
回复
引用 5 楼 schlafenhamster 的回复:
例子

WNDPROC	g_OldProc;
LRESULT CALLBACK NewProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
	CPoint pt;
	switch(message) 
	{
	case WM_ACTIVATE:
		ShowCursor(FALSE);
	break;

	case WM_DESTROY:
		ShowCursor(TRUE);
	break;	
	case WM_MOUSEMOVE: 
        pt.x = (int) LOWORD(lParam); 
        pt.y = (int) HIWORD(lParam);
		afxDump << pt << "\n";
	break;	
	} 
	return CallWindowProc(g_OldProc, hwnd, message, wParam, lParam);
}
谢谢您了,我想用SetWindowsHookEx()来实现,您的这个实例我用过,在利用的时候,没有效果。
lao_5 2017-09-14
  • 打赏
  • 举报
回复
引用 3 楼 oyljerry 的回复:
实现一个回调函数指针,然后把它做参数传递过去
版主您好:麻烦您给个实例源码可以吗?我就是卡在回调函数这里了。
schlafenhamster 2017-09-13
  • 打赏
  • 举报
回复
m_hwndF=::CreateWindowEx(WS_EX_TOPMOST|WS_EX_LAYERED,m_lpszClassName,"", WS_VISIBLE | WS_POPUP, 0,0,x_coord,y_coord,0,0,NULL,NULL); g_OldProc = (WNDPROC)SetWindowLong(m_hwndF, GWL_WNDPROC, (LONG)NewProc);
schlafenhamster 2017-09-13
  • 打赏
  • 举报
回复
例子

WNDPROC	g_OldProc;
LRESULT CALLBACK NewProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
	CPoint pt;
	switch(message) 
	{
	case WM_ACTIVATE:
		ShowCursor(FALSE);
	break;

	case WM_DESTROY:
		ShowCursor(TRUE);
	break;	
	case WM_MOUSEMOVE: 
        pt.x = (int) LOWORD(lParam); 
        pt.y = (int) HIWORD(lParam);
		afxDump << pt << "\n";
	break;	
	} 
	return CallWindowProc(g_OldProc, hwnd, message, wParam, lParam);
}
schlafenhamster 2017-09-13
  • 打赏
  • 举报
回复

CallWindowProc
The CallWindowProc function passes message information to the specified window procedure. 

LRESULT CallWindowProc(
  WNDPROC lpPrevWndFunc,  // pointer to previous procedure
  HWND hWnd,              // handle to window
  UINT Msg,               // message
  WPARAM wParam,          // first message parameter
  LPARAM lParam           // second message parameter
);
 
Parameters
lpPrevWndFunc 
Pointer to the previous window procedure. 
If this value is obtained by calling the GetWindowLong function with the nIndex parameter set to GWL_WNDPROC or DWL_DLGPROC, it is actually either the address of a window or dialog box procedure, or a handle representing that address. 

hWnd 
Handle to the window procedure to receive the message. 
Msg 
Specifies the message. 
wParam 
Specifies additional message-specific information. The contents of this parameter depend on the value of the Msg parameter. 
lParam 
Specifies additional message-specific information. The contents of this parameter depend on the value of the Msg parameter. 
Return Values
The return value specifies the result of the message processing and depends on the message sent. 

Remarks
Use the CallWindowProc function for window subclassing. Usually, all windows with the same class share one window procedure. A subclass is a window or set of windows with the same class whose messages are intercepted and processed by another window procedure (or procedures) before being passed to the window procedure of the class. 

The SetWindowLong function creates the subclass by changing the window procedure associated with a particular window, causing the system to call the new window procedure instead of the previous one. An application must pass any messages not processed by the new window procedure to the previous window procedure by calling CallWindowProc. This allows the application to create a chain of window procedures. 

If STRICT is defined, the lpPrevWndFunc parameter has the data typeWNDPROC. The WNDPROC type is declared as follows: 

LRESULT (CALLBACK* WNDPROC) (HWND, UINT, WPARAM, LPARAM); 
 
If STRICT is not defined, the lpPrevWndFunc parameter has the data typeFARPROC. The FARPROC type is declared as follows: 

int (FAR WINAPI * FARPROC) () 
 
In C, the FARPROC declaration indicates a callback function that has an unspecified parameter list. In C++, however, the empty parameter list in the declaration indicates that a function has no parameters. This subtle distinction can break careless code. Following is one way to handle this situation: 

#ifdef STRICT 
  WNDPROC MyWindowProcedure 
#else 
  FARPROC MyWindowProcedure 
#endif 
... 
    lResult = CallWindowProc(MyWindowProcedure, ...) ; 
 
For further information about functions declared with empty argument lists, refer to The C++ Programming Language, Second Edition, by Bjarne Stroustrup. 

Windows NT: The CallWindowProc function handles Unicode-to-ANSI conversion. You cannot take advantage of this conversion if you call the window procedure directly. 

oyljerry 2017-09-13
  • 打赏
  • 举报
回复
实现一个回调函数指针,然后把它做参数传递过去
zgl7903 2017-09-13
  • 打赏
  • 举报
回复
ShiftDown 是怎么声明,怎么写的? CallWindowProc 调用还应该传入 message, wParam, lParam 参数吧 而且你的参数似乎也用的不对

LRESULT CALLBACK LowLevelKeyboardProc(
  int nCode,     // hook code
  WPARAM wParam, // message identifier
  LPARAM lParam  // pointer to structure with message data
);
 
Parameters
nCode 
Specifies a code the hook procedure uses to determine how to process the message. This parameter can be one of the following values: Value Meaning 
HC_ACTION The wParam and lParam parameters contain information about a keyboard message. 


If nCode is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and should return the value returned by CallNextHookEx. 

wParam 
Specifies the identifier of the keyboard message. This parameter can be one of the following messages:WM_KEYDOWN,WM_KEYUP,WM_SYSKEYDOWN, orWM_SYSKEYUP. 
lParam 
Pointer to the KBDLLHOOKSTRUCT structure. 

lao_5 2017-09-12
  • 打赏
  • 举报
回复
我的思路是类似VB的调用方式,用CallWindowProcA的第一个参数写一个函数出来,函数返回某个数值,比如 函数返回action = 1 ,当这个数值出现在if判断action = 1 的时候,可以执行SetCursorPos(Mx,My)到处。

2,640

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧