Skip to content

Instantly share code, notes, and snippets.

@tiesmaster
Created July 23, 2011 17:34
Show Gist options
  • Save tiesmaster/1101664 to your computer and use it in GitHub Desktop.
Save tiesmaster/1101664 to your computer and use it in GitHub Desktop.
PrintStdIn
# https://raw.github.com/github/gitignore/master/Global/vim.gitignore
.*.sw[a-z]
*.un~
Session.vim
# binaries
*.exe
*.dll
#include <windows.h>
#include <stdio.h>
void main() {
FILE *f = fopen("child_output.log", "w");
printf("child: started\n");
DWORD chars_written;
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "child: started\n", sizeof("child: started\n"), &chars_written, NULL);
HANDLE std_in = GetStdHandle(STD_INPUT_HANDLE);
printf("STD IN: %#04x\n", std_in);
fprintf(f, "STD IN: %#04x\n", std_in);
STARTUPINFO si;
GetStartupInfo(&si);
printf("STD IN according to STARTUPINFO: %#04x\n", si.hStdInput);
fprintf(f, "STD IN according to STARTUPINFO: %#04x\n", si.hStdInput);
fclose(f);
Sleep(100 * 1000);
}
#include <windows.h>
#include <stdio.h>
void main() {
HANDLE std_in = GetStdHandle(STD_INPUT_HANDLE);
printf("STD IN: %#04x\n", std_in);
STARTUPINFO si;
char msg[200];
GetStartupInfo(&si);
sprintf(msg, "stdin: %#04x, stdout: %#04x, stderr: %#04x, &si: %#04x\n", si.hStdInput, si.hStdOutput, si.hStdError, &si);
printf(msg);
GetStartupInfo(&si);
sprintf(msg, "stdin: %#04x, stdout: %#04x, stderr: %#04x, &si: %#04x\n", si.hStdInput, si.hStdOutput, si.hStdError, &si);
printf(msg);
}
#include <windows.h>
#include <stdio.h>
typedef LONG NTSTATUS;
typedef struct _UNICODE_STRING
{
USHORT Length;
USHORT MaximumLength;
PWCH Buffer;
} UNICODE_STRING, *PUNICODE_STRING;
typedef enum _OBJECT_INFORMATION_CLASS
{
ObjectBasicInformation,
ObjectNameInformation,
ObjectTypeInformation,
ObjectTypesInformation,
ObjectHandleFlagInformation,
ObjectSessionInformation,
MaxObjectInfoClass
} OBJECT_INFORMATION_CLASS;
typedef struct _OBJECT_TYPE_INFORMATION
{
UNICODE_STRING TypeName;
ULONG TotalNumberOfObjects;
ULONG TotalNumberOfHandles;
ULONG TotalPagedPoolUsage;
ULONG TotalNonPagedPoolUsage;
ULONG TotalNamePoolUsage;
ULONG TotalHandleTableUsage;
ULONG HighWaterNumberOfObjects;
ULONG HighWaterNumberOfHandles;
ULONG HighWaterPagedPoolUsage;
ULONG HighWaterNonPagedPoolUsage;
ULONG HighWaterNamePoolUsage;
ULONG HighWaterHandleTableUsage;
ULONG InvalidAttributes;
GENERIC_MAPPING GenericMapping;
ULONG ValidAccessMask;
BOOLEAN SecurityRequired;
BOOLEAN MaintainHandleCount;
ULONG PoolType;
ULONG DefaultPagedPoolCharge;
ULONG DefaultNonPagedPoolCharge;
} OBJECT_TYPE_INFORMATION, *POBJECT_TYPE_INFORMATION;
NTSTATUS
WINAPI
NtQueryObject(
HANDLE Handle,
OBJECT_INFORMATION_CLASS ObjectInformationClass,
PVOID ObjectInformation,
ULONG ObjectInformationLength,
PULONG ReturnLength
);
void main() {
HANDLE std_in = GetStdHandle(STD_INPUT_HANDLE);
//FILE *f = fopen("test", "w");
printf("STD IN: %#04x\n", std_in);
//fclose(f);
DWORD mode;
if(GetConsoleMode(std_in, &mode)){
printf("console mode: %d\n", mode);
//return;
} else {
printf("GetLastError() returned: %d\n", GetLastError());
//return;
}
NTSTATUS status;
ULONG returnLength;
POBJECT_TYPE_INFORMATION buffer;
std_in = GetCurrentProcess();
status = NtQueryObject(std_in, ObjectTypeInformation, NULL, 0, &returnLength);
if (returnLength == 0)
printf("returnLength == 0 :S:S:S\n");
buffer = malloc(returnLength);
status = NtQueryObject(std_in, ObjectTypeInformation, buffer, returnLength, &returnLength);
wprintf(L"%s\n", buffer->TypeName.Buffer);
Sleep(100 * 1000);
}
#include <windows.h>
#include <stdio.h>
__declspec(dllexport) void ShowStartupInfo() {
STARTUPINFO si;
GetStartupInfo(&si);
char msg[100];
sprintf(msg, "stdin: %#04x, stdout: %#04x, stderr: %#04x, &si: %#04x", si.hStdInput, si.hStdOutput, si.hStdError, &si);
char title[100];
sprintf(title, "Startup info of process PID: %d", GetCurrentProcessId());
MessageBox(0, msg, title, 0);
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
ShowStartupInfo();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
SRCS := $(wildcard *.c)
OBJS = ${SRCS:.c=}
all: $(OBJS)
get_stdio_kobject_type: get_stdio_kobject_type.c
$(CC) $< -o $@ -lntdll
get_stdio_remote.dll: get_stdio_remote.c
$(CC) $< -o $@ -shared
test: test.c get_stdio_remote.dll
.PHONY: clean
clean:
rm -f $(OBJS:=.exe) *log *dll
#include <windows.h>
#include <stdio.h>
void main() {
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
if(!CreateProcess( NULL, "child.exe", NULL, NULL,
FALSE, 0, NULL, NULL, &si, &pi)) {
printf("CreateProcess failed (%d).\n", GetLastError());
return;
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
__declspec(dllimport) void ShowStartupInfo();
void main() {
ShowStartupInfo();
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment