首页 \ 问答 \ 在STDOUT重定向后,CloseHandle在Studio 2010中抛出异常(CloseHandle throwing an exception in Studio 2010 after STDOUT redirection)

在STDOUT重定向后,CloseHandle在Studio 2010中抛出异常(CloseHandle throwing an exception in Studio 2010 after STDOUT redirection)

从Studio 2010调试器运行时,以下代码引起了悲痛,但似乎从命令行运行正常(好吧,因为异常仅在调试器中抛出):

HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE );  // Preserve STDOUT
HANDLE hFile = CreateFile( pszOutputFile, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
// At this point hFile = 0x000000d0
// The file is created and has 0 bytes, as expected

// STDOUT is redirected to our new file handle
SetStdHandle( STD_OUTPUT_HANDLE, hFile );

// Do something here that pumps to STDOUT, output is directed to the file correctly
CloseHandle( hFile );  // EXCEPTION here: 0xC0000008: An invalid handle was specified.
SetStdHandle( STD_OUTPUT_HANDLE, hCon );  

一个简单的CreateFile / CloseHandle序列工作正常,我也试过切换CloseHandle / SetStdHandle调用的顺序无效,但GetLastError确实返回18(0x12)ERROR_NO_MORE_FILES“没有更多的文件。” 如果SetStdHandle确实运行。

STDOUT重定向的东西失败了,但是什么?


The following code is causing grief when run from Studio 2010 debugger, but seems to run fine from the command line (well, it would because the exception is only thrown in the debugger):

HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE );  // Preserve STDOUT
HANDLE hFile = CreateFile( pszOutputFile, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
// At this point hFile = 0x000000d0
// The file is created and has 0 bytes, as expected

// STDOUT is redirected to our new file handle
SetStdHandle( STD_OUTPUT_HANDLE, hFile );

// Do something here that pumps to STDOUT, output is directed to the file correctly
CloseHandle( hFile );  // EXCEPTION here: 0xC0000008: An invalid handle was specified.
SetStdHandle( STD_OUTPUT_HANDLE, hCon );  

A simple CreateFile/CloseHandle sequence works fine, and I have also tried switching the order of the CloseHandle/SetStdHandle calls to no avail, but GetLastError does return 18 (0x12) ERROR_NO_MORE_FILES "There are no more files." if SetStdHandle does run.

Something with the STDOUT redirection is failing, but what?


原文:https://stackoverflow.com/questions/13916045
更新时间:2023-11-21 16:11

最满意答案

使用Restful Webservices或Web方法从AJAX调用是个好主意。

但是你仍然可以做到以下几点。

使用Response.Write()而不是返回字符串。

使用此代码。

private void test()

{
    var response = new ResponseResult();
    var javaScriptSerializer = new JavaScriptSerializer();
    response.Status = -1;
    response.ErrorMessage = "";
    response.Action = "UserProjects";
    response.Data = "The Username or password you entered is not valid";
    string result = javaScriptSerializer.Serialize(response);

    Response.Clear();
    Response.ContentType = "application/json; charset=utf-8";
    Response.Write(result);  //write json string to output   
}

It is a good idea to use Restful Webservices or Web Method to call from AJAX.

But still you can do the following.

Use Response.Write() instead of returning string.

Use this code.

private void test()

{
    var response = new ResponseResult();
    var javaScriptSerializer = new JavaScriptSerializer();
    response.Status = -1;
    response.ErrorMessage = "";
    response.Action = "UserProjects";
    response.Data = "The Username or password you entered is not valid";
    string result = javaScriptSerializer.Serialize(response);

    Response.Clear();
    Response.ContentType = "application/json; charset=utf-8";
    Response.Write(result);  //write json string to output   
}

相关问答

更多
  • 只需在代码中设置它: protected int PageId; protected void Page_Load(object sender, EventArgs e) { PageId = 12; // some value Image1.ImageUrl ="ImageCSharp.aspx?ImageID=" + PageId; } Just set it in code behind: protected int PageId; protected void Page_Load ...
  • 你的webmethod需要是static 。 [WebMethod] public static Details[] getDetails(string columnname, string inputVal) You webmethod needs to be static. [WebMethod] public static Details[] getDetails(string columnname, string inputVal)
  • 尝试从master或.aspx页面中删除
    。 Try removing the from either your master or .aspx page.
  • 您还需要指定一个设定值。 public partial class _Default : System.Web.UI.Page { string strResponseValue; public string ResponseREST { get { return strResponseValue; } set { strResponseValue = value; } } protected void Page_Load( ...
  • 如何在加载后获取控件的id并将其添加到会话中, control o = LoadControl("~/UserControl/ContactDetails.ascx"); (Session["controlIDList"] as List).Add(o.ID); 你需要先创建会话,也许在page load使用它 Session.Add("controlIDList", new List()); 并从会话中的已保存ID列表中查找控件,然后转换为其类型,并获取所需的值... ...
  • 微软并不真正支持 我和微软讨论了这个问题,这是我从他们那里得到的。 经过进一步调查,结果如下。 由于Intellisense的工作方式,我们无法支持涉及aspx.cs文件后面的共享代码的方案的Intellisense。 可以采用两种不同的方法来处理这种情况。 VS中支持的选项是使用AppCode或UserControll作为公共代码元素,然后调用这些方法来实现公共代码库。 第二个选项涉及以您当前正在使用它的方式(不使用Intellisense)来使用CodeBehind,并假定代码对于这两个设计页都是正确的 ...
  • 使用Restful Webservices或Web方法从AJAX调用是个好主意。 但是你仍然可以做到以下几点。 使用Response.Write()而不是返回字符串。 使用此代码。 private void test() { var response = new ResponseResult(); var javaScriptSerializer = new JavaScriptSerializer(); response.Status = -1; response.Err ...
  • $.ajax({ type: "POST", url: pageMethod, contentType: "application/json; charset=utf-8", data: "{passedVal:" + JSON.stringify(clientRequest) + "}", dataType: "json", success: function (result, status) { alert("success"); ...
  • 所以我找到了一种使用UpdatePanel来实现此目的的方法。 下面是我的.aspx页面中的代码,它使用javascript警报显示后面代码中定义的消息。 我将UpdatePanel添加到我的.aspx页面的最底部。