locked
GetLastError

    Question

  • I am a vb.net 2003 programmer.  I am working on a web service application which requires me to use C++ routines for autoproxy detection on a client computer.  Specifically, I am using WinInet and Autoproxy C++ routines.  My problem is that it is hard to debug runtime difficulties within my autoproxy C++ routines.  I am familiar with Try/Catch error trapping but "GetLastError" is new to me.  My question is therefore, how should a vb programmer catch C++ errors at runtime.

     

    Thanks,

    Fred

    Thursday, May 25, 2006 5:58 PM

All replies

  • In the Win32 world, calling the GetLastError API is often the mechanism to get additional error information when an API call fails.   What happens is that C++ API are unmanaged code and therefore will not throw an exception as managed .NET code does that you can trap using a try catch.   

    So if will often return a simple value that would determine if it was successful or not and if it wasnt then you would call a second API to determine why it wasnt successful.  In this case the GetLastError API.

    Calling GetLastError directly via PInvoke is not guaranteed to work due to the CLR's internal interaction with the operating system.

    Dim ilasterror As Integer = System.Runtime.InteropServices.Marshal.GetLastWin32Error()

    You can use this after you call to the C++ API if the return type indicates that it was not successful.

    Thursday, May 25, 2006 7:17 PM
  • Spotty's answer is correct in the world of windows apps, but since this is a web service I suspect that you may be referring to the Server.GetLastError() method that returns a System.Exception object.

    The only distinction to make here is that the Server.GetLastError method will return all information about the managed code that failed.  Unfortunetly, the C++ unmanaged code is still only going to give you raw binary data.  I'm not sure that there is a good way for you to actually debug the C++ code.  I'm not even sure that calling the GetLastWin32Error API is valid from the web application/service.

    You should probably move to one of the ASP.NET boards and repost your question there.  You may find that people who work primarly with ASP have been through this same issue and have a workaround.

    Thursday, May 25, 2006 8:04 PM
    Moderator