Skip to content

Instantly share code, notes, and snippets.

@sontx
Last active July 15, 2018 04:01
Show Gist options
  • Save sontx/674a75326dd5b9d32e78b52fb89189db to your computer and use it in GitHub Desktop.
Save sontx/674a75326dd5b9d32e78b52fb89189db to your computer and use it in GitHub Desktop.
Open notepad and then auto-typing a message on it
#define EMBED_MESSAGE
#define DEBUG_MODE
#define WINVER 0x0500
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#ifdef EMBED_MESSAGE
#define SWEET_TITLE "My Sweetest Message"
#define SWEET_MESSAGE "If water were kisses, I'd send you the sea\nIf leaves were hugs, I'd send you a tree\nIf nite was love ,I'd send you the stars\nBut I can't send you my heart cause that where you are"
#else
#define MESSAGE_FILE_NAME "message.txt"
#endif // EMBED_MESSAGE
#define AUDIO_FILE_NAME "typing.mp3"
#define TEXT_EDITOR_PATH "c:\\windows\\notepad.exe"
#define MAX_TRY 5
struct HandleData
{
DWORD processId;
HWND betsHandle;
};
BOOL is_main_window(HWND handle);
BOOL CALLBACK enum_windows_callback(HWND handle, LPARAM lParam);
HWND find_main_window(DWORD processId);
VOID release_process(PPROCESS_INFORMATION processInfo);
VOID wait_until_exit(PPROCESS_INFORMATION processInfo);
VOID start_story(HWND handle);
VOID send_key(HWND handle, CHAR key);
VOID send_key(INPUT ip, SHORT vKeyCode);
#ifndef EMBED_MESSAGE
BOOL load_message_file(CONST PCHAR fileName, PCHAR content);
PCHAR get_title(CONST PCHAR content, PCHAR title);
#endif // EMBED_MESSAGE
INT main()
{
#ifndef DEBUG_MODE
ShowWindow( GetConsoleWindow(), SW_HIDE );
#endif // DEBUG_MODE
STARTUPINFO info;
PROCESS_INFORMATION processInfo;
ZeroMemory(&info, sizeof(info));
if (CreateProcess(TEXT_EDITOR_PATH, "", NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
{
for (INT i = 0; i < MAX_TRY; i++)
{
HWND handle = find_main_window(processInfo.dwProcessId);
if (handle != NULL)
{
start_story(handle);
wait_until_exit(&processInfo);
break;
}
Sleep(1000);
}
release_process(&processInfo);
}
}
BOOL is_main_window(HWND handle)
{
return GetWindow(handle, GW_OWNER) == (HWND)0 && IsWindowVisible(handle);
}
BOOL CALLBACK enum_windows_callback(HWND handle, LPARAM lParam)
{
HandleData& data = *(HandleData*) lParam;
DWORD processId = 0;
GetWindowThreadProcessId(handle, &processId);
if (data.processId != processId || !is_main_window(handle))
return TRUE;
data.betsHandle = handle;
return FALSE;
}
HWND find_main_window(DWORD processId)
{
HandleData data;
data.processId = processId;
data.betsHandle = NULL;
EnumWindows(enum_windows_callback, (LPARAM)&data);
return data.betsHandle;
}
VOID release_process(PPROCESS_INFORMATION processInfo)
{
CloseHandle((*processInfo).hProcess);
CloseHandle((*processInfo).hThread);
}
VOID wait_until_exit(PPROCESS_INFORMATION processInfo)
{
WaitForSingleObject((*processInfo).hProcess, INFINITE);
}
VOID start_story(HWND handle)
{
#ifdef EMBED_MESSAGE
PCHAR message = SWEET_MESSAGE;
SetWindowText(handle, SWEET_TITLE);
#else
CHAR content[1000];
if (!load_message_file(MESSAGE_FILE_NAME, content))
{
printf("%s not found!", MESSAGE_FILE_NAME);
return;
}
CHAR title[255];
PCHAR message = get_title(content, title);
if (message == NULL)
{
printf("Message file does not have any content!");
return;
}
SetWindowText(handle, title);
#endif // EMBED_MESSAGE
INT length = strlen(message);
srand(time(NULL));
CHAR mciString[255];
strcpy(mciString, "open \"");
strcat(mciString, AUDIO_FILE_NAME);
strcat(mciString, "\" type mpegvideo alias mp3");
mciSendString(mciString, NULL, 0, NULL);
mciSendString("play mp3 repeat", NULL, 0, NULL);
for (INT i = 0 ; i < length; i++)
{
CHAR key = message[i];
INT millis = rand() % 400 + 100;
send_key(handle, key);
printf("send[%dms]: %c\n", millis, key);
Sleep(millis);
}
mciSendString("stop mp3", NULL, 0, NULL);
}
VOID send_key(HWND handle, CHAR key)
{
if (GetForegroundWindow() != handle)
SetForegroundWindow(handle);
INPUT ip;
ZeroMemory(&ip, sizeof(ip));
ip.type = INPUT_KEYBOARD;
SHORT vKeyCode = VkKeyScan(key);
if ((vKeyCode & 0xFF00) != 256)
{
send_key(ip, vKeyCode);
}
else
{
ip.ki.dwFlags = 0;
ip.ki.wVk = VK_LSHIFT;
SendInput(1, &ip, sizeof(INPUT));
send_key(ip, vKeyCode);
ip.ki.dwFlags = KEYEVENTF_KEYUP;
ip.ki.wVk = VK_LSHIFT;
SendInput(1, &ip, sizeof(INPUT));
}
}
VOID send_key(INPUT ip, SHORT vKeyCode)
{
ip.ki.dwFlags = 0;
ip.ki.wVk = vKeyCode & 0x00FF;
SendInput(1, &ip, sizeof(INPUT));
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
}
#ifndef EMBED_MESSAGE
BOOL load_message_file(CONST PCHAR fileName, PCHAR content)
{
FILE * pFile = fopen(fileName, "r");
if (pFile == NULL)
return FALSE;
content[0] = '\0';
CHAR buff[255];
while (fgets(buff, sizeof(buff), pFile) != NULL)
{
strcat(content, buff);
}
fclose(pFile);
return TRUE;
}
PCHAR get_title(CONST PCHAR content, PCHAR title)
{
INT length = strlen(content);
for (INT i = 0, j = 0; i < length; i++)
{
if (content[i] != '\n')
{
title[j++] = content[i];
}
else
{
title[j] = '\0';
return &content[i + 1];
}
}
return NULL;
}
#endif // EMBED_MESSAGE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment