Подскажите пожалуйста новичку, что не так делаю
Есть С-ая функция:
BOOL sprPeekMessage(HANDLE Handle, LPSPRECORDMESSAGE lpMessage, LPDWORD lpdwSize);
в которую передается структура с union:
typedef struct _SPRECORDMESSAGE {
DWORD cbSize;
SYSTEMTIME MsgTime;
DWORD dwMsgType;
union {
RECORDSTART RecordStart;
NEWRING NewRing;
ANIDETECT ANIDetected;
RECORDRINGTYPE RecordRingType;
NEWOUTPHONE NewOutPhone;
ANIDETECT SkippedPhoneNumber;
RECORDSTOP RecordStop;
NEWCHANNELNAME NewChannelName;
char cbStr[SPRECORD_MAXTEXTSIZE + 1];
};
} SPRECORDMESSAGE, *PSPRECORDMESSAGE, *LPSPRECORDMESSAGE;
Пробовал вызывать эту функцию делать 2-мя вариантами:
[StructLayout(LayoutKind.Sequential)]
public struct SpRecordMessage
{
public int Size;
public DateTime MsgTime;
public int MsgType;
public EventType ev;
//public IntPtr info;
}
[StructLayout(LayoutKind.Explicit)]
public struct EventType
{
[ FieldOffset( 0 )]
public RecordStart recordStart;
[ FieldOffset( 0 )]
public NewRing newRing;
[ FieldOffset( 0 )]
public ANIDetect aniDetected;
[ FieldOffset( 0 )]
public RecordRingType recordRingType;
[ FieldOffset( 0 )]
public NewOutPhone newOutPhone;
[ FieldOffset( 0 )]
public ANIDetect skipedPhoneNumber;
[ FieldOffset( 0 )]
public RecordStop recordStop;
[ FieldOffset( 0 )]
public NewChanelName newChanelName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=4079),FieldOffset( 0 )]
public String cbStr;
}
[DllImport("spclient.DLL", EntryPoint="sprPeekMessage")]
public static extern bool sprPeekMessage(int Handle, ref SpRecordMessage spRecordMessage, int Size);
И собственно вызов:
SpRecordMessage Msg;
Msg=new SpRecordMessage();
Msg.ev.RecordStart.ChannelName="";
Msg.ev.RecordStart.FileName="";
Msg.ev.RecordStart.PhoneNumber="";
Msg.ev.RecordStart.RingType=0;
// EventType ev=new EventType();
// ev.RecordStart.ChannelName="";
// ev.RecordStart.FileName="";
// ev.RecordStart.PhoneNumber="";
// ev.RecordStart.RingType=0;
// IntPtr buffer = Marshal.AllocCoTaskMem( Marshal.SizeOf( ev ));
// Marshal.StructureToPtr( ev, buffer, false );
// spMsg.info=buffer;
sprPeekMessage(ConnectionHandle, ref Msg,Marshal.SizeOf(Msg))
//sprPeekMessage(ConnectionHandle, ref Msg,Marshal.SizeOf(Msg)+ //Marshal.SizeOf(ev))
в результате ошибка: “Object reference not set to an instance of an object”.
Подскажите плз или где можно почитать. И можно ли вообще сделать такой “анонимный” аналог union в С#
Всем спасибо !!!