summaryrefslogtreecommitdiff
path: root/kbdnotify/KnowYourRights/knowyourrights.c
blob: 8b83985e3b25f8a0584a0e1b75ed4064f3fabfae (plain)
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
#include <stdio.h>
#include <windows.h>
#include <winioctl.h>
#include "../../headers_c/newpluginapi.h"
#include "../../headers_c/m_utils.h"

HANDLE hKbdDev[10] = {INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE};
#define MAX_KBDHANDLES 10

BOOL isWindowsNT(void)
{
	OSVERSIONINFOEX osvi;
	BOOL bOsVersionInfoEx;

	ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
	if(!(bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO *) &osvi))) {
		osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
		if(!GetVersionEx((OSVERSIONINFO *)&osvi))
			osvi.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS;
	}
	return osvi.dwPlatformId==VER_PLATFORM_WIN32_NT;
}


BOOL OpenKeyboardDevice()
{
	int i = 0;
	char aux1[MAX_PATH+1], aux2[MAX_PATH+1];

	do {
		mir_snprintf(aux1, sizeof(aux1), "Kbd%d", i);
		mir_snprintf(aux2, sizeof(aux2), "\\Device\\KeyboardClass%d", i);
		DefineDosDevice(DDD_RAW_TARGET_PATH, aux1, aux2);

		mir_snprintf(aux1, sizeof(aux1), "\\\\.\\Kbd%d", i);
		hKbdDev[i] = CreateFile(aux1, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

	} while (hKbdDev[i] != INVALID_HANDLE_VALUE && ++i < MAX_KBDHANDLES);
	
	return hKbdDev[0] != INVALID_HANDLE_VALUE;
}


void CloseKeyboardDevice()
{
	int i;

	for (i = 0; i < MAX_KBDHANDLES && hKbdDev[i] != INVALID_HANDLE_VALUE; i++)
		CloseHandle(hKbdDev[i]);

}


int main(void)
{
	char buffer[MAX_PATH+1];

	if (!isWindowsNT()) {
		printf("This application is meant for NT based systems\n");
		return 0;
	}

	if (QueryDosDevice("Kbd0", buffer, sizeof(buffer))) {
		printf("Device already created\n");
		return 0;
	}

	if (!OpenKeyboardDevice()) {
		printf("Device could not be created\n");
		return 1;
	}

	CloseKeyboardDevice();
	printf("Device succesfully created\n");
	return 0;
}