dll中共享数据

 #pragma data_seg("MySec")
HWND g_hWnd=NULL;//共享的数据
#pragma data_seg()

#pragma comment(linker,"/section:MySec,RWS")

def文件中
LIBRARY "worddll"
EXPORTS
SetHook @1
SECTIONS
MySec READ WRITE SHARED

附加拦截键盘的代码
#include "stdafx.h"
#include "worddll.h"
#include <atlbase.h>
#include <atlwin.h>

#pragma data_seg("MySec")
HWND g_hWnd=NULL;
#pragma data_seg()

#pragma comment(linker,"/section:MySec,RWS")
HHOOK g_hKeyboard=NULL;
//HWND g_hWnd=NULL;
//HHOOK g_hKeyboard=NULL;
const int UM_PROGRESS=WM_USER + 100;//在dll中自定义消息,要和主程序中的自定义消息一致
LRESULT CALLBACK KeyboardProc(
							  int code,       // hook code
							  WPARAM wParam,  // virtual-key code
							  LPARAM lParam   // keystroke-message information
							  )
{
	if('1'==wParam && (1==(lParam>>29 &1)))
	{
		//SendMessage(g_hWnd,WM_CLOSE,0,0);
		//UnhookWindowsHookEx(g_hKeyboard);
		//CallNextHookEx(g_hKeyboard,KeyboardProc,wParam,lParam);	
		SendMessage(g_hWnd,UM_PROGRESS,0,0);		
	//	SendMessage(UM_PROGRESS);
		//GetModuleHandle()
		//UnhookWindowsHookEx(g_hMouse);
		//MessageBox(NULL,"text","test",MB_OK );
		return 1;
	}else{
		return CallNextHookEx(g_hKeyboard,code,wParam,lParam);
	}	
}

void SetHook(HWND hwnd)
{
	g_hWnd=hwnd;
	//g_hMouse=SetWindowsHookEx(WH_MOUSE,MouseProc,GetModuleHandle("Hook"),0);
	//g_hKeyboard=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle(_T("gjlworddll")),0);
	g_hKeyboard=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle(_T("gjlworddll")),0);
	//g_hKeyboard=SetWindowsHookEx(WH_KEYBOARD_LL,LowLevelKeyboardProc,GetModuleHandle(NULL),0);
}

你可能感兴趣的:(dll,callback,library,hook,keyboard,linker)