franciscar 0 Newbie Poster
// HookDLL.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "HookDLL.h"

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
                     )
{
    //Flesh out the entry point pending full implimentation

    // reserve the DLL handle
    ghModule = (HINSTANCE)hModule;

    // register system-wide message
    SWM_TRAYMSG = RegisterWindowMessage("TRAY_ACTIVATED"); // will be used when sending window to tray

    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

LRESULT CALLBACK KBHookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
    //Flesh out the function pending implimentation
    if(nCode < 0) {
        return CallNextHookEx(g_KBHook,nCode,wParam,lParam);
    }

    switch(wParam)
    {
        case VK_F11:
            if(HIWORD(lParam)){ // the Key is down

                if(g_bKeyF11) // key is already down
                    break;

                if(g_InUse) // a window is already subclassed
                {
                    if(WindowValid()) // check if previous window is still valid
                        break;
                }

                // ok the hook has been requested to drop program to window

                HWND hWnd = GetForegroundWindow(); // get the handle for the forground window

                if(!IsMu(hWnd))
                    break;

                g_InUse = SubClassWindowProc(hWnd); // subclass the window and get its icon for its minimization

                if(g_InUse)
                {
                    ChangeDisplaySettings(NULL,0); // drop back to windows settings

                    SetWindowRect(hWnd);
                }

                SetTimer(hWnd, IDT_RESET,100,TimerProc); 

                g_bKeyF11 = TRUE;
            }
            else { // the key is up
                g_bKeyF11 = FALSE;
            }
        break;
        case VK_F12:
            if(HIWORD(lParam)){ // the Key is down

                if(!g_Window.m_Hidden)
                {
                    g_Window.m_Hidden = TRUE;
                    Shell_NotifyIcon(NIM_ADD, &g_Window.m_niData);

                    // hide window
                    ShowWindow(g_Window.m_hWnd, SW_HIDE);
                }
            }
        break;
    }

    return CallNextHookEx(g_KBHook, nCode, wParam, lParam);
}

LRESULT CALLBACK SCWinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{

    switch(uMsg)
    {
        case WM_DESTROY:
                KillTimer(g_Window.m_hWnd,IDT_RESET);
                RestoreWindowProc();
        break;
        case WM_SIZE:
        case WM_ACTIVATE:
        case WM_SETFOCUS:
        case WM_KILLFOCUS:
            return DefWindowProc(hWnd,uMsg,wParam,lParam);
        break;
        case WM_CLOSE:
                KillTimer(g_Window.m_hWnd,IDT_RESET);
                RestoreWindowProc();
        break;
        case WM_ACTIVATEAPP:
            if((BOOL)wParam)
                SetCapture(g_Window.m_hWnd);
            else
                ReleaseCapture();
            return DefWindowProc(hWnd,uMsg,wParam,lParam);
        break;
        case WM_SETCURSOR:
        case WM_NCACTIVATE:
            return DefWindowProc( hWnd, uMsg, wParam, lParam );
        break;
        case WM_COMMAND:
            if(wParam == EN_KILLFOCUS)
                return DefWindowProc( hWnd, uMsg, wParam, lParam );
        break;
        case WM_MOUSEMOVE:
        {
            RECT rct =  { 0,0,0,0 };
            POINTS pos = MAKEPOINTS(lParam);
            GetClientRect(hWnd,&rct);
            if(pos.x <= rct.right)
            {
                if(pos.y <= rct.bottom)
                {   
                    ShowCursor(FALSE);
                    break;
                }
            }
            ShowCursor(TRUE);
            return DefWindowProc(hWnd,uMsg,wParam,lParam);
        }
        case WM_MOUSELEAVE:
            return DefWindowProc(hWnd,uMsg,wParam,lParam);
        break;
        case WM_SYSCOMMAND:                                             // Intercept System Commands
        {
            switch (wParam)                                             // Check System Calls
            {
                case SC_SCREENSAVE:                                     // Screensaver Trying To Start?
                case SC_MONITORPOWER:                                   // Monitor Trying To Enter Powersave?
                return 0;                                               // Prevent From Happening
            }
            break;                                                      // Exit
        }
        default:
            if(uMsg == SWM_TRAYMSG)
            {
                if(lParam == WM_LBUTTONDOWN)
                {
                    Shell_NotifyIcon(NIM_DELETE, &g_Window.m_niData);
                    ShowWindow(g_Window.m_hWnd, SW_SHOW);
                    g_Window.m_Hidden = FALSE;
                }
            }
        break;
    }
/*      switch(uMsg)
        {
        case WM_DESTROY:
        case WM_CLOSE:
                return CallWindowProc((WNDPROC)g_Window.m_OrigWndProc,hWnd,uMsg,wParam,lParam);
        break;
        case WM_COMMAND:
            if(wParam != EN_KILLFOCUS)
                return CallWindowProc((WNDPROC)g_Window.m_OrigWndProc,hWnd,uMsg,wParam,lParam);
        break;
        case WM_MOUSEMOVE:
        {
            RECT rct =  { 0,0,0,0 };
            POINTS pos = MAKEPOINTS(lParam);
            GetWindowRect(hWnd,&rct);
            if(pos.x <= rct.right)
                if(pos.y <= rct.bottom)
                    return CallWindowProc((WNDPROC)g_Window.m_OrigWndProc,hWnd,uMsg,wParam,lParam);
        break;
        }
        default:
            if(uMsg == SWM_TRAYMSG)
            {
                if(lParam == WM_LBUTTONDOWN)
                {
                    Shell_NotifyIcon(NIM_DELETE, &g_Window.m_niData);
                    ShowWindow(g_Window.m_hWnd, SW_SHOW);
                    g_Window.m_Hidden = FALSE;
                }
            }
        break;
    }*/

    //Flesh out the function pending implimentation
    //return DefWindowProc(hWnd,uMsg,wParam,lParam);
    return CallWindowProc((WNDPROC)g_Window.m_OrigWndProc,hWnd,uMsg,wParam,lParam);
}

