Click here to Skip to main content
15,919,613 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow to Convert BYTE * to byte[] Pin
DeepToot10-Apr-05 18:08
DeepToot10-Apr-05 18:08 
AnswerRe: How to Convert BYTE * to byte[] Pin
DeepToot12-Apr-05 8:53
DeepToot12-Apr-05 8:53 
GeneralCSliderCtrl + dblclick Pin
Chris Losinger10-Apr-05 15:39
professionalChris Losinger10-Apr-05 15:39 
Generalmenu and sub menu Pin
mpapeo10-Apr-05 12:31
mpapeo10-Apr-05 12:31 
GeneralRe: menu and sub menu Pin
Christian Graus10-Apr-05 13:57
protectorChristian Graus10-Apr-05 13:57 
GeneralRe: menu and sub menu Pin
mpapeo10-Apr-05 15:17
mpapeo10-Apr-05 15:17 
GeneralRe: menu and sub menu Pin
Christian Graus10-Apr-05 15:22
protectorChristian Graus10-Apr-05 15:22 
GeneralRe: menu and sub menu Pin
mpapeo10-Apr-05 15:42
mpapeo10-Apr-05 15:42 
Christian Graus wrote:
First trick of programming - if it crashes, work out why, don't just change to a lesser design

Yes i am working on it day and night but now i am under presure as i have to submit this tommorrow.
<br />
#include <windows.h><br />
#include <stdio.h><br />
#include <windef.h><br />
#include <winbase.h><br />
#include <stdlib.h><br />
typedef struct _iobuf FILE;<br />
#include <assert.h><br />
#include <sys/types.h><br />
#include <sys/stat.h><br />
#include <string.h><br />
<br />
int menu(void)<br />
{<br />
//int choice;<br />
char iobuf[80];<br />
int i,len,valid;<br />
HANDLE hProcess;<br />
<br />
	 printf ("\n");<br />
	 printf ("\t\t\t*****************************************\n");<br />
	 printf ("\t\t\t*\t\t\t\t\t*\n\t\t\t*\t\tPROCESS\t\t\t*\n");<br />
	 printf ("\t\t\t*\t\t\t\t\t*\n\t\t\t*  CHECKPOINTING AND MIGRATION SYSTEM\t*\n");<br />
	 printf ("\t\t\t*\t\t\t\t\t*\n");<br />
	 printf ("\t\t\t=========================================");<br />
	 printf ("\n\t\t\t=\t 1: Create a Process    \t=");<br />
	 printf ("\n\t\t\t=\t 2: Suspend Process      \t=");<br />
	 printf ("\n\t\t\t=\t 3: Resume Process       \t=");<br />
	 printf ("\n\t\t\t=\t 4: Shutdown Process\t\t=");<br />
	 printf ("\n\t\t\t=\t 5: Checkpoint Process\t\t=");<br />
	 printf ("\n\t\t\t=\t 6: Restart Process\t\t=");<br />
	 printf ("\n\t\t\t=\t 7: Help     \t\t\t=");<br />
	 printf ("\n\t\t\t=\t 8: Quit the system\t\t=");<br />
	 printf ("\n\t\t\t=========================================\n");<br />
	 printf ("\n");<br />
<br />
	 printf("\n\t\t\tEnter choice (1-8): ");<br />
	<br />
	valid = 0;<br />
	while( valid == 0)<br />
   {<br />
     fgets(iobuf,sizeof(iobuf),stdin);<br />
     <br />
     len = strlen(iobuf)-1;<br />
     iobuf[len] = 0;// the input/output buffer for storing characters<br />
     // validate data<br />
     valid = 1; // assume valid input<br />
     for(i = 0; i < len; i++)	//loop controlling invalid input<br />
     {<br />
         if( !isdigit(iobuf[i]))<br />
         {<br />
           printf("\n\t\t\tPlese enter numeric digits only (1-8):");<br />
           valid = 0;          <br />
         }<br />
	 }<br />
   }//while<br />
<br />
	hProcess = atoi(iobuf);<br />
	return hProcess;<br />
<br />
 }//menu<br />
	<br />
