Skip to content

Instantly share code, notes, and snippets.

@maxdeliso
Created February 7, 2012 21:00
Show Gist options
  • Save maxdeliso/1761916 to your computer and use it in GitHub Desktop.
Save maxdeliso/1761916 to your computer and use it in GitHub Desktop.
An examination of the CRT in windows.
#include <Windows.h>
#define BUFFER_SIZE 4096
DWORD write( HANDLE out, const char* buf );
DWORD read( HANDLE in, char* buf, int bufSize );
int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
DWORD dwThreadID;
DWORD dwProcessID;
HANDLE stdout;
HANDLE stdin;
HANDLE hProcess;
HANDLE hThread;
char userBuf[BUFFER_SIZE];
/* Allocate console and verify */
if( !AllocConsole() ) {
return 1;
}
/* Open self */
dwThreadID = GetCurrentThreadId();
dwProcessID = GetCurrentProcessId();
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessID );
hThread = OpenThread( THREAD_ALL_ACCESS, FALSE, dwThreadID );
/* Verify */
if( hProcess == NULL || hThread == NULL ) {
return 2;
}
/* Acquire standard handles */
stdin = GetStdHandle(STD_INPUT_HANDLE);
stdout = GetStdHandle(STD_OUTPUT_HANDLE);
/* Output */
sprintf(userBuf, "Current Process:\n pid: %i tid: %i\n", dwProcessID, dwThreadID);
write(stdout, userBuf);
read(stdin, userBuf, sizeof userBuf);
/* Close handles and exit */
CloseHandle(hThread);
CloseHandle(hProcess);
return 0;
}
DWORD write( HANDLE out, const char* buf ) {
DWORD written;
WriteConsole(out, buf, strlen(buf), &written, NULL);
return written;
}
DWORD read( HANDLE in, char* buf, int bufSize ) {
DWORD read;
ReadConsole(in, buf, sizeof buf, &read, NULL);
return read;
}
crttest : crttest.c
cl crttest.c /Facrttest.asm /Fmcrttest.map
dumpbin /disasm:bytes /relocations crttest.exe > crttest.disasm
clean:
erase /s /q crttest.exe crttest.obj crttest.asm crttest.disasm crttest.map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment