03 简单的进程与思考

简单的进程:

/**
*CreateProcess.c
*/
#include <stdio.h>
#include <string.h>
#include <windows.h>

//创建子进程
BOOL CreateChildProcess(PTCHAR szChildProcessName, PTCHAR szCmdLine)
{
	STARTUPINFO si;
	PROCESS_INFORMATION pi;

	ZeroMemory(&pi, sizeof(pi));
	ZeroMemory(&si, sizeof(si));
	si.cb = sizeof(si);

	//创建子进程
	if (!CreateProcess(
		szChildProcessName,		//可执行模块名
		szCmdLine,				//命令行
		NULL,					//不继承进程句柄
		NULL,					//不继承线程句柄
		FALSE,					//不继承句柄
		0,						//没有创建标志
		NULL,					//使用父进程的环境变量
		NULL,					//使用父进程目录做为当前,可以自己设置目录
		&si,					//STARTUPINFO结构体详细信息
		&pi						//PROCESS_INFORMATION结构体进程信息
		))
	{
		printf("CreateChildProcess Error:%d\n", GetLastError());
		return FALSE;
	}

	//释放资源
	CloseHandle(pi.hProcess);
	CloseHandle(pi.hThread);
	return TRUE;
}

int main()
{
	PTCHAR szApplicationName = NULL;
	TCHAR szCmdLine[] = TEXT("D://liebao//liebao//liebao.exe");
	CreateChildProcess(szApplicationName, szCmdLine);
	getchar();
	return 0;
}

测试程序如下:

/**
*AntiDebug.c
*/
#include <stdio.h>
#include <string.h>
#include <windows.h>

VOID AntiDebug()
{
	STARTUPINFO si;
	GetStartupInfo(&si);
	printf("%x %x %x %x %x %x %x %x\n",
		si.dwX, si.dwY, si.dwXCountChars, si.dwYCountChars,
		si.dwFillAttribute, si.dwXSize, si.dwYSize, si.dwFlags);
}

int main()
{
	AntiDebug();
	getchar();
	return 0;
}

首先,我们将该程序编译成.exe文件然后双击运行得到如图:
在这里插入图片描述
然后我们用OD将其打开,如下图:
在这里插入图片描述
可以看到两处的值不一样,双击打开只有最后一个值为1,其余全为0;
故而我们可以将程序修改如下:

/**
*AntiDebug.c
*/
#include <stdio.h>
#include <string.h>
#include <windows.h>

VOID AntiDebug()
{
	STARTUPINFO si;
	GetStartupInfo(&si);
	if (si.dwX != 0 || si.dwY != 0 || si.dwXCountChars != 0 || si.dwYCountChars != 0 ||
		si.dwFillAttribute != 0 || si.dwXSize != 0 || si.dwFlags != 1)
	{
		MessageBox(NULL, TEXT("警告"), TEXT("操作非法"), MB_OK);
		exit(0);
	}
	printf("%x %x %x %x %x %x %x %x\n",
		si.dwX, si.dwY, si.dwXCountChars, si.dwYCountChars,
		si.dwFillAttribute, si.dwXSize, si.dwYSize, si.dwFlags);
}

int main()
{
	AntiDebug();
	getchar();
	return 0;
}

然后我们在编译为.exe文件双击运行如下图可以正常运行;
在这里插入图片描述
再使用OD打开则出现下图警告信息,点击确定后程序退出没有往下执行,并退出程序!
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/lifeshave/article/details/84782175
03
今日推荐