-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCConsole.cpp
More file actions
172 lines (137 loc) · 5.4 KB
/
CConsole.cpp
File metadata and controls
172 lines (137 loc) · 5.4 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "CConsole.h"
#define WS_SIZEBOX WS_THICKFRAME
#define SWP_NOZORDER SWP_NOSIZE
RECT rc;
RECT rect;
#undef max
static const char alphanum[] = "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"; // he has a brain atleast
int stringLength1 = sizeof(alphanum) - 1;
char genRandom1()
{
return alphanum[rand() % stringLength1];
}
// Log a message in the console with a timestamp. LogType changes the color of the text. aka you're lazy fucker.
void Log(std::string Message, int LogType)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);;
SYSTEMTIME st, lt;
GetSystemTime(&st);
GetLocalTime(<);
SetConsoleTextAttribute(hConsole, 9);
printf("[%02d:%02d:%02d] ", st.wHour, st.wMinute, st.wSecond);
SetConsoleTextAttribute(hConsole, LogType);
std::cout << Message << std::endl;
SetConsoleTextAttribute(hConsole, 15);
}
void Log1(std::string Message, int LogType)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);;
SYSTEMTIME st, lt;
GetSystemTime(&st);
GetLocalTime(<);
SetConsoleTextAttribute(hConsole, 9);
printf("[%02d:%02d:%02d] ", st.wHour, st.wMinute, st.wSecond);
SetConsoleTextAttribute(hConsole, LogType);
std::cout << Message;
SetConsoleTextAttribute(hConsole, 15);
}
// Return the diskdrive serialnumber.
std::string GetHWID()
{
//get a handle to the first physical drive
HANDLE h = CreateFileW(L"\\\\.\\PhysicalDrive0", 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (h == INVALID_HANDLE_VALUE) return {};
//an std::unique_ptr is used to perform cleanup automatically when returning (i.e. to avoid code duplication)
std::unique_ptr<std::remove_pointer<HANDLE>::type, void(*)(HANDLE)> hDevice{ h, [](HANDLE handle) {CloseHandle(handle); } };
//initialize a STORAGE_PROPERTY_QUERY data structure (to be used as input to DeviceIoControl)
STORAGE_PROPERTY_QUERY storagePropertyQuery{};
storagePropertyQuery.PropertyId = StorageDeviceProperty;
storagePropertyQuery.QueryType = PropertyStandardQuery;
//initialize a STORAGE_DESCRIPTOR_HEADER data structure (to be used as output from DeviceIoControl)
STORAGE_DESCRIPTOR_HEADER storageDescriptorHeader{};
//the next call to DeviceIoControl retrieves necessary size (in order to allocate a suitable buffer)
//call DeviceIoControl and return an empty std::string on failure
DWORD dwBytesReturned = 0;
if (!DeviceIoControl(hDevice.get(), IOCTL_STORAGE_QUERY_PROPERTY, &storagePropertyQuery, sizeof(STORAGE_PROPERTY_QUERY),
&storageDescriptorHeader, sizeof(STORAGE_DESCRIPTOR_HEADER), &dwBytesReturned, NULL))
return {};
//allocate a suitable buffer
const DWORD dwOutBufferSize = storageDescriptorHeader.Size;
std::unique_ptr<BYTE[]> pOutBuffer{ new BYTE[dwOutBufferSize]{} };
//call DeviceIoControl with the allocated buffer
if (!DeviceIoControl(hDevice.get(), IOCTL_STORAGE_QUERY_PROPERTY, &storagePropertyQuery, sizeof(STORAGE_PROPERTY_QUERY),
pOutBuffer.get(), dwOutBufferSize, &dwBytesReturned, NULL))
return {};
//read and return the serial number out of the output buffer
STORAGE_DEVICE_DESCRIPTOR* pDeviceDescriptor = reinterpret_cast<STORAGE_DEVICE_DESCRIPTOR*>(pOutBuffer.get());
const DWORD dwSerialNumberOffset = pDeviceDescriptor->SerialNumberOffset;
if (dwSerialNumberOffset == 0) return {};
const char* serialNumber = reinterpret_cast<const char*>(pOutBuffer.get() + dwSerialNumberOffset);
return serialNumber;
}
void CConsole::MakeSexy()
{
HWND hwnd = GetConsoleWindow();
DWORD style = GetWindowLong(hwnd, GWL_STYLE);
style &= ~WS_MAXIMIZEBOX & ~WS_SIZEBOX;
SetWindowLong(hwnd, GWL_STYLE, style);
GetWindowRect(hwnd, &rc);
int xPos = (GetSystemMetrics(SM_CXSCREEN) - rc.right) / 2;
int yPos = (GetSystemMetrics(SM_CYSCREEN) - rc.bottom) / 2;
SetWindowPos(hwnd, 0, xPos, yPos, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
void CConsole::SetRandomTitle()
{
srand(time(0));
std::string Str;
for (unsigned int i = 0; i < 20; ++i)
{
Str += genRandom1();
}
SetConsoleTitleA(Str.c_str());
}
void CConsole::SetTitle(_In_ LPCSTR lpConsoleTitle)
{
SetConsoleTitleA(lpConsoleTitle);
return;
}
int CConsole::Print(char const* _Format, ...)
{
int _Result;
va_list _ArgList;
__crt_va_start(_ArgList, _Format);
_Result = _vfprintf_l(stdout, _Format, NULL, _ArgList);
__crt_va_end(_ArgList);
return _Result;
}
int CConsole::WPrint(wchar_t const* _Format, ...)
{
int _Result;
va_list _ArgList;
__crt_va_start(_ArgList, _Format);
_Result = _vfwprintf_l(stdout, _Format, NULL, _ArgList);
__crt_va_end(_ArgList);
return _Result;
}
void CConsole::Clear()
{
COORD topLeft = { 0, 0 };
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO screen;
DWORD written;
GetConsoleScreenBufferInfo(console, &screen);
FillConsoleOutputCharacterA(console, ' ', screen.dwSize.X * screen.dwSize.Y, topLeft, &written);
FillConsoleOutputAttribute(console, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE, screen.dwSize.X * screen.dwSize.Y, topLeft, &written);
SetConsoleCursorPosition(console, topLeft);
return;
}
void CConsole::SetColor(unsigned short color)
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon, color);
}
void CConsole::Pause()
{
_getch();
std::cout << std::endl;
}