文档导航目录(5 专栏 / 65 篇)

3-进程的创建过程

win32基础 更新于 2024-03-16 1 分钟 0 次阅读 0 条评论

什么是进程?

进程提供程序所需的资源,如:数据,代码等。

进程的创建:

  1. 任何进程都是别的进程创建的:调用CreateProcess()函数
  2. 进程的创建过程

​

article image

进程创建API

c++
BOOL CreateProcess(
    LPCTSTR lpApplicationName,// pointer to name of executable module
    LPTSTR lpCommandLine,// pointer to command line string
    LPSECURITY_ATTRIBUTES lpProcessAttributes,// pointer to process security attributes
    LPSECURITY_ATTRIBUTES lpThreadAttributes,// pointer to thread security attributes 
    BOOL bInheritHandles,// handle inheritance flag
    DWORD dwCreationFlags,// creation flags
    LPVOID lpEnvironment,// pointer to new environment block
    LPCTSTR lpCurrentDirectory,// pointer to current directory name
    LPSTARTUPINFO lpStartupInfo,// pointer to STARTUPINFO
    LPPROCESS_INFORMATION lpProcessInformation// pointer to PROCESS_INFORMATION
);
c++
#include <windows.h>
#include <stdio.h>

int main()
{
	STARTUPINFO si;
	PROCESS_INFORMATION pi;

	TCHAR app[] = TEXT("C:\\Windows\\notepad.exe");
	TCHAR arg[] = TEXT(" D:\\desktop\\win32基础\\gbk.txt");  //注意要传入命令行参数,前面必须有个空格
    //参考:https://blog.csdn.net/xjbclz/article/details/51834706

	ZeroMemory(&si, sizeof(si));  //初始化结构体
	ZeroMemory(&pi, sizeof(pi));

	si.cb = sizeof(si);  //记录si结构体的大小,必须写
	bool CP = CreateProcess(
		app,
		arg,
		NULL,
		NULL,
		FALSE,
		0,
		NULL,
		NULL,
		&si,
		&pi
	);
	if (!CP) {
		printf("CreateProcess failed (%d).\n", GetLastError());
		return 0;
	}

	// Wait until child process exits.
	WaitForSingleObject(pi.hProcess, INFINITE);

	// Close process and thread handles. 
	CloseHandle(pi.hProcess);
	CloseHandle(pi.hThread);
	return 0;
}

评论讨论 (0) Artalk 自托管