System.TypeLoadException, incorrectly aligned or overlapped by a non-object field
<p>Thanks for taking out time to read this email. Can someone help me with this?</p>

<p>When I try to run this, <code>System.TypeLoadExeception</code> Error appears.<br>
What I am trying to do is, run a native function that is in dll. It requires lots of datatypes namely :</p>

<ol>
<li>ATTRIBUTE_TYPE</li>
<li>DIAG_SOCKADDR</li>
<li>LIFE_TIME</li>
<li>FILETIME</li>
<li>An Union which I have named as “unionForHelpAttribute”</li>
<li>HELPER_ATTRIBUTE</li>
</ol>

<p>The function I am trying to call is <code>NdfCreateIncident</code></p>

<p>So, when I try to run the code this exception is happening:</p>

<pre><code>System.TypeLoadException was unhandled
  Message=Could not load type 'unionForHelpAttribute' from assembly 'NDFApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because it contains an object field at offset 0 that is incorrectly aligned or overlapped by a non-object field.
  Source=NDFApplication1
  TypeName=unionForHelpAttribute
  StackTrace:
       at NDFApplication1.Program.Main(String[] args)
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
</code></pre>

<p>Here is the source code.</p>

<pre><code>public enum ATTRIBUTE_TYPE
    {

        /// AT_INVALID -&gt; 0
        AT_INVALID = 0,
        AT_BOOLEAN,
        AT_INT8,
        AT_UINT8,
        AT_INT16,
        AT_UINT16,
        AT_INT32,
        AT_UINT32,
        AT_INT64,
        AT_UINT64,
        AT_STRING,
        AT_GUID,
        AT_LIFE_TIME,
        AT_SOCKADDR,
        AT_OCTET_STRING,
    }

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Auto, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
    public struct DIAG_SOCKADDR {

        /// USHORT-&gt;unsigned short
        public ushort family;

        /// CHAR[126]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=126)]
        public string data;
    }


    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Auto)]
    public struct LIFE_TIME {

        /// FILETIME-&gt;_FILETIME
        public FILETIME startTime;

        /// FILETIME-&gt;_FILETIME
        public FILETIME endTime;
    }

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Auto)]
    public struct FILETIME {

        /// DWORD-&gt;unsigned int
        public uint dwLowDateTime;

        /// DWORD-&gt;unsigned int
        public uint dwHighDateTime;
    }

   [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Explicit)]
    public  struct unionForHelpAttribute{

        [System.Runtime.InteropServices.FieldOffset(0)]
        public  bool Boolean;

        [System.Runtime.InteropServices.FieldOffset(0)]
        public  char Char;

        [System.Runtime.InteropServices.FieldOffset(0)]
        public  byte Byte;

        [System.Runtime.InteropServices.FieldOffset(0)]
        public  short Short;

        [System.Runtime.InteropServices.FieldOffset(0)]
         //WORD-&gt;unsigned short
        public  ushort Word;

        [System.Runtime.InteropServices.FieldOffset(0)]
        public  int Int;

        [System.Runtime.InteropServices.FieldOffset(0)]
        public uint DWord;

        [System.Runtime.InteropServices.FieldOffset(0)]
         //LONGLONG-&gt;__int64
        public long Int64;

        [System.Runtime.InteropServices.FieldOffset(0)]
         //ULONGLONG-&gt;unsigned __int64
        public ulong UInt64;

         //LPWSTR-&gt;WCHAR*
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
        [System.Runtime.InteropServices.FieldOffset(0)] 
        public string pwszDescription;


         //GUID-&gt;_GUID
        [System.Runtime.InteropServices.FieldOffset(0)] 
        public GUID Guid;

        [System.Runtime.InteropServices.FieldOffset(0)] 
        public LIFE_TIME LifeTime;

        [System.Runtime.InteropServices.FieldOffset(0)] 
        public DIAG_SOCKADDR Address;


        [System.Runtime.InteropServices.FieldOffset(0)]     
        public byte[] octets;

    }

 [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Ansi)]
    public struct HELPER_ATTRIBUTE
    {
        /// LPWSTR-&gt;WCHAR*
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
        public string pwszName;

        public ATTRIBUTE_TYPE type;

        public unionForHelpAttribute Anonymus1;
    }

[DllImport("ndfapi.dll", SetLastError = true)]
    public static extern long NdfCreateIncident(
        string helperClassName,
        ulong celt,
        ref HELPER_ATTRIBUTE attribues,
        out IntPtr NDFHandle
        );

Problempath = “ \\\\desktop\\somedrive “;

HELPER_ATTRIBUTE ha = new HELPER_ATTRIBUTE();
        ha.pwszName = Name;
        ha.type = ATTRIBUTE_TYPE.AT_STRING;

          ha.Anonymus1.pwszDescription = problemPath;

        //converting string to byte array
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

        ha.Anonymus1.octets = new byte[encoding.GetBytes(problemPath).Length];

        ha.Anonymus1.octets = encoding.GetBytes(problemPath);
</code></pre>

<p>Thanks</p>
