Solved

Logging System Messages

Posted on 2000-02-23
7
223 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 clear a vector as well as how to detect empty vectors in C++.

734 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