Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Logging System Messages

Posted on 2000-02-23
7
Medium Priority
?
230 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 1212 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

886 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