.NET Questions and Solutions

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘microsoft visual c’

PRB: Exception Code 0xE06D7363 When Calling Win32 SEH APIs

Symptoms
When you call Win32 Structured Exception Handling (SEH) APIs, such asGetExceptionCode() and GetExceptionInformation(), sometimes the systemreports the following exception code:

0xE06D7363 Sometimes GetLastError() also returnsthis value. This exception code will be used for any error that is raised by the Microsoft Visual C++ compiler through a call to “throw”.
Resolution
All Visual C++ exceptions thrown from code generated by the MicrosoftVisual C++ compiler contain this error code. Because this is a compiler-generated error, the code is not listed in the Win32 API header files. The code is actually a cryptic mnemonic device, with the initial “E” standing for “exception” and the final 3 bytes (0×6D7363) representing the ASCII values of “msc”.

How to use structured exception handling in Visual C# .NET and in Visual C# 2005 or Visual C# 2008

Symptoms
This article describes how to use structured exception handling in Microsoft Visual C# .NET, in Microsoft Visual C# 2005, or in Microsoft Visual C# 2008.

RequirementsThe following list outlines the recommended hardware, software, network infrastructure, and service packs that you need: Visual C# .NET or later versions of Visual C#
This article assumes that you are familiar with the following topics: Visual C# .NET or later versions of Visual C#Microsoft Visual Studio .NETor or later versions of Visual Studio
Structured Exception HandlingVisual C# .NET offers structured exception handling that provides a powerful and a more readable way to handle errors. Structured exception handling enables you to nest error handlers inside other error handlers that are in the same procedure. Structured exception handling uses a block syntax that is similar to the If…Else…End If statement. This makes Visual C# .NET code or code in later versionsof Visual C# more readable and easier to maintain. You can handle exceptions in Visual C# .NET or in later versions Visual C# by using a combination of exception handling statements:trycatchfinallythrow The basic syntax of structured error handling is as follows:

