Solved

Logging System Messages

Posted on 2000-02-23
7
211 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
ID: 2554059
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
ID: 2554074
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
ID: 2561529
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:Maquiavelo
ID: 2563164
 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
ID: 2563672
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
ID: 2566969
 Thanks again, my email is:

  fmesplet@infovia.com.ar
0
 
LVL 4

Expert Comment

by:inpras
ID: 2567550
I have sent the files
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

895 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

11 Experts available now in Live!

Get 1:1 Help Now