Exceptions in Windows PowerShell, other dynamic languages, and dynamically executed C# code when code that targets the .NET Framework calls some methods

Article translations Article translations
Article ID: 2909958 - View products that this article applies to.
Expand all | Collapse all

On This Page

Symptoms

When code that targets the Microsoft .NET Framework 4.5.1 or the Microsoft .NET Framework 4.5.2, and the code dynamically binds to methods (for example, using a script in Windows PowerShell, IronPython, IronRuby, or another dynamic language) calls the System.Runtime.InteropServices.Marshal.SizeOf method or the System.Runtime.InteropServices.Marshal.PtrToStructure method, you may experience the following issues.

Note Code that is already compiled into a managed executable does not exhibit these issues, unless that code uses the dynamic keyword in C#.

Issue 1

Calls to System.Runtime.InteropServices.Marshal.SizeOf throw the following MethodInvocationException exception:

Type '<Type name>' cannot be marshalled as an unmanaged structure; no meaningful size or offset can be computed.

Issue 2

Calls to System.Runtime.InteropServices.Marshal.PtrToStructure throw the following MethodInvocationException exception:

The specified structure must be blittable or have layout information.

Issue 3

Calls to System.Runtime.InteropServices.Marshal.PtrToStructure throw the following RuntimeBinderException exception:

Cannot implicitly convert type 'void' to 'object'.

Cause

This issue occurs because scripting engines and dynamic languages may bind to a new overload that is introduced in the .NET Framework. Specifically, calls that formerly used Marshal.SizeOf(Type) may now call Marshal.SizeOf<T>(T), and calls that used Marshal.PtrToStructure(IntPtr, Type) may now call Marshal.PtrToStructure<T>(IntPtr, T). This change causes the methods or the runtime binder to throw an exception.

Workaround

To work around this issue, change the code so that it uses the correct overload if your language enables you to do this. If you cannot specify a specific method overload, change the code so that it uses the new method overload correctly instead.

For C# Dynamic Invocation

Add a cast to System.Type within the call to method SizeOf or method PtrToStructure. For example:

object obj = System.Runtime.InteropServices.Marshal.PtrToStructure(ptr, (System.Type)type);
int size = System.Runtime.InteropServices.Marshal.SizeOf((System.Type)type);

Note This is only necessary when one of the arguments to the method is dynamic.

For Windows PowerShell scripts

Add a cast to System.Type within the call to method SizeOf or method PtrToStructure. For example:

$size = [System.Runtime.InteropServices.Marshal]::SizeOf([System.Type] $type)

$obj = [System.Runtime.InteropServices.Marshal]::PtrToStructure($ptr, [System.Type] $type)

For IronPython scripts

Create a new type instance of the type, and then use the new method overload. For example:

typeInstance = type()

size = System.Runtime.InteropServices.Marshal.SizeOf(typeInstance)

obj = System.Runtime.InteropServices.Marshal.PtrToStructure(ptr, typeInstance)

Status

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

References

Properties

Article ID: 2909958 - Last Review: May 6, 2014 - Revision: 5.0
Applies to
  • Microsoft .NET Framework 4.5.2
  • Microsoft .NET Framework 4.5.1, when used with:
    • Windows Vista Service Pack 2
    • Windows 7 Service Pack 1
    • Windows Server 2008 Service Pack 2
    • Windows Server 2008 R2 Service Pack 1
    • Windows 8
    • Windows Server 2012 Datacenter
    • Windows Server 2012 Essentials
    • Windows Server 2012 Foundation
    • Windows Server 2012 Standard
    • Windows Server 2012 R2 Datacenter
    • Windows Server 2012 R2 Essentials
    • Windows Server 2012 R2 Foundation
    • Windows Server 2012 R2 Standard
    • Windows 8.1
Keywords: 
kbsurveynew kbtshoot kbexpertiseadvanced KB2909958

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com