RasConnection..::..GetProjectionInfo Method

Oct 25, 2013 at 6:33 PM
I am getting an exception when I try to get projection info for RasProjectionType.PPP. I am using the DotRas.dll for Windows 7 and running on a Win 7 system.

Below is the information I have gathered:

FatalExecutionEngineError was detected
Message: The runtime has encountered a fatal error. The address of the error was at 0xf1a6d6c0, on thread 0xc30. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

Call Stack:
internal static class Utilities
    /// <summary>
    /// Marshals data from an unmanaged memory block to a newly allocated managed object.
    /// </summary>
    /// <typeparam name="T">The type of managed object.</typeparam>
    /// <param name="ptr">A pointer to an unmanaged block of memory.</param>
    /// <returns>The managed object.</returns>
    public static T PtrToStructure<T>(IntPtr ptr)
        T result = default(T);

            result = (T)Marshal.PtrToStructure(ptr, typeof(T));   <---------__Error occurs here__
            return result;
            DiagnosticTrace.Default.TraceEvent(TraceEventType.Verbose, new MarshalStructTraceEvent(result));

Which is called at this point:

if (WIN7 || WIN8)

    /// <summary>
    /// Retrieves extended information about a remote access projection operation for a connection.
    /// </summary>
    /// <param name="handle">The handle of the connection.</param>
    /// <returns>The resulting projection information, otherwise null reference (<b>Nothing</b> in Visual Basic) if the protocol was not found.</returns>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling", Justification = "The method uses a large switch statement to generate the appropriate return type.")]
    public object GetProjectionInfoEx(RasHandle handle)
        object retval = null;

        int size = Marshal.SizeOf(typeof(NativeMethods.RAS_PROJECTION_INFO));

        IntPtr lpdwSize = new IntPtr(size);
        bool retry = false;

            NativeMethods.RAS_PROJECTION_INFO projectionInfo = new NativeMethods.RAS_PROJECTION_INFO();
            projectionInfo.version = GetCurrentApiVersion();

            IntPtr pRasProjection = IntPtr.Zero;
                pRasProjection = Marshal.AllocHGlobal(lpdwSize);
                Marshal.StructureToPtr(projectionInfo, pRasProjection, true);

                int ret = SafeNativeMethods.Instance.GetProjectionInfoEx(handle, pRasProjection, ref lpdwSize);
                if (ret == NativeMethods.ERROR_BUFFER_TOO_SMALL)
                    retry = true;
                else if (ret == NativeMethods.SUCCESS)
                    projectionInfo = Utilities.PtrToStructure<NativeMethods.RAS_PROJECTION_INFO>(pRasProjection);

                    // Use the object located at the end of the structure since the union will cause portability issues on 64-bit platforms.
                    IntPtr pInfo = new IntPtr(pRasProjection.ToInt64() + size);

                    switch (projectionInfo.type)
                        case NativeMethods.RASPROJECTION_INFO_TYPE.Ppp:
Calling point -----> NativeMethods.RASPPP_PROJECTION_INFO ppp = Utilities.PtrToStructure<NativeMethods.RASPPP_PROJECTION_INFO>(pInfo);
                            ReadOnlyCollection<byte> interfaceIdentifier = null;
                            if (ppp.interfaceIdentifier != null && ppp.interfaceIdentifier.Length > 0)
                                interfaceIdentifier = new ReadOnlyCollection<byte>(new List<byte>(ppp.interfaceIdentifier));
My app is in VB and the call I am making is:
      Dim pppinfo As RasPppInfo = CType(mPPPConn.GetProjectionInfo(RasProjectionType.Ppp), RasPppInfo)
Other calls to GetProjectionInfo, such as for lcp, IP or ccp succeed with no problem, and the information is returned correctly.

Any suggestions?