我知道下面的代码曾经是 C 语言,但是,我在 Visual Studio 2008 中将其编写为 C++ 程序,并且运行良好(保存为 C++)。但是,该程序是 C 代码,对吗? (或者是吗?)。
因此,当我尝试在 Visual Studio 中将其编译为 C 时(转到 -> 文件属性 -> c/c++ -> 高级 -> 编译为 -> 将其更改为“编译为 C 代码”),然后我得到很多错误,其中最主要的是它无法识别 LPSTR 类型。所以,我想我的问题是:它是 C 还是 C++ 代码,如果是 C,为什么当我将其更改为编译 C 代码时它不起作用?
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <strsafe.h>
#include <direct.h>
#include <string.h>
#include <conio.h>
int main(VOID)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
//allocate memory
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
//create child process
if (!CreateProcess(NULL,
L"C:\\Windows\\Notepad.exe",
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi))
{
fprintf(stderr, "create process failed");
return -1;
}
//parent waits for child to complete
WaitForSingleObject(pi.hProcess, INFINITE);
printf("Child Complete");
//close handle
CloseHandle(pi.hProcess);
CloseHandle(pi.hthread);
}
最佳答案
它可以像 C 一样干净地编译。唯一的错误是由 CloseHandle(pi.hthread)
触发的,因为它不是 PROCESS_INFORMATION
的成员。您正在寻找 hThread
(大写 T)。
关于c++ - 代码是C还是C++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10054349/