Win32 DLL窗口没有显示

时间:2011-10-09 09:54:12

标签: c++ winapi dll window

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

的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

1 个答案:

答案 0 :(得分:0)

在函数GetWnd()中,语句DialogBox(0,MAKEINTRESOURCE(IDD_DIALOG1),0,0);是错误的。

它必须是:

DialogBox( hInst ,MAKEINTRESOURCE(IDD_DIALOG1),0 ,EventHandler );

其中hInst是你的dll的实例句柄。