try{// Code that is expected to raise an exception.}catch(Exception e){// Code that can handle an error.}finally{// Code to do any final cleanup.}You can include any valid Visual C# code in the try block, or you can include another try block or another hierarchy of try blocks. When an exception occurs at any point, the common language runtime looks for the nearest try block that encloses this code and does not run any additional lines of code. The control is then passed to a matching catch block, if one exists, and to the associated finally block. You can also specify multiple catch statements so that each catch block handles a specific error.

Catch an ExceptionIn Microsoft Visual Studio.NET, click Start, point to Programs, point to Microsoft Visual Studio .NET, and then click Microsoft Visual Studio .NET.
In Microsoft Visual Studio 2005, click Start, point to Programs, point toMicrosoft Visual Studio 2005, and then click Microsoft Visual Studio 2005.
In Microsoft Visual Studio 2008, click Start, point to Programs, point toMicrosoft Visual Studio 2008, and then click Microsoft Visual Studio 2008..On the File menu, point to New, and then click Project.In the New Project dialog box, follow these steps: Under Project Types, click Visual C# Projects.
Note In Visual Studio 2005 or in Visual Studio 2008, click Visual C# under Project Types.Under Templates, click Console Application.In the Name box, type MyConsoleApp.In the Location box, type C:\, and then click OK.Add the following code in the Main() function:

int a = 0; int b = 10; int c = 0; try { a = b / c; } catch (Exception e) { Console.WriteLine(“A run-time error occurred.”); } finally { Console.ReadLine(); }To run the application, click Start on the Debug menu. The code tries to divide a number by 0. This operation is not valid and causes a divide-by-zero error. However, the catch block catches this error, and the Console window displays the following error message:

A run-time error occurredClose the Console window.
Catch Multiple ExceptionsThis section demonstrates how to use multiple catch statements to handle different errors. Open the Console Application project that you created in the “Catch an Exception” section of this article. Replace the existing code in the Main() function with the following code:

int a = 2147483647; int b = 0; int c = 0; try { a = checked(a + 1); } catch (DivideByZeroException e) { Console.WriteLine(“Error: Divide by Zero”, e.Message); } catch (OverflowException e) { Console.WriteLine(“Error: Overflow”, e.Message); } finally { Console.ReadLine(); }This code includes two catch blocks:One catch block catches the previous divide-by-zero error.One catch block catches the new overflow error.To run the application, click Start on the Debug menu. The Console window displays the following error message:

Error: OverflowClose the Console window.Because you cannot always anticipate every error that may occur, you can add a catch block for all exceptions that you cannot anticipate. For example, add the following code before the finally statement to catch any errors that you cannot anticipate andto display the appropriate error message:

catch (Exception e) { Console.WriteLine(“Error: “, e.Message); }On the File menu, click Close Solution.
Throw an ExceptionStructured exception handling uses the catch statement to catch an exception. With structured exception handling, you can also throw an exception. For example, you may find it useful to throw an exception when you perform data validation inside a Property Set procedure, because you may want to throw an error if a business rule is violated. In Microsoft Visual Studio.NET, click Start, point to Programs, point to Microsoft Visual Studio .NET, and then click Microsoft Visual Studio .NET.
In Microsoft Visual Studio 2005, click Start, point to Programs, point toMicrosoft Visual Studio 2005, and then click Microsoft Visual Studio 2005.
In Microsoft Visual Studio 2008, click Start, point to Programs, point toMicrosoft Visual Studio 2008, and then click Microsoft Visual Studio 2008..On the File menu, point to New, and then click Project.In the New Project dialog box, follow these steps: Under Project Types, click Visual C# Projects.
Note In Visual Studio 2005 or in Visual Studio 2008, click Visual C# under Project Types.Under Templates, click Console Application.In the Name box, type MyNewConsoleApp.In the Location box, type C:\, and then click OK.On the Project menu, click Add Class.In the Add New Item dialog box, type clsPerson.cs in the Name box, and then click Open.
Note In Visual Studio 2005 or in Visual Studio 2008, click Add.Add the following code in the clsPerson class:

public clsPerson() { } private int mintAge; public int Value; public int Age { get { Age = mintAge; return Age; } set { if(Value > 0) mintAge = Value; else throw new ArgumentException(“Age cannot be negative.”); } }This code creates an Age property. Because a person cannot have a negative age, an error is raised if the user of the class tries to set the Age property to a number that is less than 0. In the Main() function of Class1.cs, add the following code:

clsPerson p = new clsPerson(); try { p.Age = -1; } catch (Exception e) { Console.WriteLine(e.Message); } finally { Console.ReadLine(); }To run the application, click Start on the Debug menu. The Console window displays the following error message:

Age cannot be negativeClose the Console window.
Complete Code ListingCatch an Exception

using System;namespace MyConsoleApp{ class Class1 { [STAThread]static void Main(string[] args) { int a = 0; int b = 0; int c = 0; try { a = b / c; } catch (Exception e) { Console.WriteLine(“A run-time error occurred.”); } finally { Console.ReadLine(); } }}}Catch Multiple Exceptions

using System;namespace MyConsoleApp{ class Class1 { [STAThread] static void Main(string[] args) { int a = 2147483647; try { a = checked(a + 1); } catch (DivideByZeroException e) { Console.WriteLine(“Error: Divide by Zero”, e.Message); } catch (OverflowException e) { Console.WriteLine(“Error: Overflow”, e.Message); } catch (Exception e) { Console.WriteLine(“Error: “, e.Message); } finally { Console.ReadLine(); } } }}Throw an Exception

using System;namespace MyNewConsoleApp{ class Class1 { [STAThread] static void Main(string[] args) { clsPerson p = new clsPerson(); try { p.Age = -1; } catch (Exception e) { Console.WriteLine(e.Message); } finally { Console.ReadLine(); } } }public class clsPerson { public clsPerson() { } private int mintAge; public int Value; public int Age { get { Age = mintAge; return Age; } set { if(Value > 0) mintAge = Value; else throw new ArgumentException(“Age cannot be negative.”); } } }}

Error message when you use the binaries from the SQL Server System CLR Types package (SQLSysClrTypes.msi): “Unable to load DLL ‘SqlServerSpatial.dll’: This application has failed to start because …

Symptoms
After you install the SQL Server System CLR Types package (SQLSysClrTypes.msi), you receive the following error message when you try to use the binaries that this package installs:

Unable to load DLL ‘SqlServerSpatial.dll’: This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0×800736B1)Additionally, the following information is logged in the event log:
EventID: 32
Description: Dependent Assembly Microsoft.VC80.CRT could not be found and Last Error was The referenced assembly is not installed on your system.
EventID: 59
Description: Resolve Partial Assembly failed for Microsoft.VC80.CRT. Reference error message: The referenced assembly is not installed on your system.
EventID: 59
Description: Generate Activation Context failed for C:\WINDOWS\system32\SqlServerSpatial.dll. Reference error message: The operation completed successfully.
Resolution
This problem occurs because the C Run-Time (CRT) library is not installed. The binary files in the SQL Server System CLR Types package rely on the CRT library.
Note The CRT library is included in the Microsoft Visual C++ 2008 Redistributable Package.

