Solved

Logging System Messages

Posted on 2000-02-23
7
208 Views
Last Modified: 2010-04-02
 I'm writing an app that logs to a txt file every click and key pressed by the user on the system.

  Any ideas how to achieve this? I've been doing some reading: SetWindowsHooksEx, GetMessage, PeekMessage, SubClassing, etc., but can't make it work.

  I'd appreciate source code if possible.

  Thanks in advance, Mq.

P.D.: Please be very explicit because I'm new in C++.
0
Comment
Question by:Maquiavelo
  • 4
  • 3
7 Comments
 
LVL 4

Accepted Solution

by:
inpras earned 404 total points
Comment Utility
Hi
This is how U have to do it

Create a mfc dll add the following code or copy paste

void __stdcall InitiateMe(HWND hWnd, HINSTANCE hInstance);
HWND g_hWnd;
HHOOK g_hHookKeyboard;
HHOOK g_hHookMouse;
LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam);
BOOL SetMyKbMouseHook(HWND hWnd, HINSTANCE hInstance);
LRESULT CALLBACK MouseProc(int code, WPARAM wParam, LPARAM lParam) ;
CFile file("c:\\MyKBMSHook.txt", CFile::modeCreate|CFile::modeWrite);
void __stdcall InitiateMe(long lMyTime, long lpEnumFunc, HWND hWnd, HINSTANCE hInstance)
{
    vbFunc = (FUNCPTR)lpEnumFunc;
    glTimeMax = lMyTime;
    bMyFlag = FALSE;
    SetMyKbMouseHook(hWnd, hInstance)
}

BOOL SetMyKbMouseHook(HWND hWnd, HINSTANCE hInstance)
{
    g_hHookKeyboard = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, (HINSTANCE)NULL, GetCurrentThreadId());
    g_hHookMouse    = SetWindowsHookEx(WH_MOUSE, MouseProc, (HINSTANCE)NULL, GetCurrentThreadId());
    if(g_hHookKeyboard == FALSE)
    {
        return FALSE;
    }
    g_hWnd=hWnd;
    return TRUE;
}


LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
    if(code < 0 || code != HC_ACTION)
    {
        return CallNextHookEx(g_hHookKeyboard, code, wParam, lParam);
    }

    file.Write("KeyPressed", 10);
   
    return CallNextHookEx(g_hHookKeyboard, code, wParam, lParam);
}

LRESULT CALLBACK MouseProc(int code, WPARAM wParam, LPARAM lParam)
{
    if(code < 0 || code != HC_ACTION)
    {
        return CallNextHookEx(g_hHookMouse, code, wParam, lParam);
    }
    file.Write("MouseProc", 9);
    return CallNextHookEx(g_hHookMouse, code, wParam, lParam);
}

int CCallbackToVBApp::ExitInstance()
{
    file.Close();
    UnhookWindowsHookEx(g_hHookMouse);
    UnhookWindowsHookEx(g_hHookKeyboard);
    return CWinApp::ExitInstance();
}

Go to .def file add the function name in as
InitiateMe under section EXPORT

Call this function from an exe

Note if U want to log specific message then U have to handle in respective call back functions

Hope this helps
inpras
0
 
LVL 4

Expert Comment

by:inpras
Comment Utility
the function declaration should be
void __stdcall InitiateMe(HWND hWnd, HINSTANCE hInstance)
{
            SetMyKbMouseHook(hWnd, hInstance)
}

I have passed hWnd and hInstance just in case U need them
this is system wide hook and all the messages will be logged with related to keyboard and mouse

inpras
0
 
LVL 1

Author Comment

by:Maquiavelo
Comment Utility
Sorry for this question but I'm a VB guy. How do I test this MFC DLL??

I've never used Visual C++, tough I know some C++. So far as I've seen your answer is what I wanted, but I'd like to test it.. :)
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Author Comment

by:Maquiavelo
Comment Utility
 When I tried to compile it this happened:

Compiling...
Error spawning cl.exe

SysHook.obj - 1 error(s), 0 warning(s)

----------------------------------------
And this when I tried to built it:

Compiling resources...
D:\Proyectos VB\MSG\SysHook\SysHook.rc(10) : fatal error RC1015: cannot open include file 'afxres.h'.
Error executing rc.exe.

SysHook.dll - 1 error(s), 0 warning(s)
0
 
LVL 4

Expert Comment

by:inpras
Comment Utility
if U give me UR email ID I will send U code both VB and VC U can run them easily or U can contact me at inpras@hotmail.com by sending what U have done so that I will currect it and send U back
0
 
LVL 1

Author Comment

by:Maquiavelo
Comment Utility
 Thanks again, my email is:

  fmesplet@infovia.com.ar
0
 
LVL 4

Expert Comment

by:inpras
Comment Utility
I have sent the files
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now