Unhandled Exception - incorrect structure size was detected

Feb 20, 2011 at 6:30 PM

Hello Jeff :)

We have taken the DotRas SDK, and based on it built an application which creates dialer, dials disconnects and more.

Everything worked perfectly, until we needed to access the InnerException of a dial attempt (eg. detect whether it was 691, 789 etc).

We figured, that to access the inner exception, we needed the DotRas source code.

So we downloaded it, imported the project, and referenced it in our application project.

Now we could write: MessageBox.Show(ex.InnerException.ToString()); inside the RasDialer.cs, to start inspecting the inner exception.

However, when we tried to run it, we have got this exception:

 

DotRas.RasException was unhandled
  Message=An incorrect structure size was detected.
  Source=DotRas
  ErrorCode=632
  StackTrace:
       at DotRas.Internal.ThrowHelper.ThrowRasException(Int32 errorCode) in C:\Program Files\DotRas SDKs\v1.2\Examples\WinForms\CreateAndDialVpnEntry\CS\DotRas v1.2 Source\Source\DotRas\DotRas\Internal\ThrowHelper.cs:line 124
       at DotRas.Internal.RasHelper.GetEntryProperties(RasPhoneBook phoneBook, String entryName) in C:\Program Files\DotRas SDKs\v1.2\Examples\WinForms\CreateAndDialVpnEntry\CS\DotRas v1.2 Source\Source\DotRas\DotRas\Internal\RasHelper.cs:line 1039
       at DotRas.RasEntryCollection.Load() in C:\Program Files\DotRas SDKs\v1.2\Examples\WinForms\CreateAndDialVpnEntry\CS\DotRas v1.2 Source\Source\DotRas\DotRas\RasEntryCollection.cs:line 134
       at DotRas.RasPhoneBook.Open(String phoneBookPath) in C:\Program Files\DotRas SDKs\v1.2\Examples\WinForms\CreateAndDialVpnEntry\CS\DotRas v1.2 Source\Source\DotRas\DotRas\RasPhoneBook.cs:line 291
       at DotRas.RasPhoneBook.Open(Boolean openUserPhoneBook) in C:\Program Files\DotRas SDKs\v1.2\Examples\WinForms\CreateAndDialVpnEntry\CS\DotRas v1.2 Source\Source\DotRas\DotRas\RasPhoneBook.cs:line 262
       at DotRas.RasPhoneBook.Open() in C:\Program Files\DotRas SDKs\v1.2\Examples\WinForms\CreateAndDialVpnEntry\CS\DotRas v1.2 Source\Source\DotRas\DotRas\RasPhoneBook.cs:line 245
       at DotRas.Samples.CreateAndDialVpnEntry.MainForm.MainForm_Shown(Object sender, EventArgs e) in C:\Program Files\DotRas SDKs\v1.2\Examples\WinForms\CreateAndDialVpnEntry\CS\CreateAndDialVpnEntry\MainForm.cs:line 318
       at System.Windows.Forms.Form.OnShown(EventArgs e)
       at System.Windows.Forms.Form.CallShownEvent()
       at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at DotRas.Samples.CreateAndDialVpnEntry.Program.Main() in C:\Program Files\DotRas SDKs\v1.2\Examples\WinForms\CreateAndDialVpnEntry\CS\CreateAndDialVpnEntry\Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
       at System.Activator.CreateInstance(ActivationContext activationContext)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 


We are not sure how to fix this, the other discussions about this error are obsolete and seem to be irrelevant.

Attached is https://disk.ccccloud.com/v2/files/79FBE61C-3D27-11E0-A2F4-003048CFCC1E/content/inline/CS.rar of our whole project, in case it would help figure out what we did :)

 

Coordinator
Feb 20, 2011 at 11:19 PM

You don't need the source code to see any errors, anything that happens inside the project is bubbled up as a RasException with the error code being what Windows indicated from the Win32 APIs.  If you're dialing asynchronously the dialer notifies what errors are occuring from dialing in the DialCompleted event. If any errors occur while dialing asynchronously you can retrieve them from the DialCompleted event on the DialCompletedEventArgs instance passed to it. The error property will contain the information. Here's a brief example of what I'm talking about:

private RasDialer dialer;

private void Dial()
{
    if (this.dialer == null)
    {
        dialer = new RasDialer();
        dialer.DialCompleted += new EventHandler<DialCompletedEventArgs>(this.dialer_DialCompleted);
    }

    dialer.EntryName = "My Connection";
    dialer.Credentials = new NetworkCredential("Test", "User");
    dialer.DialAsync();
}

private void dialer_DialCompleted(object sender, DialCompletedEventArgs e)
{
    if (e.Error != null)
    {
        // You can use the error information here.
    }
}

As for the 632 error code, that means the structure sizes aren't what the version of Windows you're using is expecting. I build the application with the WIN7 compilation symbol set which means everything compiles for the Windows 7 operating system. If you want to build the project for a different operating system, you need to go to the DotRas project properties, under the Build options, and change WIN7 to one of the following: WIN2K, WINXP, WIN2K8, WIN7. You can find more about the features from each of the settings in the help file under Getting Started - Choosing your build.

Feb 21, 2011 at 10:55 AM

Thank you Jeff, we found the place you meant - however I cant find the necessary information, here is a sample debug information when I intentionally cause the dialer to fail due to wrong username/pass (error 691):

Free Image Hosting at www.ImageShack.us

Free Image Hosting at www.ImageShack.us

As you can see, the InnerException here is null :( and there is no other error code such as the 691 user sees when using the dialer the classic way.

Our only other alternative is to parse the error exception msg string to figure out the type of error, which is very wrong way I think.

Coordinator
Feb 21, 2011 at 6:35 PM

The error code 691 is showing up on the exception. You're looking at the properties on the Exception base class, the ErrorCode property exists on the RasException class that derives from Exception. Here I was trying to track down a bug and wasted a good 30 minutes of time.

Screenshot

Feb 23, 2011 at 4:19 PM
jeff_winn wrote:

The error code 691 is showing up on the exception. You're looking at the properties on the Exception base class, the ErrorCode property exists on the RasException class that derives from Exception. Here I was trying to track down a bug and wasted a good 30 minutes of time.

Screenshot

 

Jeff I am very sorry if that has got you checking it out for 30 mins! For others who may look at this in the future, the solution was: 

switch ((int)((RasException)e.Error).ErrorCode)
That would give you the pure error number.
Was not trivial to me at least, and my friend who has 7+ years .net experience, and thought we needed to reference the source code to see that InnerException :)

Thanks Jeff!
Coordinator
Feb 24, 2011 at 12:50 AM
Edited Feb 24, 2011 at 12:57 AM

No problem, the reason that the e.Error property doesn't return a RasException directly is because an exception not RAS related might be thrown from underneath the hood of the dialer component. The DialCompletedEventArgs class passed to the event also extends the AsyncCompletedEventArgs class which is part of the .NET framework as part of the event-based asynchronous pattern. So I wouldn't have the ability to change the type returned by the Error property even if I wanted to.

Edit:

I thought you might like to know your application will crash if you always assume that e.Error contains a RasException, it would be better to test for it like so:

using DotRas;

private void dialer_DialCompleted(object sender, DialCompletedEventArgs e)
{
    if (e.Error is RasException)
    {
        // You can now be sure it's a RasException and avoid the potential cast exception from being thrown.
        switch (((RasException)e.Error).ErrorCode)
        {
            case 691:
                break;
        }
    }
    else
    {
        // Do whatever you want if an exception other than a RasException has been thrown.
    }
}

 Hopefully that'll take care of your problems.