gpt4 book ai didi

c - 使用 CreateProcess() 启动进程时出现问题

转载 作者:行者123 更新时间:2023-11-30 14:31:23 36 4
gpt4 key购买 nike

问题是这样给出的:

Using the CreateProcess () in the Win32 API. In this instance, you will need to specify a separate program to be invoked from CreateProcess(). It is this separate program that will run as a child process outputting the Fibonacci sequence. Perform necessary error checking to ensure that a non-negative number is passed on the command line.

我已完成以下操作。它不显示任何错误消息。当我尝试执行它时,它自动退出:

#include <sys/types.h>
#include <windows.h>
#define _WIN32_WINNT 0x0501

#include <stdio.h>

int main()
{

STARTUPINFO si;
PROCESS_INFORMATION pi;
int a=0, b=1, n=a+b,i,ii;

ZeroMemory(&si, sizeof(si));

si.cb = sizeof(si);


if(! CreateProcess("C:\\WINDOWS\\system32\\cmd.exe",NULL,NULL,NULL,FALSE,0,
NULL,NULL,&si,&pi))
printf("\nSorry! CreateProcess() failed.\n\n");
else{
printf("Enter the number of a Fibonacci Sequence:\n");
scanf("%d", &ii);

if (ii < 0)
printf("Please enter a non-negative integer!\n");
else
{
{
printf("Child is producing the Fibonacci Sequence...\n");
printf("%d %d",a,b);
for (i=0;i<ii;i++)
{
n=a+b;
printf("%d ", n);
a=b;
b=n;
}
printf("Child ends\n");
}

{
printf("Parent is waiting for child to complete...\n");
printf("Parent ends\n");
}
}
}

WaitForSingleObject(pi.hProcess, 5000);
printf("\n");

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

return 0;
}

我做错了什么?

最佳答案

我认为你误解了你的锻炼。另外,您可能希望在使用 CreateProcess 时进行句柄继承。这可能超出了您的技能水平,但仍然是一个有用的教训: http://support.microsoft.com/kb/q190351/

关于c - 使用 CreateProcess() 启动进程时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1422038/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com