Tuesday, March 22, 2011

incorrectly aligned or overlapped by a non-object field

Hi All,

Thanks for taking out time to read this post. Can someone help me with this?

When I try to run this, System.TypeLoadExeception Error appears.
What I am trying to do is, run a native function that is in dll. It requires lots of datatypes namely :-
1) ATTRIBUTE_TYPE
2) DIAG_SOCKADDR
3) LIFE_TIME
4) FILETIME
5) An Union which I have named as “unionForHelpAttribute”
6) HELPER_ATTRIBUTE

The function I am trying to call is NdfCreateIncident

So, when I try to run the code this exception is happening

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:

Here is the source code.


public enum ATTRIBUTE_TYPE
{

/// AT_INVALID -> 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->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->_FILETIME
public FILETIME startTime;

/// FILETIME->_FILETIME
public FILETIME endTime;
}

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

/// DWORD->unsigned int
public uint dwLowDateTime;

/// DWORD->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->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->__int64
public long Int64;

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

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


//GUID->_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->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);

No comments:

Node.JS rest api Tutorials