Solved

Logging System Messages

Posted on 2000-02-23
7
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

617 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