我知道下面的代码曾经是C,但是,我把它写在Visual Studio 2008中作为一个C++程序,它工作正常(它保存为C++)。但是,该程序是在C代码中,对吗? (或是否?)。因此,当我试图在Visual Studio中将其编译为C(Go to> Properties of file - > c/C++ - > Advanced - > Compile as - >将其更改为'Compile as C code')然后得到很多错误,其中主要不识别LPSTR类型。所以,我想我的问题是:是C或C++代码,如果它是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还是C++的一种方法是使用C或C++编译器编辑迂腐标志。这将消除任何编译器扩展或其他编译器违规行为,这些行为将允许非标准的C或C++编译。 – mydogisbox 2012-04-07 12:15:01