首页
社区
课程
招聘
[求助]dll 创建窗口问题
发表于: 2010-3-5 14:37 4321

[求助]dll 创建窗口问题

2010-3-5 14:37
4321
下面代码编译成dll 我用另一个程序 LoadLibrary 123.dll 会创建窗口,用热键也可以
但是把这个 dll 注入到其他程序则不能创建窗口,注入后 MessageBox(NULL,"DLL_PROCESS_ATTACH",NULL,MB_OK); 都会提示
请问是为什么?
// 123.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "resource.h"


#define WH_KEYBOARD_LL 13
typedef struct tagKBDLLHOOKSTRUCT {
	DWORD vkCode;
	DWORD scanCode;
	DWORD flags;
	DWORD time;
	DWORD dwExtraInfo;	
} KBDLLHOOKSTRUCT, FAR *LPKBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;


HINSTANCE hinst = NULL;
HWND hwndDLG = NULL;
HHOOK hhook = NULL;
HANDLE hProc;


LRESULT CALLBACK mykeyproc(int,WPARAM,LPARAM);
void WINAPI start();
BOOL CALLBACK DlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam);    
extern "C" __declspec(dllexport) void ShowDlg();

//dll入口
BOOL APIENTRY DllMain( HANDLE hModule, 
					  DWORD  ul_reason_for_call, 
					  LPVOID lpReserved
					  )
{
	switch( ul_reason_for_call )
	{
	case DLL_PROCESS_ATTACH:
		hinst = (HINSTANCE)hModule;
		start();
		MessageBox(NULL,"DLL_PROCESS_ATTACH",NULL,MB_OK);
		break;
	case DLL_THREAD_ATTACH:
		break;
	case DLL_THREAD_DETACH:
		break;
	case DLL_PROCESS_DETACH:
		MessageBox(NULL,"DLL_PROCESS_DETACH",NULL,MB_OK);
		break;
	}
    return TRUE;
}

void WINAPI start()
{
	hhook = SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)mykeyproc,hinst,NULL);
	ShowDlg();
	myhook();
}

extern "C" __declspec(dllexport) void ShowDlg()
{     
	hwndDLG = CreateDialog(hinst,MAKEINTRESOURCE(IDD_DIALOG1),NULL,(DLGPROC)DlgProc);     
	ShowWindow(hwndDLG,SW_SHOW);
}

BOOL CALLBACK DlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)						  
{
	switch(message)
	{
	case WM_INITDIALOG:
		return TRUE;
	case WM_COMMAND:
		/*
		if(LOWORD(wParam)==IDOK)
		{
			MessageBox(NULL,"IDOK",NULL,MB_OK);
		}
		if(LOWORD(wParam)==IDCANCEL)
		{
			MessageBox(NULL,"IDCANCEL",NULL,MB_OK);
		}
		*/
		if(LOWORD(wParam)==IDC_CHECK1)
		{
			if (IsDlgButtonChecked(hDlg,IDC_CHECK1)==BST_CHECKED)
			//if (BST_CHECKED == SendDlgItemMessage(hDlg,IDC_CHECK1,BM_GETCHECK,0,0))
			{
				MessageBox(NULL,"IDC_CHECK1",NULL,MB_OK);
			}
			else
			{
				//............................
			}
		}
		return TRUE;
	case WM_CLOSE:
		DestroyWindow(hDlg);
		hwndDLG = NULL;
		return TRUE;
	}
	return FALSE;
}



LRESULT CALLBACK mykeyproc(int dwCode,WPARAM wParam,LPARAM lParam)
{
	PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) lParam;
	if (dwCode == HC_ACTION)
	{
		if (wParam == WM_KEYDOWN)
		{
			if (p->vkCode == VK_MULTIPLY)
			{
				if (hwndDLG == NULL)
				{
					ShowDlg();
				}
				else
				{
					DestroyWindow(hwndDLG);
					hwndDLG = NULL;
				}
				//MessageBox(NULL,"VK_NUMLOCK",NULL,MB_OK);
			}
		}
	}
	LRESULT RetVal = CallNextHookEx(hhook,dwCode,wParam,lParam);
	return RetVal;
}


[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回
//