Skip to content

Instantly share code, notes, and snippets.

@shrubb
Last active December 26, 2015 08:56
Show Gist options
  • Save shrubb/b263061043ef62898a37 to your computer and use it in GitHub Desktop.
Save shrubb/b263061043ef62898a37 to your computer and use it in GitHub Desktop.
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c
#include <windows.h>
#include <psapi.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <stdio.h>
#include <limits.h>
#include <iostream>
char textToCopy[15] = {};
HANDLE hMMRealFile_in, hMMFile_in;
char *sharedData_in;
void InitClipboardFillerWindow() {
hMMRealFile_in = CreateFile(
"dummy.dat",
GENERIC_ALL,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
hMMFile_in = CreateFileMapping(
hMMRealFile_in,
NULL,
PAGE_EXECUTE_READWRITE,
0,
512,
"Connection_point");
sharedData_in = (char *)MapViewOfFile(
hMMFile_in,
FILE_MAP_WRITE,
0,
0,
512);
}
LRESULT CALLBACK WndProc2(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
char buf[1024];
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
sprintf(buf, "Press 'c' to fill the clipboard, 'r' to read from MMF");
TextOut(hdc, 5, 5, buf, _tcslen(buf));
sprintf(buf, "Writing to clipboard: %s", textToCopy);
TextOut(hdc, 5, 20, buf, _tcslen(buf));
sprintf(buf, "Reading from MMF: %s", sharedData_in);
TextOut(hdc, 5, 35, buf, _tcslen(buf));
EndPaint(hWnd, &ps);
break;
case WM_KEYDOWN:
/*char temp[20];
sprintf(temp, "%u", wParam);
MessageBox(NULL, temp, "aa", 0);*/
if (wParam == 67) { // 'c'
int i;
for (i = 0; i < 14; ++i) {
textToCopy[i] = 'a' + rand() % 26;
}
textToCopy[14] = 0;
if (FALSE == OpenClipboard(NULL)) {
MessageBox(hWnd, "Failed to open clipboard", "Error", 0);
break;
}
if (FALSE == EmptyClipboard()) {
MessageBox(hWnd, "Failed to clear clipboard", "Error", 0);
}
else {
HGLOBAL hResult = GlobalAlloc(GMEM_MOVEABLE, sizeof(textToCopy));
LPTSTR lptstrCopy = (LPTSTR)GlobalLock(hResult);
memcpy(lptstrCopy, textToCopy, sizeof(textToCopy));
GlobalUnlock(hResult);
if (SetClipboardData(CF_TEXT, hResult) == NULL) {
GlobalFree(hResult);
CloseClipboard();
char temp[50];
sprintf(temp, "Failed to copy to clipboard: error %u", GetLastError());
MessageBox(hWnd, temp, "Error", 0);
}
}
CloseClipboard();
InvalidateRect(hWnd, NULL, TRUE);
} else if (wParam == 82) { // 'r'
InvalidateRect(hWnd, NULL, TRUE);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c
#include <windows.h>
#include <psapi.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <stdio.h>
#include <limits.h>
#include <iostream>
int stage = 0;
HANDLE hFile;
HANDLE currentProc;
DWORD processId = UINT_MAX;
LPSTR cmdLine;
DWORD currentCPU;
FILETIME creationTime, kernelTime, userTime;
SYSTEMTIME creationTimeSys, kernelTimeSys, userTimeSys;
DWORD priorityClass;
PROCESS_MEMORY_COUNTERS memoryCounters;
char clipboardText[50] = {};
HKEY hRegKey;
HANDLE hMMRealFile, hMMFile;
char *sharedData;
void InitProcInfoWindow() {
hMMRealFile = CreateFile(
"dummy.dat",
GENERIC_ALL,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
hMMFile = CreateFileMapping(
hMMRealFile,
NULL,
PAGE_EXECUTE_READWRITE,
0,
512,
"Connection_point");
sharedData = (char *)MapViewOfFile(
hMMFile,
FILE_MAP_WRITE,
0,
0,
512);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
char updateMsg[] = "********** Press Space to update info **********";
char buf[1024];
BOOL errorFlag;
LSTATUS errorValue;
DWORD recvBufSize = 500;
char testData[] = "ExaJIu meDBeDu, Ha BeJIocuneDe";
char testDataKey[] = "the_key";
char testDataVal[] = "ExaJIu meDBeDu, Ha BeJIocuneDe";
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
TextOut(hdc, 5, 5, updateMsg, _tcslen(updateMsg));
if (processId != UINT_MAX) {
sprintf(buf, "PID: %u", processId);
TextOut(hdc, 5, 20, buf, _tcslen(buf));
sprintf(buf, "Command: %s", cmdLine);
TextOut(hdc, 5, 35, buf, _tcslen(buf));
sprintf(buf, "Running on CPU #%u", currentCPU);
TextOut(hdc, 5, 50, buf, _tcslen(buf));
GetDateFormat(LOCALE_CUSTOM_DEFAULT, DATE_LONGDATE,
&creationTimeSys, NULL, buf + 512, 230);
GetTimeFormat(LOCALE_CUSTOM_DEFAULT, 0,
&creationTimeSys, NULL, buf + 512 + 232, 230);
sprintf(buf, "Created at: %s %s", buf + 512, buf + 512 + 232);
TextOut(hdc, 5, 65, buf, _tcslen(buf));
sprintf(buf, "Time in kernel mode: %u ms", kernelTimeSys.wMilliseconds);
TextOut(hdc, 5, 80, buf, _tcslen(buf));
sprintf(buf, "Time in user mode: %u s %u ms",
userTimeSys.wSecond, userTimeSys.wMilliseconds);
TextOut(hdc, 5, 95, buf, _tcslen(buf));
switch (priorityClass) {
case ABOVE_NORMAL_PRIORITY_CLASS:
sprintf(buf + 512, "Above normal");
break;
case IDLE_PRIORITY_CLASS:
sprintf(buf + 512, "Idle");
break;
case BELOW_NORMAL_PRIORITY_CLASS:
sprintf(buf + 512, "Below normal");
break;
case REALTIME_PRIORITY_CLASS:
sprintf(buf + 512, "Realtime");
break;
case HIGH_PRIORITY_CLASS:
sprintf(buf + 512, "High");
break;
case NORMAL_PRIORITY_CLASS:
sprintf(buf + 512, "Normal");
break;
}
sprintf(buf, "Priority class: %s", buf + 512);
TextOut(hdc, 5, 110, buf, _tcslen(buf));
sprintf(buf, "Working set size: %u KB", memoryCounters.WorkingSetSize / 1024);
TextOut(hdc, 5, 125, buf, _tcslen(buf));
sprintf(buf, "PagefileUsage: 0x%08X", memoryCounters.PagefileUsage);
TextOut(hdc, 5, 140, buf, _tcslen(buf));
sprintf(buf, "Reading from clipboard: %s", clipboardText);
TextOut(hdc, 5, 170, buf, _tcslen(buf));
sprintf(buf, "Writing to MMF: %s", sharedData);
TextOut(hdc, 5, 185, buf, _tcslen(buf));
}
EndPaint(hWnd, &ps);
break;
case WM_KEYDOWN:
switch (wParam) {
case 32:; // Space
for (int i = 0; i < 20; ++i) {
sharedData[i] = 'A' + rand() % 26;
}
sharedData[20] = 0;
currentProc = GetCurrentProcess();
processId = GetCurrentProcessId();
currentCPU = GetCurrentProcessorNumber();
cmdLine = GetCommandLine();
GetProcessTimes(currentProc, &creationTime, &kernelTime, &kernelTime, &userTime);
FileTimeToLocalFileTime(&creationTime, &creationTime);
FileTimeToSystemTime(&creationTime, &creationTimeSys);
FileTimeToSystemTime(&kernelTime, &kernelTimeSys);
FileTimeToSystemTime(&userTime, &userTimeSys);
priorityClass = GetPriorityClass(currentProc);
GetProcessMemoryInfo(currentProc, &memoryCounters, sizeof(memoryCounters));
if (FALSE == OpenClipboard(NULL)) {
MessageBox(hWnd, "Failed to open clipboard", "Error", 0);
clipboardText[0] = 0;
}
else {
HANDLE hClip = GetClipboardData(CF_TEXT);
if (hClip == NULL) {
MessageBox(hWnd, "Error while copying from clipboard", "Error", 0);
}
else {
strcpy(clipboardText, (char *)hClip);
}
CloseClipboard();
}
InvalidateRect(hWnd, NULL, TRUE);
break;
case 13:; // Enter
char filePath[] = "test_file.txt";
switch (stage) {
case 0:;
// Create file
hFile = CreateFile(
filePath,
GENERIC_ALL,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf("error %u\n", GetLastError());
stage = 0;
if (GetLastError() == ERROR_ACCESS_DENIED) {
MessageBox(hWnd, "Access denied", "Error", NULL);
}
else {
MessageBox(hWnd, "Some error occured", "Error", NULL);
}
}
else {
MessageBox(hWnd, "File created", "Message", NULL);
++stage;
}
break;
case 1:; // Write to file
DWORD bytesWritten;
errorFlag = WriteFile(
hFile, // open file handle
testData, // start of data to write
strlen(testData), // number of bytes to write
&bytesWritten, // number of bytes that were written
NULL);
if (errorFlag == FALSE) {
if (GetLastError() == ERROR_ACCESS_DENIED) {
MessageBox(hWnd, "Access denied", "Error", 0);
}
else {
MessageBox(hWnd, "Some error occured", "Error", 0);
}
stage = 0;
CloseHandle(hFile);
}
else {
if (strlen(testData) != bytesWritten) {
MessageBox(hWnd, "dwBytesWritten != dwBytesToWrite", "Error", 0);
stage = 0;
CloseHandle(hFile);
}
else {
MessageBox(hWnd, "Wrote data to file successfully", "Message", 0);
++stage;
}
}
break;
case 2:; // Read from file
char readBuf[512];
readBuf[0] = 0;
OVERLAPPED overlapped;
overlapped = {};
DWORD bytesRead;
if (!ReadFile(hFile, readBuf, 511, &bytesRead, &overlapped)) {
char temp[10];
sprintf(temp, "error %u", GetLastError());
MessageBox(hWnd, "Couldn't read from the file", "Error", 0);
MessageBox(hWnd, temp, "Error", 0);
CloseHandle(hFile);
stage = 0;
}
else {
readBuf[bytesRead] = 0;
char temp[200];
sprintf(temp, "Successfully read from the file:\n%s", readBuf);
MessageBox(hWnd, temp, "Message", 0);
++stage;
}
break;
case 3:; // Delete file
CloseHandle(hFile);
if (FALSE == DeleteFile(filePath)) {
MessageBox(hWnd, "Couldn't delete the file", "Error", 0);
stage = 0;
}
else {
MessageBox(hWnd, "Successfully deleted the file", "Message", 0);
++stage;
}
break;
case 4:; // Create registry entry
DWORD disposition;
if (ERROR_SUCCESS != RegCreateKeyEx(
HKEY_CURRENT_USER,
"Software\\CASP2015\\",
0,
NULL,
REG_OPTION_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hRegKey,
NULL)) {
MessageBox(hWnd, "Error creating registry key", "Error", 0);
break;
}
else {
MessageBox(hWnd, "Successfully created a registry key", "Message", 0);
++stage;
}
break;
case 5:; // Put key-value
errorValue = RegSetValueEx(
hRegKey,
testDataKey,
0,
REG_SZ,
(BYTE*)testDataVal, sizeof(testDataVal));
if (errorValue != ERROR_SUCCESS) {
printf("error %d\n", errorValue);
MessageBox(hWnd, "Error writing to registry", "Error", 0);
RegCloseKey(hRegKey);
stage = 4;
break;
}
else {
MessageBox(hWnd, "Successfully put a value to that key", "Message", 0);
//RegCloseKey(hRegKey);
++stage;
}
break;
case 6:; // Read from registry
char recvBuf[512];
errorValue = RegGetValue(
HKEY_CURRENT_USER,
"Software\\CASP2015\\",
testDataKey,
RRF_RT_REG_SZ,
NULL,
(BYTE*)recvBuf,
&recvBufSize);
if (errorValue != ERROR_SUCCESS) {
printf("error %d\n", errorValue);
MessageBox(hWnd, "Error reading from registry", "Error", 0);
++stage;
//stage = 4;
break;
}
else {
char msg[512];
sprintf(msg, "Successfully read from registry:\n%s", recvBuf);
MessageBox(hWnd, msg, "Message", 0);
++stage;
}
break;
case 7:; // Delete registry record
errorValue = RegDeleteKeyEx(
HKEY_CURRENT_USER,
"Software\\CASP2015\\",
KEY_WOW64_64KEY,
0);
if (errorValue != ERROR_SUCCESS) {
char temp[60];
sprintf(temp, "Error deleting from registry: code %u", errorValue);
MessageBox(hWnd, temp, "Error", 0);
CloseHandle(hRegKey);
stage = 4;
break;
}
else {
CloseHandle(hRegKey);
MessageBox(hWnd, "Successfully deleted the whole key", "Message", 0);
++stage;
}
}
break;
}
InvalidateRect(hWnd, NULL, TRUE);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c
#include <windows.h>
#include <psapi.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <stdio.h>
#include <limits.h>
#include <iostream>
using std::cout;
static TCHAR szWindowClass[] = _T("Procinfo");
static TCHAR szWindowClass2[] = _T("ClipboardWin");
// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("CASP 2015");
HINSTANCE hInst;
// Forward declarations of functions included in this code module:
extern LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
extern LRESULT CALLBACK WndProc2(HWND, UINT, WPARAM, LPARAM);
HWND hWndGlobal, hWndGlobal2;
extern void InitProcInfoWindow();
extern void InitClipboardFillerWindow();
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Error"),
NULL);
return 1;
}
hInst = hInstance; // Store instance handle in our global variable
// The parameters to CreateWindow explained:
// szWindowClass: the name of the application
// szTitle: the text that appears in the title bar
// WS_OVERLAPPEDWINDOW: the type of window to create
// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
// 1024, 768: initial size (width, length)
// NULL: the parent of this window
// NULL: this application does not have a menu bar
// hInstance: the first parameter from WinMain
// NULL: not used in this application
hWndGlobal = CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
40, 400,
700, 290,
NULL,
NULL,
hInstance,
NULL
);
if (!hWndGlobal)
{
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Win32 Guided Tour"),
NULL);
return 1;
}
SetThreadLocale(0x00000409); // en-US
// The parameters to ShowWindow explained:
// hWndGlobal: the value returned from CreateWindow
// nCmdShow: the fourth parameter from WinMain
ShowWindow(hWndGlobal,
nCmdShow);
UpdateWindow(hWndGlobal);
InitProcInfoWindow();
WNDCLASSEX wcex2;
wcex2.cbSize = sizeof(WNDCLASSEX);
wcex2.style = CS_HREDRAW | CS_VREDRAW;
wcex2.lpfnWndProc = WndProc2;
wcex2.cbClsExtra = 0;
wcex2.cbWndExtra = 0;
wcex2.hInstance = hInstance;
wcex2.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex2.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex2.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex2.lpszMenuName = NULL;
wcex2.lpszClassName = szWindowClass2;
wcex2.hIconSm = LoadIcon(wcex2.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
if (!RegisterClassEx(&wcex2))
{
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Error"),
NULL);
return 1;
}
// The parameters to CreateWindow explained:
// szWindowClass: the name of the application
// szTitle: the text that appears in the title bar
// WS_OVERLAPPEDWINDOW: the type of window to create
// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
// 1024, 768: initial size (width, length)
// NULL: the parent of this window
// NULL: this application does not have a menu bar
// hInstance: the first parameter from WinMain
// NULL: not used in this application
hWndGlobal2 = CreateWindow(
szWindowClass2,
szTitle,
WS_OVERLAPPEDWINDOW,
40, 40,
400, 100,
NULL,
NULL,
hInstance,
NULL
);
if (!hWndGlobal2)
{
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Win32 Guided Tour"),
NULL);
return 1;
}
// The parameters to ShowWindow explained:
// hWndGlobal: the value returned from CreateWindow
// nCmdShow: the fourth parameter from WinMain
ShowWindow(hWndGlobal2,
nCmdShow);
UpdateWindow(hWndGlobal2);
InitClipboardFillerWindow();
// Main message loop:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment