Skocz do zawartości

Haki


kt1117

Rekomendowane odpowiedzi

Mam kod:

#include <windows.h>

HHOOK g_MyHook = NULL;
HWND hwnd;

/*  Declare Windows procedure  */
LRESULT CALLBACK AltF4Proc (int code, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "CodeBlocksWindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
                 /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Code::Blocks Template Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    g_MyHook = SetWindowsHookEx (WH_KEYBOARD, &AltF4Proc, NULL, ???);

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            UnhookWindowsHookEx (g_MyHook);
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        case WM_KEYDOWN:
        if (wParam==VK_F1)
        {
            Beep(128,222);
        }
        if (wParam==VK_F2)
        {
            Beep(144,222);
        }
        if (wParam==VK_F3)
        {
            Beep(160,222);
        }
        if (wParam==VK_F4)
        {
            Beep(186,222);
        }
        if (wParam==VK_F5)
        {
            Beep(212,222);
        }
        if (wParam==VK_F6)
        {
            Beep(238,222);
        }
        if (wParam==VK_F7)
        {
            Beep(264,222);
        }
        if (wParam==VK_F8)
        {
            Beep(290,222);
        }

        break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

LRESULT CALLBACK AltF4Proc (int code, WPARAM wParam, LPARAM lParam)
{
    if (code < 0) return CallNextHookEx (0, code, wParam, lParam);

    if (wParam == VK_F4 && (lParam & 536870912))
    {
        MessageBox (hwnd, "Nie zamkniesz mnie tak łatwo!", NULL, MB_ICONEXCLAMATION);
        return 1;
    }

    return CallNextHookEx (0, code, wParam, lParam);
}

Taki tam, i nie wiem co podstawić w miejsce ???, żeby kompilowało się i działało pod XP.

Odnośnik do komentarza
Udostępnij na innych stronach

Ta funkcja działa od Visty w górę. Sprawdzałem ją jako pierwszą. Przed chwilą sprawdziłem:

g_MyHook = SetWindowsHookEx (WH_KEYBOARD, &AltF4Proc, (HINSTANCE) hwnd, NULL);

Kompiluję się, ale hak nie działa.

Odnośnik do komentarza
Udostępnij na innych stronach

Kurde, nie działa w żaden sposób, zostawię to na razie i wrócę do tego za jakiś czas, a na razie wezmę się za Irllichta.

E:Albo się jeszcze nie poddaje, bo dowiedziałem się, że teraz muszę sprawdzić, czy hak jest założony. Jednak powinno być 0.

E2:Dałem radę, mam takie coś i działa:

#include <windows.h>
#include <fstream>

int a=1;
int b=true;
int c=true;

HHOOK g_MyHook = NULL;

HBRUSH pedzel=CreateSolidBrush(RGB(00,60,00));

HWND hwnd;
DWORD errorCode;

/*  Declare Windows procedure  */
LRESULT CALLBACK AltF4Proc (int code, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "Muzykant";

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{              /* This is the handle for our window */
    MSG messages;            /* Here meCreateSolidBrush(RGB(00,00255))ssages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = pedzel;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Muzykant",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           30,       /* Windows decides the position */
           30,       /* where the window ends up on the screen */
           1400,                 /* The programs width */
           800,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );




    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);
    MessageBox (hwnd,"Komunikat na pulpicie",NULL,NULL);
    g_MyHook = SetWindowsHookEx (WH_KEYBOARD, &AltF4Proc, NULL, GetCurrentThreadId());

    DWORD errorCode = GetLastError();

    std::ofstream log( "log.txt" );
    if( log.good() )
    {
        log << "Kod błędu: " << errorCode << '\n';
    }

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

LRESULT CALLBACK AltF4Proc (int code, WPARAM wParam, LPARAM lParam)
{

    if (code < 0) return CallNextHookEx (0, code, wParam, lParam);


    if (wParam==VK_F1)
    {
        MessageBox (hwnd, "Nie zamkniesz mnie tak łatwo!", NULL, MB_ICONEXCLAMATION);
        return 0;
    }
    if (wParam==VK_F2)
    {
        MessageBox (hwnd, "Nie zamkniesz mnie tak łatwo!", NULL, MB_ICONEXCLAMATION);
        return 0;
    }


    return CallNextHookEx (0, code, wParam, lParam);
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
                 UnhookWindowsHookEx (g_MyHook);
                 PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                 break;
        case WM_KEYDOWN:

                if (wParam==VK_F1)
                {
                    Beep(128*16*a,222);
                }
                if (wParam==VK_F2)
                {
                    Beep(144*16*a,222);
                }
                if (wParam==VK_F3)
                {
                    Beep(160*16*a,222);
                }
                if (wParam==VK_F4)
                {
                    Beep(176*16*a,222);
                }
                if (wParam==VK_F5)
                {
                    Beep(192*16*a,222);
                }
                if (wParam==VK_F6)
                {
                    Beep(208*16*a,222);
                }
                if (wParam==VK_F7)
                {
                    Beep(224*16*a,222);
                }
                if (wParam==VK_F8)
                {
                    Beep(240*16*a,222);
                }
                if (wParam==VK_UP)
                {
                    if (b and a<16)
                    {
                        a*=2;
                        b=false;
                    }

                }if (wParam==VK_DOWN)
                {
                    if (c and a>1)
                    {
                        a/=2;
                        c=false;
                    }

                }
                if (wParam==VK_ESCAPE)
                {
                    UnhookWindowsHookEx (g_MyHook);
                    PostQuitMessage (0);
                }




                break;
        case WM_KEYUP:
                if (wParam==VK_UP)
                {
                        b=true;
                }

                if (wParam==VK_DOWN)
                {
                        c=true;
                }
                break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return errorCode;
}

Więc teraz idę do globalnych i mam następny problem, bo mam kod dll:

#include "main.h"


BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{
    g_hInst = hModule;

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

    return TRUE;
}

To było .cpp, teraz .h:

#ifndef _MAIN_H_
#define _MAIN_H_

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */

#include <windows.h>
#include <vector>
using namespace std;

vector < EVENTMSG >
g_Events;
int g_CurrentEvent;
bool g_PlaybackStopped;
HHOOK g_RecordHook = NULL;
HHOOK g_PlaybackHook = NULL;
DWORD g_StartTime = 0;
HINSTANCE g_hInst = NULL;

DLL_API LRESULT CALLBACK RecordProc( int code, WPARAM wParam, LPARAM lParam )
{
    if( code < 0 ) return CallNextHookEx( 0, code, wParam, lParam );

    if( code == HC_ACTION )
    {
        EVENTMSG * msg =( EVENTMSG * ) lParam;
        msg->time -= g_StartTime;
        g_Events.push_back( * msg );
        return 0;
    }

    return CallNextHookEx( 0, code, wParam, lParam );
}

DLL_API LRESULT CALLBACK PlaybackProc( int code, WPARAM wParam, LPARAM lParam )
{
    if( code < 0 ) return CallNextHookEx( 0, code, wParam, lParam );

    if( code == HC_GETNEXT )
    {
        EVENTMSG * msg =( EVENTMSG * ) lParam;
        msg->hwnd = g_Events[ g_CurrentEvent ].hwnd;
        msg->message = g_Events[ g_CurrentEvent ].message;
        msg->paramH = g_Events[ g_CurrentEvent ].paramH;
        msg->paramL = g_Events[ g_CurrentEvent ].paramL;
        msg->time = g_StartTime + g_Events[ g_CurrentEvent ].time;
        DWORD delta = msg->time - GetTickCount();

        if( delta > 0 )
             return delta;
        else
             return 0;

    }
    else if( code == HC_SKIP )
    {
        if( !g_PlaybackStopped )
             g_CurrentEvent++;

        if( g_CurrentEvent >= g_Events.size() )
        {
            g_CurrentEvent = 0;
            g_StartTime = GetTickCount();
            g_PlaybackStopped = false;
            return 0;
        }
    }

    return 0;
}

DLL_API void  StartRecording( void )
{
    g_StartTime = GetTickCount();
    g_RecordHook = SetWindowsHookEx( WH_JOURNALRECORD, RecordProc, g_hInst, 0 );
}

DLL_API void  StartPlayback( void )
{
    g_CurrentEvent = 0;
    g_StartTime = GetTickCount();
    g_PlaybackStopped = false;
    UnhookWindowsHookEx( g_RecordHook );
    g_PlaybackHook = SetWindowsHookEx( WH_JOURNALPLAYBACK, PlaybackProc, g_hInst, 0 );
}

extern "C"
{
  DLL_API void  StartPlayback( void );
  DLL_API void  StartRecording( void );
}

#endif /* _DLL_H_ */

I niestety nie działa, nie rozpoznaje makra DLL_API, wyskakują takie błędy:

w\hakglobalny\main.h|23|error: 'DLL_API' does not name a type|
w\hakglobalny\main.h|38|error: 'DLL_API' does not name a type|
w\hakglobalny\main.h|75|error: expected constructor, destructor, or type conversion before 'void'|
w\hakglobalny\main.h|81|error: expected constructor, destructor, or type conversion before 'void'|
w\hakglobalny\main.h|92|error: expected constructor, destructor, or type conversion before 'void'|
w\hakglobalny\main.h|93|error: expected constructor, destructor, or type conversion before 'void'|

Odnośnik do komentarza
Udostępnij na innych stronach

Jeśli chcesz dodać odpowiedź, zaloguj się lub zarejestruj nowe konto

Jedynie zarejestrowani użytkownicy mogą komentować zawartość tej strony.

Zarejestruj nowe konto

Załóż nowe konto. To bardzo proste!

Zarejestruj się

Zaloguj się

Posiadasz już konto? Zaloguj się poniżej.

Zaloguj się
  • Ostatnio przeglądający   0 użytkowników

    • Brak zarejestrowanych użytkowników przeglądających tę stronę.
×
×
  • Dodaj nową pozycję...