<br />
HWND g_hWnd = NULL;<br />
<br />
BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)<br />
{<br />
  DWORD dwProcID;<br />
  GetWindowThreadProcessId(hwnd,&dwProcID);<br />
  if(dwProcID == (DWORD)lParam)<br />
  {<br />
    //this hwnd is the top level window ofthe process<br />
    g_hWnd = hwnd;<br />
  }<br />
<br />
  return TRUE;<br />
}<br />
int main(int argc, char **argv) <br />
{<br />
<br />
	FILE* fp;<br />
	//char fname[20];<br />
	char ans[4];<br />
	int result;<br />
<br />
	PROCESS_INFORMATION pi;       /* filled in by CreateProcess */<br />
	STARTUPINFO si;               /* startup info for the new process*/<br />
	HANDLE hProcess;<br />
	HANDLE hThread = 0;<br />
	DWORD NumberOfBytesRead = 0;<br />
	BYTE buf[20000];<br />
	DWORD bufsize = sizeof buf;<br />
//LPCTSTR lpApplicationName="";<br />
	DWORD baseaddr = 0;<br />
	DWORD lpAddr = 0;<br />
	PMEMORY_BASIC_INFORMATION lpBuffer = 0;<br />
	DWORD dwLength = 200000;<br />
	DWORD flNewProtect =0;<br />
	PDWORD lpflOldProtect = 0;<br />
	DWORD dwSize =0;<br />
	LPCVOID lpBaseAddress = 0;<br />
	DWORD nSize = 0;<br />
	MEMORY_BASIC_INFORMATION mbi;<br />
	LPDWORD lpNumberOfBytesWritten = 0;<br />
	TCHAR lpApplicationName[_MAX_PATH]="";<br />
	SYSTEM_INFO sinfo;<br />
	LPDWORD lpExitCode = 0;<br />
	DWORD pid;<br />
    LPVOID base;<br />
	HWND hWnd = 0;<br />
	DWORD dwProcessID = 0;<br />
	UINT GetBase();<br />
	LPCONTEXT lpContext = 0;<br />
	PHANDLE TokenHandle =0;<br />
	BOOL OpenAsSelf=0;<br />
	DWORD DesiredAccess=0;<br />
	HANDLE ThreadHandle=0;<br />
	DWORD dwDesiredAccess = 0;<br />
	BOOL bInheritHandle = 0;<br />
	DWORD dwThreadId = 0;<br />
<br />
		<br />
	LPSECURITY_ATTRIBUTES lpThreadAttributes = 0;<br />
	DWORD dwStackSize = 0;<br />
	LPTHREAD_START_ROUTINE lpStartAddress = 0;<br />
	LPVOID lpParameter = 0;<br />
	DWORD dwCreationFlags = 0;<br />
	LPDWORD lpThreadId = 0;<br />
	//CONST CONTEXT *lpContext;<br />
	int nRet = 0;<br />
    char *state;<br />
    unsigned int regnum = 0;<br />
	BOOL OpenThread();<br />
	int choice;<br />
<br />
while((choice = menu())!=9)<br />
		{<br />
			switch (choice)<br />
			{						<br />
			case 1:<br />
					<br />
			GetSystemInfo(&sinfo);<br />
			lpAddr = 0;<br />
			ZeroMemory( &si, sizeof(si) );<br />
			si.cb = sizeof(si);<br />
			ZeroMemory( &pi, sizeof(pi) );<br />
			<br />
			printf("\t\t\tEnter process you want to execute: ",lpApplicationName);<br />
			scanf ("%s",lpApplicationName);<br />
<br />
	GetStartupInfo(&si);<br />
<br />
		CreateProcess(NULL,		/* lpApplicationName	*/<br />
        lpApplicationName,		/* lpCommandLine	 	*/<br />
		NULL,					/* lpsaProcess			*/<br />
		NULL,					/* lpsaThread			*/<br />
		FALSE,					/* bInheritHandles		*/<br />
        CREATE_NEW_CONSOLE,     /* dwCreationFlags		*/<br />
		NULL,					/* lpEnvironment		*/<br />
        NULL,					/* lpCurDir				*/<br />
        &si,					/* lpStartupInfo		*/<br />
		&pi						/* lpProcInfo			*/<br />
		);<br />
		<br />
		hProcess = pi.hProcess; <br />
<br />
		 pid = atoi(lpApplicationName);<br />
<br />
		if ((hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pi.dwProcessId)) == NULL) <br />
		<br />
          printf("\t\t\tUnable to open process, the process does not exist!\n");<br />
       		<br />
		//to avoid crashing<br />
		VirtualProtectEx(<br />
		hProcess,				// handle to process<br />
		&lpAddr,				// address of region of committed pages<br />
		dwSize,					// size of region<br />
		flNewProtect,			// desired access protection<br />
		lpflOldProtect );		// address of variable to get old protection<br />
<br />
	/*	base = 0;				//starts from 0<br />
     while (base < sinfo.lpMaximumApplicationAddress) <br />
	 {<br />
          VirtualQueryEx(hProcess, base, &mbi, sizeof(mbi));<br />
<br />
          if (mbi.State == MEM_COMMIT)<br />
               state = "Used";<br />
          else if (mbi.State == MEM_FREE)<br />
               state = "Free";<br />
          else if (mbi.State == MEM_RESERVE)<br />
               state = "Reserved";<br />
          else<br />
               state = "Unknown";<br />
<br />
        printf("\n\tREGION #%d\nState:%s\nBase Address:%p\nAllocation Base:%p\nAccess Protection: %08x\nRegion \<br />
		Size: %08x\nProtection: %08x\nType: %08x\n", regnum, state, mbi.BaseAddress, mbi.AllocationBase, \<br />
		mbi.AllocationProtect, mbi.RegionSize, mbi.Protect, mbi.Type);<br />
<br />
        base = (LPVOID)((unsigned long)mbi.BaseAddress + (unsigned long)mbi.RegionSize);<br />
          /* make base point to one byte past the end of current region, which is either the beginning of the next region, or the beginning of the gap before the next region. <br />
		  VirtualQueryEx() with this address will tell us about the next region in either case */ <br />
         <br />
	//	 regnum++;<br />
	//	printf("\nProcess ID: ", GetCurrentProcessId());<br />
	//	fprintf(fp,"\nREGION #%d\nState:%s\nBase Address:%p\nAllocation Base:%p\nAccess Protection: %08x\nRegion Size: %08x\nProtection: %08x\nType: %08x\n", regnum, state, mbi.BaseAddress, mbi.AllocationBase, mbi.AllocationProtect, mbi.RegionSize, mbi.Protect, mbi.Type);	<br />
		<br />
	// }				<br />
	break;<br />
			case 2:<br />
			<br />
/*if (pi.dwThreadId == NULL)<br />
					<br />
printf("No process which is running \n");<br />
	<br />
					else*/<br />
<br />
SuspendThread(pi.hThread);// identifies thread to suspend<br />
		<br />
			printf("\n\t\tThe thread of ID: %d",pi.dwProcessId);<br />
			printf("\t\t has been suspended!\n");<br />
			GetLastError();<br />
			<br />
			GetThreadContext(<br />
			hProcess,       // handle to thread with context<br />
			lpContext   // address of context structure<br />
			);<br />
			GetLastError();<br />
<br />
			/*regnum = 0;<br />
			base = 0;<br />
<br />
			fp = fopen("info.bin","w");<br />
			if( fp )<br />
<br />
			while (base < sinfo.lpMaximumApplicationAddress) <br />
	 {<br />
          VirtualQueryEx(hProcess, base, &mbi, sizeof(mbi));<br />
<br />
          if (mbi.State == MEM_COMMIT)<br />
               state = "Used";<br />
          else if (mbi.State == MEM_FREE)<br />
               state = "Free";<br />
          else if (mbi.State == MEM_RESERVE)<br />
               state = "Reserved";<br />
          else<br />
               state = "Unknown";<br />
			<br />
		printf("\nREGION #%d\nState:%s\nBase Address:%p\nAllocation Base:%p\nAccess Protection: %08x\nRegion\<br />
		Size: %08x\nProtection: %08x\nType: %08x\n", regnum, state, mbi.BaseAddress, mbi.AllocationBase, \<br />
		mbi.AllocationProtect, mbi.RegionSize, mbi.Protect, mbi.Type);<br />
<br />
          base = (LPVOID)((unsigned long)mbi.BaseAddress + (unsigned long)mbi.RegionSize);<br />
          /* make base point to one byte past the end of current region, which is either the beginning of the next region, or the beginning of the gap before the next region. <br />
		  VirtualQueryEx() with this address will tell us about the next region in either case */ <br />
        // regnum++;<br />
		 <br />
		//printf("Enter the file-name to save the Memory Information: ",fname);<br />
		//scanf ("%s",fname);<br />
<br />
		//fprintf(fp,"\nREGION #%d\nState:%s\nBase Address:%p\nAllocation Base:%p\nAccess Protection: %08x\nRegion \<br />
		Size: %08x\nProtection: %08x\nType: %08x\n", regnum, state, mbi.BaseAddress, \<br />
		mbi.AllocationBase, mbi.AllocationProtect, mbi.RegionSize, mbi.Protect, mbi.Type);	<br />
		<br />
		//}//inner while<br />
			<br />
break;<br />
	<br />
			case 3:<br />
				<br />
ResumeThread(pi.hThread);// identifies thread to restart<br />
GetLastError();	 <br />
// return ResumeThread(pi.hThread);<br />
<br />
break;<br />
			<br />
			case 4:<br />
printf("\n\t\t\tYou are about to terminate a running process, do you want to continue ( y or n) ");<br />
scanf ("%s",ans);<br />
					<br />
result = strcmp(ans,"y");<br />
<br />
if (result==0)<br />
				<br />
TerminateProcess(pi.hProcess, 0);//identifies the process to terminate<br />
					GetLastError();<br />
				<br />
break;<br />
<br />
case 5://checkpointing<br />
				<br />
SuspendThread(pi.hThread);// identifies thread to suspend<br />
		<br />
printf("\n\t\tThe thread of ID: %d",pi.dwProcessId);<br />
printf(" \t\thas been suspended!\n");<br />
GetLastError();<br />
			<br />
GetThreadContext(<br />
hProcess,       // handle to thread with context<br />
lpContext   // address of context structure<br />
);<br />
GetLastError();<br />
<br />
	ZeroMemory(buf, sizeof(buf));<br />
if( ReadProcessMemory( hProcess,	//handle to the process to be suspended<br />
				&baseaddr,						//address to start reading<br />
				&buf,							//buffer where to place data to<br />
				bufsize,						//size of the buffer<br />
				NULL ) == FALSE )<br />
			/*{<br />
printf("\nProcess memory read failed", GetLastError());<br />
				_exit(1);<br />
			}<br />
		else<br />
		{<br />
			printf("\nProcess memory read: \n",buf);<br />
		}*/<br />
	<br />
//fp = fopen(f"checkpoint.bin","w");<br />
//if( fp )<br />
<br />
//printf("Enter the file-name to save the Memory Information: ",fname);<br />
		//scanf ("%s",fname);<br />
			<br />
			regnum = 0;<br />
			base = 0;<br />
		<br />
		while (base < sinfo.lpMaximumApplicationAddress) <br />
<br />
	 {<br />
          VirtualQueryEx(hProcess, base, &mbi, sizeof(mbi));<br />
<br />
          if (mbi.State == MEM_COMMIT)<br />
               state = "Used";<br />
          else if (mbi.State == MEM_FREE)<br />
               state = "Free";<br />
          else if (mbi.State == MEM_RESERVE)<br />
               state = "Reserved";<br />
          else<br />
               state = "Unknown";<br />
			<br />
		printf("\n\t\tREGION #%d\nState:%s\nBase Address:%p\nAllocation Base:%p\nAccess Protection: %08x\nRegion \<br />
		Size: %08x\nProtection: %08x\nType: %08x\n", regnum, state, mbi.BaseAddress, mbi.AllocationBase, \<br />
		mbi.AllocationProtect, mbi.RegionSize, mbi.Protect, mbi.Type);<br />
<br />
          base = (LPVOID)((unsigned long)mbi.BaseAddress + (unsigned long)mbi.RegionSize);<br />
          /* make base point to one byte past the end of current region, which is either the beginning of the next region, or the beginning of the gap before the next region. <br />
VirtualQueryEx() with this address will tell us about the next region in either case */ <br />
         regnum++;<br />
		<br />
		/*fprintf(fp,"\nREGION #%d\nState:%s\nBase Address:%p\nAllocation Base:%p\nAccess Protection: %08x\nRegion \<br />
Size: %08x\nProtection: %08x\nType: %08x\n", regnum, state, mbi.BaseAddress, \<br />
mbi.AllocationBase, mbi.AllocationProtect, mbi.RegionSize, mbi.Protect, mbi.Type);	*/<br />
<br />
		}//inner while<br />
<br />
		/*Sleep(3600);<br />
<br />
TerminateProcess(pi.hProcess, 0);*/<br />
		break;<br />
<br />
case 6://restarting the process using information from the file<br />
<br />
lpBaseAddress = 0;<br />
hProcess = mbi.BaseAddress;<br />
<br />
SetThreadContext( <br />
hThread, <br />
lpContext <br />
);			<br />
GetThreadContext(<br />
hThread,       // handle to thread with context<br />
lpContext   // address of context structure<br />
				);<br />
				//fp = fopen("info.bin","r");<br />
				//if( fp )<br />
nSize = 10000;	//numbers of bytes in the file<br />
<br />
lpBuffer =	buf;	//pointer to the file where data is to be read<br />
<br />
WriteProcessMemory(<br />
hThread,		// handle to process whose memory is written to<br />
lpBaseAddress,			// address to start writing to--dwDesiredAccess<br />
lpBuffer,				// pointer to buffer to write data to<br />
nSize,		// number of bytes to write<br />
lpNumberOfBytesWritten  // actual number of bytes written<br />
);<br />
<br />
printf("Numbers of bytes written: ",lpNumberOfBytesWritten);<br />
CreateThread(<br />
lpThreadAttributes, // pointer to security attributes<br />
dwStackSize,        // initial thread stack size<br />
lpStartAddress,     // pointer to thread function<br />
lpParameter,        // argument for new thread<br />
dwCreationFlags,    // creation flags<br />
lpThreadId          // pointer to receive thread ID<br />
);<br />
<br />
ResumeThread(hThread);// identifies thread to restart(dwDesiredAccess)<br />
GetLastError();<br />
break;<br />
<br />
case 7:<br />
<br />
printf ("\n");<br />
printf ("\t\t\t*****************************************\n");<br />
printf ("\t\t\t*\t\t\t\t\t*\n\t\t\t*\t\tPROCESS\t\t\t*\n");<br />
printf ("\t\t\t*\t\t\t\t\t*\n\t\t\t*  CHECKPOINTING AND MIGRATION SYSTEM\t*\n");<br />
printf ("\t\t\t*\t\t\t\t\t*\n");<br />
printf ("\t\t\t=========================================\n\n");<br />
printf ("\t\t\t<<>>Create a Process:<<>>\n");<br />
printf ("\t\t\tThis option is used to create an executable file specified\<br />
							  by the user.\n");<br />
printf ("\t\t\tThe file should be in the same directory of the systemt\<br />
							  or it should and executable file from Windows system directory\n");<br />
printf ("\t\t\t-------------------------------------------------------------\n\n");<br />
printf ("\t\t\t<<>>Stop Process:<<>>\n");<br />
printf ("\t\t\tThe SuspendThread function suspends the specified thread \n");<br />
<br />
printf ("\t\t\t<<>>The ResumeThread:<<>> \n");<br />
printf ("\t\t\tThe function decrements a thread's suspend count. \n");<br />
printf ("\t\t\tWhen the suspend count is decremented to zero, the execution of\<br />
		the thread is resumed.\n");<br />
printf ("\t\t\t----------------------------------------------------------------\n\n");<br />
printf ("\t\t\t<<>>Shutdown:<<>> \n");<br />
printf ("\t\t\tThe option terminates the process, and exits with the supplied\<br />
status code.\n");<br />
<br />
printf ("\t\t\t<<>>checkpoint:<<>> \n");<br />
printf ("\t\t\tThis option stops a process and save its state in a file \n");<br />
<br />
printf ("\t\t\t<<>>Restart:<<>> \n");<br />
printf ("\t\t\tThis option restarts the execution of the process from where it left\n");<br />
printf ("\t\t\toff when it was checkpointed\n");<br />
printf ("\t\t\t------------------------------------------------------------------\n\n");<br />
printf ("\t\t\t<<>>Help:<<>> \n");<br />
printf ("\t\t\tGives information about how different options do in the system\n");<br />
<br />
printf ("\t\t\t<<>>Quit:<<>> \n");<br />
printf ("\t\t\tThis option quites the whole system. If there are any running\<br />
processes it will also terminate them while they are still running\n");<br />
<br />
printf ("\t\t\t------------------------------------------------------------------\n");<br />
&sinfo;<br />
<br />
break;<br />
<br />
case 8:<br />
printf("\n\t\t\tAre you sure you want to quit the system!(y or n) ");<br />
scanf ("%s",ans);<br />
					<br />
result = strcmp(ans,"y");	//comparing the input and y to quit<br />
				<br />
if (result==0)<br />
					<br />
{<br />
TerminateProcess(pi.hProcess, 0);//identifies the process to terminate<br />
GetLastError();<br />
exit(EXIT_SUCCESS);//exiting the console<br />
}GetLastError();				<br />
break;<br />
					<br />
default: printf("\n\t\t\tYou have entered an invalid choice, check the menu below: \n");<br />
GetLastError();						<br />
}//switch<br />
}	//while base	<br />
fclose(fp);	<br />
 return(0);<br />
 }<br />


-oam-
GeneralRe: menu and sub menu Pin
Christian Graus10-Apr-05 16:03
protectorChristian Graus10-Apr-05 16:03 
GeneralRe: menu and sub menu Pin
mpapeo10-Apr-05 16:16
mpapeo10-Apr-05 16:16 
GeneralRe: menu and sub menu Pin
Christian Graus10-Apr-05 16:28
protectorChristian Graus10-Apr-05 16:28 
GeneralRe: menu and sub menu Pin
mpapeo10-Apr-05 16:43
mpapeo10-Apr-05 16:43 
Generalrun my program from DOS Pin
includeh1010-Apr-05 10:45
includeh1010-Apr-05 10:45 
GeneralRe: run my program from DOS Pin
Ravi Bhavnani10-Apr-05 11:14
professionalRavi Bhavnani10-Apr-05 11:14 
GeneralTernary Operation Pin
Ganesh_Srim10-Apr-05 9:49
Ganesh_Srim10-Apr-05 9:49 
GeneralRe: Ternary Operation Pin
Joaquín M López Muñoz10-Apr-05 10:10
Joaquín M López Muñoz10-Apr-05 10:10 
GeneralRe: Ternary Operation Pin
Ganesh_Srim11-Apr-05 8:32
Ganesh_Srim11-Apr-05 8:32 
GeneralA regular expression needed Pin
John R. Shaw10-Apr-05 9:47
John R. Shaw10-Apr-05 9:47 
GeneralRe: A regular expression needed Pin
Michael Dunn10-Apr-05 10:25
sitebuilderMichael Dunn10-Apr-05 10:25 
GeneralRe: A regular expression needed Pin
John R. Shaw10-Apr-05 11:06
John R. Shaw10-Apr-05 11:06 
GeneralRe: A regular expression needed Pin
Michael Dunn10-Apr-05 12:05
sitebuilderMichael Dunn10-Apr-05 12:05 
GeneralRe: A regular expression needed Pin
John R. Shaw10-Apr-05 13:03
John R. Shaw10-Apr-05 13:03 
GeneralDundas UltimateTCP40 - question Pin
#realJSOP10-Apr-05 5:22
professional#realJSOP10-Apr-05 5:22 
GeneralParasolid Pin
4apai10-Apr-05 4:05
4apai10-Apr-05 4:05 
GeneralMessageBox in other languages Pin
a_kiani10-Apr-05 3:45
a_kiani10-Apr-05 3:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.