为什么我不能在Windows 7测试返回code? [英] Why can't I test the return code in Windows 7?

查看:95
本文介绍了为什么我不能在Windows 7测试返回code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个失败一个非常复杂的程序,我就简化为这个测试用的批处理文件和C程序设置的。

我的C程序使用了ExitProcess的ERRORLEVEL传回一个批处理文件。有时,在Windows 7(的Microsoft Windows [版本6.1.7600])时,错误级别是不是被跨$ P $正确PTED。

我想这应该永远运行。在Windows XP似乎永远运行。在两个不同的双核Windows 7的机器(一个64位的一个32位)没有一两分钟之内。

我无法想象我做错事,但如果没有对Windows 7的一些有趣的关于ExitProcess的,我想我会问。有什么在这里,我已经非法办?

有关cmd.exe的test.bat批处理文件:

  @ECHO OFF
SET I = 0
:通过
SET / A I = I + 1
标题%I%
启动/等待level250
如果错误级别251转到失败
如果错误级别250转到通
:失败

程序level250.c:

 的#includeWINDOWS.H静态挥发INT终止= 0;静态无符号__stdcall TestThread(void *的使用)
    {
    终止= 1;
    返回0;
    }INT WINAPI的WinMain(HINSTANCE的hInstance,HINSTANCE ^ h prevInstance,LPSTR lpszCmdLine,诠释的nCmdShow)
    {
    的CreateThread(NULL,0,TestThread,NULL,0,NULL);    而(终止== 0)睡眠(1);
    ExitProcess的(250);
    }

我的编译器版本和调用是:


  

微软(R)32位C / C ++优化编译器版本12.00.8804为80×86


  
  

版权所有(C)微软公司1984-1998。保留所有权利。


  
  

CL / MT level250.c


其他信息:我自己也尝试下JPSoft的TCC运行,并获得相同的行为与使用CMD。我使用的是直的.C程序,而不是的.cpp。我看不出在单线程版本的任何故障。我把源代码和二进制文件在 http://jcook.info/win7fail 和zip文件MD5是579F4FB15FC7C1EA454E30FDEF97C16B和CRC32是C27CB73D。

修改的建议后,我已经改变了进一步的测试案例,仍然可以看到该故障。在我们的实际应用中,有数百个线程。某些线程退出各种显著收益codeS,一些运行永远的,有些是挂在操作系统调用或DLL,而且很难(如果不是不可能的)杀了。

 的#includeWINDOWS.H静态无符号__stdcall TestThread(void *的使用)
    {
    返回0;
    }INT WINAPI的WinMain(HINSTANCE的hInstance,HINSTANCE ^ h prevInstance,LPSTR lpszCmdLine,诠释的nCmdShow)
    {
    的CreateThread(NULL,0,TestThread,NULL,0,NULL);
    返回(250);
    }


解决方案

这似乎是在返回的时候线程的结果是失败。我线程的返回值改为37,并添加回声%ERRORLEVEL%到批处理文件的末尾。当它停在我的电脑上,它印37.因此,似乎有某种同步的问题。要修复它,我改变了code主为以下内容:

 手柄H =的CreateThread(NULL,0,TestThread,NULL,0,NULL);
而(终止== 0)睡眠(1);
WaitForSingleObject的(H,INFINITE);
ExitProcess的(250);

了ExitProcess 该文件明确指出,退出code是的进程和所有线程。因此,它似乎有一个bug,但是,依靠了ExitProcess杀死所有的线程似乎不是最好的计划。所以,等待他们完成大概是行动的一个合理的过程。

我建立的程序,并用VC6(您使用我相信版),VS2005,VS2008和转载的问题。我跑了,出于好奇,在一个2芯win7的笔记本电脑和一个4芯win7的台式机。它没有重现旧的单核超线程XP的机器上,但是这并不是说,它不会最终它失败;也许它需要只是运行不再出现了。

修改这将是一个有点杂牌的,但也许是一个解决方法是将存储退出code在应用程序中的全局变量和所有的线程返回值。然后在此问题/错误发生的情况下,该应用程序的退出code仍然是所希望的值。

I have a very complicated program that is failing, and I've simplified it to this test set with a batch file and C program.

My C program uses ExitProcess to pass back the errorlevel to a batch file. Sometimes on Windows 7 (Microsoft Windows [Version 6.1.7600]), the errorlevel is not being interpreted correctly.

I think this should run forever. On Windows XP it appears to run forever. On two different dual-core Windows 7 machines (one 64-bit one 32-bit) it fails within a couple minutes.

I can't imagine that I'm doing something wrong, but in case there is something funny about ExitProcess on Windows 7, I thought I'd ask. Is there anything here I've done illegally?

Batch file test.bat for cmd.exe:

@ECHO OFF
SET I=0
:pass
SET /A I=I+1
Title %I%
start/wait level250
if errorlevel 251 goto fail
if errorlevel 250 goto pass
:fail

Program level250.c:

#include "windows.h"

static volatile int Terminate = 0;

static unsigned __stdcall TestThread(void * unused)
    {
    Terminate = 1;
    return 0;
    }

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
    {
    CreateThread(NULL, 0, TestThread, NULL, 0, NULL);

    while (Terminate == 0) Sleep(1);
    ExitProcess(250);
    }

My compiler version and invocation are:

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86

Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

cl /MT level250.c

Other information: I have also tried running under JPSoft's TCC and get the same behavior as using CMD. I am using a straight .c program, not .cpp. I do not see any failures in a single threaded version. I put the sources and binaries on http://jcook.info/win7fail and the zip file MD5 is 579F4FB15FC7C1EA454E30FDEF97C16B and CRC32 is C27CB73D.

EDIT After suggestions, I have further changed the test case and still see the failures. In our actual application, there are hundreds of threads. Some threads exit with various significant return codes, some run forever, and some are hung in operating system calls or dlls and are difficult (if not impossible) to kill.

#include "windows.h"

static unsigned __stdcall TestThread(void * unused)
    {
    return 0;
    }

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
    {
    CreateThread(NULL, 0, TestThread, NULL, 0, NULL);
    return(250);
    }

解决方案

It appears to be returning the result of the thread in the times that it fails. I changed the return value of the thread to 37 and added echo %errorlevel% to the end of the batch file. When it stopped on my PC, it printed 37. So it seems that there is some kind of synchronization problem. To fix it, I changed the code in main to the following:

HANDLE h = CreateThread(NULL, 0, TestThread, NULL, 0, NULL);
while (Terminate == 0) Sleep(1);
WaitForSingleObject( h, INFINITE );
ExitProcess(250);

The documentation for ExitProcess clearly says that the exit code is "for the process and all threads". So it would seem that there is a bug, however, relying on ExitProcess to kill all the threads doesn't seem like the best plan. So waiting for them to finish is probably a reasonable course of action.

I built the program and reproduced the problem with VC6 (the version you used I believe), VS2005, and VS2008. I ran it, out of curiosity, on a 2-core win7 laptop and a 4-core win7 desktop machine. It did not reproduce on an older single-core hyperthreaded XP machine, but that isn't to say that it would not eventually fail on it; maybe it needed to just run longer there.

Edit It would be a bit of kludge, but perhaps a workaround would be to store the exit code in a global variable in the application and return that value from all threads. Then in the situation where this problem/bug occurs, the exit code of the application would still be the desired value.

这篇关于为什么我不能在Windows 7测试返回code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