Win32 DLL 窗口未显示

发布于 2024-12-09 06:46:03 字数 2131 浏览 0 评论 0原文

我最近想出了一些逻辑(以及互联网上的各个地方)认为应该有效的代码。 当将我的 Win32 DLL 注入任何程序以对其进行测试时,我创建的窗口不会出现。我不知道为什么会这样,我的代码如下:

ma​​in.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潦草背影 2024-12-16 06:46:03

在函数 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文