Win32 DLL 窗口未显示
我最近想出了一些逻辑(以及互联网上的各个地方)认为应该有效的代码。 当将我的 Win32 DLL 注入任何程序以对其进行测试时,我创建的窗口不会出现。我不知道为什么会这样,我的代码如下:
main.cpp
#include "stdafx.h"
#include "resource.h"
#include <tchar.h>
HWND PGHWND;
BOOL CALLBACK EventHandler(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
return 0;
}
DWORD WINAPI MainWin(HMODULE hMod)
{
DialogBox(hMod, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)EventHandler);
ExitThread(0);
return 0;
}
void GetWnd()
{
DialogBox(0,MAKEINTRESOURCE(IDD_DIALOG1),0,0);
for(;;)
{
PGHWND = FindWindow(NULL, "3D Pinball for Windows - Space Cadet");
if(PGHWND)
{
break;
}
}
}
__declspec(dllexport) bool __stdcall DllMain(HMODULE hModule,DWORD Reason,LPVOID lpv) //DllMain
{
switch (Reason){ //What happened?
case DLL_PROCESS_ATTACH: //Did the DLL attach?
DisableThreadLibraryCalls(hModule); //Disable THREAD_ATTACH and THREAD_DETACH
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MainWin, hModule, 0, NULL); //Start the thread to create the dialog
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)&GetWnd,NULL,0,NULL); //Start our thread to get the window
MessageBox(0, _T("This program was created by Joe Savage"), _T("Pinball Modifications!"), 0);
break;
break;
}
return true;
}
Main.rc
// Generated by ResEdit 1.5.4
// Copyright (C) 2006-2010
// http://www.resedit.net
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"
//
// Dialog resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG1 DIALOG 0, 0, 55, 24
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "Ms Shell Dlg"
{
DEFPUSHBUTTON "OK", IDOK, 3, 3, 50, 14
}
resource.h
#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif
#define IDD_DIALOG1 100
I've recently come up with some code that logic (and various places on the internet) says should work.
When injecting my Win32 DLL into any program though to test it, the Window I've create doesn't appear. I have no idea why this is, my code is as follows:
main.cpp
#include "stdafx.h"
#include "resource.h"
#include <tchar.h>
HWND PGHWND;
BOOL CALLBACK EventHandler(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
return 0;
}
DWORD WINAPI MainWin(HMODULE hMod)
{
DialogBox(hMod, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)EventHandler);
ExitThread(0);
return 0;
}
void GetWnd()
{
DialogBox(0,MAKEINTRESOURCE(IDD_DIALOG1),0,0);
for(;;)
{
PGHWND = FindWindow(NULL, "3D Pinball for Windows - Space Cadet");
if(PGHWND)
{
break;
}
}
}
__declspec(dllexport) bool __stdcall DllMain(HMODULE hModule,DWORD Reason,LPVOID lpv) //DllMain
{
switch (Reason){ //What happened?
case DLL_PROCESS_ATTACH: //Did the DLL attach?
DisableThreadLibraryCalls(hModule); //Disable THREAD_ATTACH and THREAD_DETACH
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MainWin, hModule, 0, NULL); //Start the thread to create the dialog
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)&GetWnd,NULL,0,NULL); //Start our thread to get the window
MessageBox(0, _T("This program was created by Joe Savage"), _T("Pinball Modifications!"), 0);
break;
break;
}
return true;
}
Main.rc
// Generated by ResEdit 1.5.4
// Copyright (C) 2006-2010
// http://www.resedit.net
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"
//
// Dialog resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG1 DIALOG 0, 0, 55, 24
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "Ms Shell Dlg"
{
DEFPUSHBUTTON "OK", IDOK, 3, 3, 50, 14
}
resource.h
#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif
#define IDD_DIALOG1 100
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在函数 GetWnd() 中,语句
DialogBox(0,MAKEINTRESOURCE(IDD_DIALOG1),0,0);
是错误的。它必须类似于:
DialogBox( hInst ,MAKEINTRESOURCE(IDD_DIALOG1),0 ,EventHandler );
其中 hInst 是 dll 的实例句柄。
In function GetWnd() the statement
DialogBox(0,MAKEINTRESOURCE(IDD_DIALOG1),0,0);
is wrong.it must be something like :
DialogBox( hInst ,MAKEINTRESOURCE(IDD_DIALOG1),0 ,EventHandler );
where hInst is the instance-handle of your dll.