HOOKDLL_API BOOL IH()
{
    g_KBHook = SetWindowsHookEx(WH_KEYBOARD, KBHookProc, ghModule, 0);

    if( g_KBHook == NULL ){
        return FALSE;   
    }

    return TRUE;
}

HOOKDLL_API BOOL UIH()
{
    if(g_InUse)
        RestoreWindowProc();

    return UnhookWindowsHookEx(g_KBHook);
}

HICON GetFileIconHandle(LPCTSTR lpszFileName, BOOL bSmallIcon)
{
    UINT uFlags = SHGFI_ICON | SHGFI_USEFILEATTRIBUTES;

    if (bSmallIcon)
        uFlags |= SHGFI_SMALLICON;
    else
        uFlags |= SHGFI_LARGEICON;

    SHFILEINFO sfi;
    SHGetFileInfo(lpszFileName, FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(SHFILEINFO), uFlags);

    return sfi.hIcon;
}

BOOL SubClassWindowProc(HWND hWnd)
{
    char szText[255];
    GetWindowText(hWnd, szText, 255);

    // prepare a NotifyData struct for this window
    ZeroMemory(&(g_Window.m_niData), sizeof(NOTIFYICONDATA));
    g_Window.m_niData.cbSize = sizeof(NOTIFYICONDATA);
    g_Window.m_niData.hWnd = hWnd;

    HICON hIcon = (HICON)SendMessage(hWnd, WM_GETICON, ICON_SMALL,0);                   

    if(!hIcon){
        char szPath[255];
        HMODULE hModule = (HMODULE)OpenProcess(0, FALSE, GetWindowThreadProcessId(hWnd, 0));
        GetModuleFileName(hModule, szPath, 255);
        hIcon = GetFileIconHandle(szPath, TRUE);
    }


    if(hIcon){
        g_Window.m_niData.hIcon = CopyIcon(hIcon);
    }
    else{
        g_Window.m_niData.hIcon = LoadIcon(NULL, IDI_QUESTION);
    }

    g_Window.m_niData.uID = TRAYICONID;
    g_Window.m_niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
    strcpy(g_Window.m_niData.szTip, szText);


    g_Window.m_niData.uCallbackMessage = SWM_TRAYMSG;

    g_Window.m_hWnd = hWnd;
    g_Window.m_Hidden = FALSE;
    g_Window.m_OrigWndProc = GetWindowLongPtr(hWnd,GWL_WNDPROC);

    if(SetWindowLongPtr(hWnd,GWL_WNDPROC,(LONG)SCWinProc) == g_Window.m_OrigWndProc)
        g_Window.m_Subclassed = TRUE;
    else
        g_Window.m_Subclassed = FALSE;

    return g_Window.m_Subclassed;
}

BOOL RestoreWindowProc()
{

    g_Window.m_Subclassed = FALSE;
    g_Window.m_Hidden = FALSE;
    Shell_NotifyIcon(NIM_DELETE, &g_Window.m_niData);
    ShowWindow(g_Window.m_hWnd, SW_SHOW);
    g_Window.m_Hidden = FALSE;
    g_InUse = FALSE;

    return ( SetWindowLongPtr(g_Window.m_hWnd,GWL_WNDPROC,(LONG)g_Window.m_OrigWndProc) >= 0);
}

VOID SetWindowRect(HWND hWnd)
{   
    RECT rct = { 0,0 ,g_XRES,g_YRES };
    BOOL bHasMenu = TRUE;

    SetWindowLongPtr(hWnd,GWL_STYLE,(LONG)WS_HOOKEDWINDOW);
    SetWindowLongPtr(hWnd,GWL_EXSTYLE,(LONG)WS_EX_OVERLAPPEDWINDOW);

    if(GetMenu(g_Window.m_hWnd) == NULL)
        bHasMenu = FALSE;

    AdjustWindowRectEx(&rct,WS_HOOKEDWINDOW,bHasMenu,WS_EX_OVERLAPPEDWINDOW);
    SetWindowPos(hWnd,HWND_NOTOPMOST,0,0,rct.right - rct.left,rct.bottom-rct.top,SWP_HOOKED);

    ShowCursor(TRUE);
}

