MSVCRT系统函数返回代码始终为-1

MSVCRT系统函数返回代码始终为-1,c,windows-7,msvcrt,C,Windows 7,Msvcrt,是什么导致MSVCRTsystem()函数始终返回-1错误代码,即使应用程序已成功执行并退出,并返回0作为退出代码 我用TDM-GCC-4.9.2和FASM进行了测试,它们只调用system()并打印返回代码,它们都打印-1,所以这不是我的开发环境 另外,errno在调用system()和GetLastError返回18之后是0,这很奇怪 事实上,我系统上使用system()的每个应用程序现在都假定它失败了 因此,这是MSVCRT的一个全球性问题,我似乎无法在任何其他机器上复制,请提供一些帮助

是什么导致MSVCRT
system()
函数始终返回-1错误代码,即使应用程序已成功执行并退出,并返回0作为退出代码

我用TDM-GCC-4.9.2和FASM进行了测试,它们只调用
system()
并打印返回代码,它们都打印-1,所以这不是我的开发环境

另外,
errno
在调用
system()
GetLastError
返回18之后是0,这很奇怪

事实上,我系统上使用
system()
的每个应用程序现在都假定它失败了

因此,这是MSVCRT的一个全球性问题,我似乎无法在任何其他机器上复制,请提供一些帮助

编辑:在一些调试之后,
系统
调用
\u spawnve
,后者依次调用
CreateProcessA
,然后调用
WaitForSingleObject
。可执行文件终止后,它调用
GetExitCodeProcess
,返回-1,然后将其反馈到链中

编辑2:经过一些测试后,如果调用的进程返回0,则系统似乎只返回-1,否则返回正确的值

编辑3:只是澄清一下,即使
系统
返回-1,调用的进程也会成功执行

编辑4:以下是我的测试来源。
始终返回0成功的:

#include <stdio.h>

int main( int argc, char* argv[argc]) {
    printf("success\n");
    return 0;
}
编辑5:由于
system
调用
\u spawnve
调用
CreateProcess
我尝试了所有调用,当调用
cmd/c test\u success
时,它们都返回-1,但当调用
cmd/c test\u失败时,它们工作正常。
因此,这似乎是一个更深层次的问题,与
系统
没有直接关系

编辑6:经过一番讨价还价,我将
ComSpec
改为
C:\Windows\winsxs\amd64\u microsoft-Windows-commandprompt\u 31bf3856ad364e35\u 6.1.7601.17514\u none\u e932cc2c30fc13b0\cmd.exe
,现在一切都正常了!
这有点奇怪,因为我使用的是Intel Core 2 Duo,这可能不是正确的做法,但我仍然感到满意:p

以下是“system()”手册中关于返回值的说明

   The value returned is -1 on  error  (e.g.,  fork(2)  failed),  and  the
   return  status  of the command otherwise.  This latter return status is
   in the format specified in wait(2).  Thus, the exit code of the command
   will  be  WEXITSTATUS(status).   In case /bin/sh could not be executed,
   the exit status will be that of a command that does exit(127).

   If the value of command is NULL, system() returns nonzero if the  shell
   is available, and zero if not.

   system() does not affect the wait status of any other children.

下面是“system()”手册中关于返回值的内容

   The value returned is -1 on  error  (e.g.,  fork(2)  failed),  and  the
   return  status  of the command otherwise.  This latter return status is
   in the format specified in wait(2).  Thus, the exit code of the command
   will  be  WEXITSTATUS(status).   In case /bin/sh could not be executed,
   the exit status will be that of a command that does exit(127).

   If the value of command is NULL, system() returns nonzero if the  shell
   is available, and zero if not.

   system() does not affect the wait status of any other children.

我给自己加了一个答案,因为我认为这样做了。< /P>
<> > <代码> >系统> 32·CM.exe < /C> > >代码> C:\Windows \WINSXS\AMD64μFieldSCOMPRINDPROPTSPY31,BF38 56AD364E35Y61.7601.17514IONEYE22E92C2C30FC13B0\CMD.exe < /代码>解决了我在伪退出代码中遇到的所有问题。


ComSpec
C:\Windows\System32\cmd.exe
更改为
C:\Windows\winsxs\amd64\u microsoft-Windows-commandprompt\u 31bf3856ad364e35\u 6.1.7601.17514\u none\u e932cc2c30fc13b0\cmd.exe
解决了我使用假退出代码时遇到的所有问题。

你试过进入系统吗()与调试器一起运行以查看发生了什么?另外,请检查ComSpec环境变量的值,并确保
cmd.exe
本身没有以某种方式损坏。@HarryJohnston:ComSpec是“C:\Windows\System32\cmd.exe”,并使用/C递归调用它可以正常工作。我将尝试调试它…您是否尝试使用
GetLastError()
并检查错误代码?@GingerJack:GetLastError返回18个“没有更多文件”,这根本没有任何意义。请显示您的确切代码。如果它是
system(“ls”)
,那么您的测试是错误的,因为ls在Windows/cmd.exe上不工作。您是否尝试过使用调试器进入system()函数以查看发生了什么?另外,请检查ComSpec环境变量的值,并确保
cmd.exe
本身没有以某种方式损坏。@HarryJohnston:ComSpec是“C:\Windows\System32\cmd.exe”,并使用/C递归调用它可以正常工作。我将尝试调试它…您是否尝试使用
GetLastError()
并检查错误代码?@GingerJack:GetLastError返回18个“没有更多文件”,这根本没有任何意义。请显示您的确切代码。如果它是
system(“ls”)
,那么您的测试是错误的,因为ls在Windows/cmd.exe上不工作。
success
success == -1 0
failure
failure == 1 0
   The value returned is -1 on  error  (e.g.,  fork(2)  failed),  and  the
   return  status  of the command otherwise.  This latter return status is
   in the format specified in wait(2).  Thus, the exit code of the command
   will  be  WEXITSTATUS(status).   In case /bin/sh could not be executed,
   the exit status will be that of a command that does exit(127).

   If the value of command is NULL, system() returns nonzero if the  shell
   is available, and zero if not.

   system() does not affect the wait status of any other children.