BUG: You receive a “System.Runtime.InteropServices.COMException” message when you bind an ImageList ActiveX control to a CoolBar ActiveX control

Symptoms
In Microsoft Visual Studio 2005 or in Microsoft Visual Studio .NET, when you set the ImageList property of the CoolBar ActiveX control to the ImageList ActiveX control, and then you run the application, you receive the following exception:

An unhandled exception of type ‘System.Runtime.InteropServices.COMException’ occurred in axinterop.comctl3.dll
Additional information: Invalid property value. Note You cannot set the ImageList property of the CoolBar ActiveX control in the Properties window.
Resolution
Microsoft Visual Basic 2005, Microsoft Visual Basic .NET, and Microsoft Visual C# .NET interpret the ImageList property of the CoolBar ActiveX control as a read-only property. Therefore, Visual Basic 2005, Visual Basic .NET, and Visual C# .NET throw an exception when you set the ImageList property of the CoolBar ActiveX control to the ImageList ActiveX control.

BUG: Various errors may occur when you try to call managed code from unmanaged code in Visual C++ .NET 2003

Symptoms
You have a Microsoft Visual C++ .NET 2003 application that uses multiple application domains. When you try to call managed code from unmanaged code in a DLL that has already been loaded in another application domain, you may receive the following error message:

An unhandled exception of type ‘System.NullReferenceException’ occurred in mscorlib.dll
Additional information: Object reference not set to an instance of an object.Note In some scenarios, you may receive one of the following error messages:

DllNotFoundException

Illegal Instruction
Resolution
You may notice this problem if your application contains unmanaged code and uses multiple application domains.
For example, if you have an application that uses multiple application domains that use a set of DLLs, you can successfully load and use these DLLs in the first application domain. However, when you try to call the managed code in one of these DLLs from unmanaged code in a different (second) application domain, the behavior that is mentioned in the “Symptoms” section of this article occurs. The application does not load the managed code in the second application domain because the code has already been loaded in another application domain that is in the same process.

BUG: Error message when you try to pass a Collection object from Visual Basic 6.0 components to Visual Basic 2005 or to Visual Basic .NET: “System.InvalidCastException”

Symptoms
When you try to pass a Collection object fromMicrosoft Visual Basic 6.0 components to Microsoft Visual Basic 2005 or to Microsoft Visual Basic .NET, you may receive an error message. In Microsoft Visual Studio 2005, you receive the following error message:

An unhandled exception of type ‘System.InvalidCastException’ occurred in ApplicationName.exe
Additional information: Unable to cast object of type ‘Microsoft.VisualBasic.Collection’ to type ‘VBA.Collection’.In Microsoft Visual Studio .NET, you receive the following error message:

An unhandled exception of type ‘System.InvalidCastException’ occurred in ApplicationName.exe
Additional information: Specified cast is not valid.If you examine the type of the collection object that Visual Basic 2005 or Visual Basic .NET expects, you find that Visual Basic 2005 or Visual Basic .NET expects the VBA.Collection type instead of the Microsoft.VisualBasic.Collection type. If you change your code to pass a collection object of the VBA.Collection type, you receive the following error message on the line of code where you try to create a new instance of the VBA.Collection class:

An unhandled exception of type ‘System.Runtime.InteropServices.COMException’ occurred in ApplicationName.exe
Additional information: COM object with CLSID {A4C4671C-499F-101B-BB78-00AA00383CBB} is either not valid or not registered.This problem also occurs in other Microsoft .NET Framework-supported languages such as Microsoft Visual C# 2005 and earlier versions of .NET Framework-supported Microsoft Visual C#.
Resolution
The InvalidCastException error occurs because the Microsoft.VisualBasic.Collection type is incompatible with the VBA.Collection type. The COMException error occurs because only a Visual Basic 6.0 application can create an instance of the VBA.Collection class. You cannot create an instance of the VBA.Collection class outside a Visual Basic 6.0 application.