HOOKDLL_API VOID SR(INT Width,INT Height)
{
    g_XRES = Width;
    g_YRES = Height;
}

BOOL WindowValid()
{
    HWND hWnd = GetWindow(g_Window.m_hWnd,GW_HWNDFIRST); // QUICKHACK: to check if window is still valid

    DWORD ErrorCode = GetLastError();

    if(ErrorCode == ERROR_INVALID_WINDOW_HANDLE)
        return FALSE;

    return TRUE;
}

VOID CALLBACK TimerProc( HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
    if(idEvent == IDT_RESET)
    {
        if(!WindowValid())
        {
            KillTimer(g_Window.m_hWnd,IDT_RESET);
            MessageBox(g_Window.m_hWnd,"Timer killed","TIMER",MB_OK);
        }

        if(!g_Window.m_Hidden)
        {
            RECT rct = { 0,0, g_XRES,g_YRES };
            BOOL bHasMenu = TRUE;

            if(GetMenu(g_Window.m_hWnd) == NULL)
                bHasMenu = FALSE;

            AdjustWindowRectEx(&rct,WS_HOOKEDWINDOW,bHasMenu,WS_EX_OVERLAPPEDWINDOW);
            SetWindowPos(hWnd,HWND_NOTOPMOST,0,0,rct.right - rct.left,rct.bottom-rct.top,SWP_HOOKED);
            //ShowCursor(TRUE);
        }
    }
}

BOOL IsMu(HWND hWnd)
{
    char szText[255]="\0";
    char szPath[255]="\0";
    char szTokS[] = "\\/";
    char *szTokC = NULL;
    char szTokO[255]="\0";
    int Len = 0;

    HMODULE hModule = (HMODULE)OpenProcess(0, FALSE, GetWindowThreadProcessId(hWnd, 0));

    Len = GetWindowText(hWnd, szText, 255);

    if(strcmp(strlwr(szText),"mu") != 0 )
        return FALSE;

    GetModuleFileName(hModule, szPath, 255);
    CloseHandle(hModule);

    szTokC = strtok(szPath,szTokS); 

    while(szTokC != NULL)
    {
        strcpy(szTokO,szTokC);

        szTokC = strtok(NULL,szTokS);
    }

    if(strcmp(strlwr(szTokO),"main.exe") != 0)
        return FALSE;

    return TRUE;
}
// The following ifdef block is the standard way of creating macros which make exporting 
// from a DLL simpler. All files within this DLL are compiled with the HOOKDLL_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see 
// HOOKDLL_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef HOOKDLL_EXPORTS // dont decorate the exports
#define HOOKDLL_API extern "C" __declspec(dllexport)
#else
#define HOOKDLL_API extern "C" __declspec(dllimport)
#endif

#define IDT_RESET       (WM_USER+WM_TIMER+0xff)
#define SC_SENDTOTRAY   (-90)
#define TRAYICONID  4
#define WS_HOOKEDWINDOW     (WS_CAPTION        | \
                             WS_SYSMENU)

#define SWP_HOOKED          (SWP_DRAWFRAME      | \
                             SWP_FRAMECHANGED   | \
                             SWP_NOMOVE         | \
                             SWP_NOZORDER       | \
                             SWP_SHOWWINDOW)


struct WNDDATA{
    HWND            m_hWnd;
    LRESULT         m_OrigWndProc;
    NOTIFYICONDATA  m_niData;
    BOOL            m_Subclassed;
    BOOL            m_Hidden;
};

#pragma data_seg(".SHARE")
    HHOOK           g_KBHook = NULL;
    WNDDATA         g_Window;
    BOOL            g_InUse = FALSE;
    BOOL            g_bKeyF12 = FALSE;
    BOOL            g_bKeyF11 = FALSE;
    INT             g_XRES = 800;
    INT             g_YRES = 600; // default to 800 * 600
    UINT            SWM_TRAYMSG;
#pragma data_seg()
#pragma comment(linker,"/SECTION:.SHARE,RWS")

HINSTANCE   ghModule    = NULL;

HOOKDLL_API BOOL IH();
HOOKDLL_API BOOL UIH();
HICON GetFileIconHandle(LPCTSTR lpszFileName, BOOL bSmallIcon);
BOOL SubClassWindowProc(HWND hWnd);
BOOL RestoreWindowProc();
BOOL IsMu(HWND hWnd);
LRESULT CALLBACK SCWinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam); // subclassed window proc
LRESULT CALLBACK KBHookProc(INT nCode,WPARAM wParam,LPARAM lParam); // the KeyBoard proc
VOID CALLBACK TimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
VOID SetWindowRect(HWND hWnd);
HOOKDLL_API VOID SR(INT Width,INT Height);
BOOL WindowValid();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.