7

This is only the important stuff that the bluescreen shows. I'm on Windows 7 x64.

"A problem has been detected and Windows has been shut down to prevent damage to your computer.

PROCESS_HAS_LOCKED_PAGES

* STOP: 0x00000076 (0x0000000000000000, 0xfffffa8009dcd060, 0x0000000000000011, 0x0000000000000000)"

I can't work on it now because every time I close it I get a bluescreen! The program doesn't do anything yet except run the background worker below. It pings all addresses that could be part of the user's home network and attempts to connect to a certain port that another program will be listening on.

private void NetworkScanner_DoWork(object sender, DoWorkEventArgs e)
    {
        bool ExceptionEncountered = false;
        int IPsProcessed = 0;

        NetworkSearcherOutput = "Starting network scanner...";
        NetworkSearcher.ReportProgress(0);
        Thread.Sleep(1000);

        foreach (IPAddress IP in Dns.GetHostAddresses(Dns.GetHostName()))
        {
            if (IP.AddressFamily == AddressFamily.InterNetwork)
            {
                string[] Octets = IP.ToString().Split('.');
                Octets[3] = "0";

                IPAddress CurrentAddressIteration = StringArrayToIP(Octets);
                while (GetLastOctet(CurrentAddressIteration) != 255)
                {
                    PingReply Reply = new Ping().Send(CurrentAddressIteration, 5);

                    if (Reply.Status == IPStatus.Success)
                    {
                        NetworkSearcherOutput = CurrentAddressIteration.ToString() + " sent response.";
                        NetworkSearcher.ReportProgress(0);
                        Thread.Sleep(500);

                        InClient Client = new InClient(CurrentAddressIteration);

                        try
                        {
                            Client.Connect();

                            SnapshotBox.Image = Client.Receive(typeof(Image));

                            NetworkSearcherOutput = CurrentAddressIteration.ToString() + " is running program.";
                            NetworkSearcher.ReportProgress(0);
                            Thread.Sleep(1000);
                        }

                        catch (Exception E)
                        {
                            // A socket exception is expected when the client is not running the program.
                            if (E is SocketException)
                            {
                                Client.Close();

                                NetworkSearcherOutput = CurrentAddressIteration.ToString() + " is not running program.";
                                NetworkSearcher.ReportProgress(0);
                                Thread.Sleep(1000);
                            }

                            //Unhandled exception. Show messagebox and close.
                            else
                            {
                                MessageBox.Show("Network scanner encountered an unhandled exception.\n\n" + E.GetType().ToString() + ": " + E.Message, "Unhandled Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                ExceptionEncountered = true;
                                break;
                            }
                        }
                    }

                    else
                    {
                        NetworkSearcherOutput = CurrentAddressIteration.ToString() + " did not respond.";
                        NetworkSearcher.ReportProgress(0);
                    }

                    IPsProcessed++;

                    if (IPsProcessed == 5)
                    {
                        NetworkSearcher.ReportProgress(2);
                        IPsProcessed = 0;
                    }

                    Octets = CurrentAddressIteration.ToString().Split('.');
                    Octets[3] = (Int32.Parse(Octets[3]) + 1).ToString();
                    CurrentAddressIteration = StringArrayToIP(Octets);
                }
            }
        }

        if (!ExceptionEncountered)
        {
            NetworkSearcherOutput = "Network scanning complete.";
            NetworkSearcher.ReportProgress(0);
            NetworkSearcher.ReportProgress(100);
        }

        else
        {
            NetworkSearcherOutput = "Network scanning encountered an error.";
            NetworkSearcher.ReportProgress(-1);
        }

I thought C# programs were supposed to never cause bluescreens?

WildBamaBoy
  • 2,663
  • 6
  • 25
  • 23
  • You're not supposed to be able to cause bluescreens directly. but if the underlying code from Microsoft has a bug it could cause it – William Mioch Nov 02 '11 at 04:26
  • No program is "supposed" to create blue screens... – Andrew Barber Nov 02 '11 at 04:26
  • 4
    http://support.microsoft.com/kb/256010 implies this is related to a driver misbehaving, perhaps a network driver based on your program. This KB indicates ways of indicating which driver is likely at fault; it has nothing to do with your managed code. – Joe Nov 02 '11 at 04:27
  • No, user-mode code can't cause a blue screen, even if it's from Microsoft. Are you running any VPN software, or security software? – John Saunders Nov 02 '11 at 04:27
  • @Andrew, I think you'll find that the ping'o'death _was_ supposed to cause BSODs but, admittedly, that was a special case :-) In any case, what if (like me) you have a class A home network? :-) – paxdiablo Nov 02 '11 at 04:28
  • I once had a piece of VPN software with "endpoint security", that crashed when too many DLLs were opened in a short period of time. We had been blaming Visual Studio, but the same problem happened with a program I wrote to test this theory - it just loaded all the assemblies it could find, in a loop. Only a few loops were necessary to crash the system. – John Saunders Nov 02 '11 at 04:28
  • @paxdiablo - got me there! hehe – Andrew Barber Nov 02 '11 at 04:30
  • I'm not running any VPN or security software. Thank you Joe for the Microsoft link. I should be able to find out what's going on. – WildBamaBoy Nov 02 '11 at 04:31
  • And the culprit is tcpip.sys, should it help anyone else. – WildBamaBoy Nov 02 '11 at 04:38
  • @WildBamaBoy: If this is .NET 4. Known bug. Please see my answer. – leppie Nov 02 '11 at 05:47

2 Answers2

7

I discovered this issue a few weeks back. It only happens when using .NET 4.

Reported at MS Connect.

Edit:

(Will*) Add this link to MS Connect bug report.

*login.live.com is going into an infinite loop again...

leppie
  • 115,091
  • 17
  • 196
  • 297
  • 1
    I just want to verify that yes it only happens in .NET 4. I switched frameworks and the crash no longer happens. However I tested the BSOD causing program on another PC with VS 2010 and nothing happened... [Someone give it a try.](https://connect.microsoft.com/VisualStudio/feedback/details/698441/tcpip-sys-crash-with-managed-code#details) – WildBamaBoy Nov 02 '11 at 21:54
4

Just to be clear, there is no way for "user" mode code to forcibly create a blue screen in windows, unless it uses undocumented APIs and or forces bad data into a driver. Your C# code is likely not be at fault here, as if you use the user mode classes (Socket) then the socket is responsible for not crashing your computer.

As @Joe has commented Microsoft Support KB Article 256010 clearly describes this stop message, but better yet has clear instructions on capturing the driver name responsible for this error.

Note that any software firewall that you have installed also is involved at kernel mode level so could also be responsible for this error. I recommend you follow the KB articles advice and try to find out what is at fault. But you could also ensure that you have updated your network drivers and firewall/VPN software to the latest stable versions.

Spence
  • 28,526
  • 15
  • 68
  • 103
  • Condense your code to a reproduction and submit to MS Connect. You may have just found a bug in Windows 7. – Spence Nov 02 '11 at 04:47
  • Perhaps those who have more wisdom than I can correct me, but I'm fairly sure that Socket is implemented in TcpIp.sys – Spence Nov 02 '11 at 04:50
  • @Spence: Sorry you are incorrect. This is a bug in .NET 4. And already reported. See my answer. – leppie Nov 02 '11 at 05:44
  • 2
    No, it's generating a BSOD. Therefore the bug is either in the windows API, or in a driver accessing the API from Kernel Mode. Your MS connect has clearly shown that the bug occurs after a call to a Windows DLL. Granted the code is using a different API in .Net 4.0, it doesn't make that API any less broken... – Spence Nov 02 '11 at 05:55
  • @leppie: Just because it is .NET 4 that is triggering the bug does not mean that the bug is *in* .NET 4! – Gabe Nov 02 '11 at 06:01
  • @Spence: Or the usage of the API is broken. Either way, this is related to iphlpapi.dll. Not sure if that is being used elsewhere in Windows. – leppie Nov 02 '11 at 06:03
  • Doesn't matter. The Windows Kernel is responsible (either through code or contract) to guarantee the integrity of operating system (i.e hardware, memory, CPU scheduling etc.). If ANY use of an API triggers a BSOD then this API is at fault, provided it is published. The only exception is using an undocumented API to do so, however this is a security issue now, if user mode code can cause a BSOD then this is a Denial of Service attack – Spence Nov 02 '11 at 07:37
  • Note when you are writing kernel mode code (Drivers/Antivirus) then the API will have a contract that must be met, as there is no OS to protect you at this level. – Spence Nov 02 '11 at 07:47