6.1创建进程6.2子进程 [编码问题]

2017-09-14  本文已影响0人  f675b1a02698

//创建进程,获取进行相关信息,获取启动参数

#include

#include

DWORD CreateChildProcess(LPTSTR szChildProcessCmd);

int main(int argc, PCHAR argv[]){

CreateChildProcess(TEXT("6.2子进程.exe misakanumber 99999"));

return 0;

}

//参加子进程

DWORD CreateChildProcess(LPTSTR szChildProcessCmd){

STARTUPINFO si;

PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));//将启动信息结构清零

si.cb = sizeof(si);//设置结构大小

ZeroMemory(&pi, sizeof(pi));//将进程信息结构清零

//创建子进程,判断是否成功

if (!CreateProcess(

NULL,//使用命令行

szChildProcessCmd,//命令行

NULL,//不继承进程句柄

NULL,//不继承线程句柄

FALSE,//不继承句柄

0,//没有创建标志

NULL,//使用父进程环境变量

NULL,//使用父进程目录作为当前目录

&si,//启动信息结构

&pi//进程信息结构

)){

printf("创建进程失败: %d\n", GetLastError());

return 1;

}

//创建成功后,父进程也可以直接退出,这里等待子进程执行结束

//hProcess为子进程句柄

WaitForSingleObject(pi.hProcess, INFINITE);

//关闭进程句柄和现成句柄

CloseHandle(pi.hProcess);

CloseHandle(pi.hThread);

return 0;

}

源码

#include

#include

#define MyAlloc(size) HeapAlloc(GetProcessHeap(),0,size)

#define MyFree(lpMem) HeapFree(GetProcessHeap(),0,lpMem)

typedef struct _PROCESS_INFO{

DWORD dwPid;

HANDLE hProcess;

DWORD dwPrioClass;

DWORD dwHandleCount;

DWORD dwAffinityMask;

SIZE_T dwWorkingSetSizeMax;

SIZE_T dwWorkingSetSizeMin;

LPWSTR szwCommandLine;

STARTUPINFO sti;

}PROCESS_INFO,*LPPROCESS_INFO;

//全局变量

HANDLE hMySelf;

DWORD GetProcessInfo(LPPROCESS_INFO lppi);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow){

PROCESS_INFO pi;

INT argc;

WCHAR **argv;

INT i;

DWORD dwBufferSize = lstrlen(lpCmdLine) + MAX_PATH + 1024;

LPWSTR szShowBuffer = MyAlloc(dwBufferSize);

hMySelf = hInstance;

//显示从WinMain函数参数得到的信息

wsprintf(szShowBuffer, TEXT("启动参数:\n实例句柄: %.8X, 命令行参数: %s, 显示选项: %.8X"), hInstance, lpCmdLine, nCmdShow);

MessageBox(NULL, szShowBuffer, TEXT("WinMain函数参数信息"), MB_OK);

//使用自定义函数获取相关信息

GetProcessInfo(&pi);

//将命令行参数分离

argv = CommandLineToArgvW(pi.szwCommandLine, &argc);

//字符显示

*szShowBuffer = (WCHAR)NULL;

for (i = 0; i< argc; i++){

DWORD dwBufferSize = lstrlenW(*argv) + 1;

LPSTR szMBAargv = MyAlloc(dwBufferSize);

WideCharToMultiByte(CP_ACP, 0,*argv, -1, szMBAargv, dwBufferSize, NULL, NULL);

argv++;

lstrcat(szShowBuffer, TEXT("\n"));

lstrcat(szShowBuffer, szMBAargv);

MyFree(szMBAargv);

}

MessageBoxW(NULL, szShowBuffer, TEXT("参数"), MB_OK);

MyFree(szShowBuffer);

//打印其他信息 TODO

return 0;

}

//获取进程相关信息,保存到PROCESS_INFO结构中

DWORD GetProcessInfo(LPPROCESS_INFO lppi){

lppi->dwPid = (DWORD)GetCurrentProcessId;//PID

lppi->hProcess = GetCurrentProcess();//句柄

lppi->dwPrioClass = GetPriorityClass(hMySelf);//优先级

//句柄计数,xp sdk新增的API...无语,现在都win7了

//lppi->dwHandleCount = GetProcessHandleCount(lppi->hProcess, &lppi->dwHandleCount);

//AffinityMask

GetProcessAffinityMask(hMySelf, &lppi->dwAffinityMask, NULL);

//WorkingSetSize

GetProcessWorkingSetSize(hMySelf, &lppi->dwWorkingSetSizeMin, &lppi->dwWorkingSetSizeMax);

lppi->szwCommandLine = GetCommandLineW();

//启动信息

GetStartupInfo(&lppi->sti);

return 0;

}

上一篇下一篇

猜你喜欢

热点阅读