#define STRICT                         // Avoids some type mismatches
#include <windows.h>
#include <stdio.h>
#include "plugin.h"

extern char string[TEXTLEN];  // defined in cmdexec.c

typedef enum _PROCESSINFOCLASS {
ProcessDebugFlags=31 // From ntddk.h
} PROCESSINFOCLASS;

typedef DWORD (NTAPI *NTSETINFORMATIONPROCESS)(
IN HANDLE ProcessHandle,
IN PROCESSINFOCLASS ProcessInformationClass,
IN PVOID ProcessInformation,
IN ULONG ProcessInformationLength
);


int Childdbg(char *answer,ulong parm)
{  
	char					buf[TEXTLEN]={0};
	int						DebugFlag;
	HMODULE					hNtdll;
	NTSETINFORMATIONPROCESS ntsetinfproc;

	strncpy(buf,string,(TEXTLEN-1));

	if(strnicmp(buf,"0",1)==0)
	{
		if((hNtdll = LoadLibrary("ntdll.dll")) == NULL)
		{
			Addtolist(0,1,"loadlib failed in command childdbg");
			return GetLastError();
		}

		if((ntsetinfproc = (NTSETINFORMATIONPROCESS) GetProcAddress(hNtdll,"NtSetInformationProcess")) == NULL)
		{
			Addtolist(0,1,"GetProcAddress failed in command childdbg");
			return GetLastError();
		}
		DebugFlag = 0;
		ntsetinfproc((PVOID)Plugingetvalue(VAL_HPROCESS),ProcessDebugFlags,&DebugFlag,sizeof(DebugFlag));
		Addtolist(0,1,"hello Childdbg Disabled");
	}
	else if(strnicmp(buf,"1",1)==0)
	{
		if((hNtdll = LoadLibrary("ntdll.dll")) == NULL)
		{
			Addtolist(0,1,"loadlib failed in command childdbg");
			return GetLastError();
		}

		if((ntsetinfproc = (NTSETINFORMATIONPROCESS) GetProcAddress(hNtdll,"NtSetInformationProcess")) == NULL)
		{
			Addtolist(0,1,"GetProcAddress failed in command childdbg");
			return GetLastError();
		}
		DebugFlag = 1;
		ntsetinfproc((PVOID)Plugingetvalue(VAL_HPROCESS),ProcessDebugFlags,&DebugFlag,sizeof(DebugFlag));
		Addtolist(0,1,"hello Childdbg Enabled");
	}
	else
	{
		Addtolist(0,1,"use \"childdbg 0 to Disable or childdbg 1 to Enable\" ");
	}
	return 0;
}


