gborges
March 10th, 2009, 07:53 AM
Hi all!
I am trying to code something more efficient, and so, I try do not use a loop in order to marshal the data which the app receives.
It throws an exception which I do not know how to handle it.
Does someone can help me?
I attach here a piece of relevant code and the error message. During the execution of this line:
Marshal.PtrToStructure(dst, finputDoublebuffer);
it occurs an exception.
Thank you in advance
Gustavo
public class ds_param_value_buffer : Object {
public ds_param_value_buffer(int sz) {
values = new ds_param_value[sz];
}
public ds_param_value [] values;
};
...
public class DS {
private ds_param_value_buffer finputDoublebuffer;
public DS() {
...
finputDoublebuffer = new ds_param_value_buffer((int)Constants.DSMAXDATAVISINPUTPARAMS);
}
public static IntPtr IntPtr_Offset(IntPtr src,int offset) {
switch (IntPtr.Size) {
case 4:
return new IntPtr(src.ToInt64() + offset);
case 8:
return new IntPtr(src.ToInt64() + offset);
default:
throw new NotSupportedException("This is running on a machine where pointers are "
+ IntPtr.Size
);
}
}
private void MarshalIncommingData(){
// manual marshalling to convert native incomming data to managed data
IntPtr dst;
dst = IntPtr_Offset(funmanagedinputbuffer,0);
/////////////////
Marshal.PtrToStructure(dst, finputDoublebuffer); //ERROR HERE
/////////////////
return;
}
private ds_status handle_data() {
...
switch(status){
case ds_status.DS_OK:
// Do manual marshalling of the incomming data
MarshalIncommingData();
if (fOnEvtData != null) {
// Fire "on data" event
EvtDataArgs args = new EvtDataArgs(timestamp, ref finputDoublebuffer);
fDispatcher.Invoke(DispatcherPriority.Normal, fOnEvtData, this, args);
args = null;
}
break;
...
}//switch
...
return status;
}
...
}
Error Message:
System.ArgumentException was unhandled
Message="The specified structure must be blittable or have layout information.\r\nParameter name: structure"
Source="mscorlib"
ParamName="structure"
StackTrace:
at System.Runtime.InteropServices.Marshal.PtrToStructureHelper(IntPtr ptr, Object structure, Boolean allowValueClasses)
at System.Runtime.InteropServices.Marshal.PtrToStructure(IntPtr ptr, Object structure)
at DSI.DS.MarshalIncommingData() in DS.cs:line 827
at DSI.DS.handle_data() in DS.cs:line 847
at DSI.DS.handle_event(ds_dv_event_type evt, StringBuilder errmsgbuf) in DS.cs:line 980
at DSI.DS.ds_evt_deamon() in DS.cs:line 1024
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
I am trying to code something more efficient, and so, I try do not use a loop in order to marshal the data which the app receives.
It throws an exception which I do not know how to handle it.
Does someone can help me?
I attach here a piece of relevant code and the error message. During the execution of this line:
Marshal.PtrToStructure(dst, finputDoublebuffer);
it occurs an exception.
Thank you in advance
Gustavo
public class ds_param_value_buffer : Object {
public ds_param_value_buffer(int sz) {
values = new ds_param_value[sz];
}
public ds_param_value [] values;
};
...
public class DS {
private ds_param_value_buffer finputDoublebuffer;
public DS() {
...
finputDoublebuffer = new ds_param_value_buffer((int)Constants.DSMAXDATAVISINPUTPARAMS);
}
public static IntPtr IntPtr_Offset(IntPtr src,int offset) {
switch (IntPtr.Size) {
case 4:
return new IntPtr(src.ToInt64() + offset);
case 8:
return new IntPtr(src.ToInt64() + offset);
default:
throw new NotSupportedException("This is running on a machine where pointers are "
+ IntPtr.Size
);
}
}
private void MarshalIncommingData(){
// manual marshalling to convert native incomming data to managed data
IntPtr dst;
dst = IntPtr_Offset(funmanagedinputbuffer,0);
/////////////////
Marshal.PtrToStructure(dst, finputDoublebuffer); //ERROR HERE
/////////////////
return;
}
private ds_status handle_data() {
...
switch(status){
case ds_status.DS_OK:
// Do manual marshalling of the incomming data
MarshalIncommingData();
if (fOnEvtData != null) {
// Fire "on data" event
EvtDataArgs args = new EvtDataArgs(timestamp, ref finputDoublebuffer);
fDispatcher.Invoke(DispatcherPriority.Normal, fOnEvtData, this, args);
args = null;
}
break;
...
}//switch
...
return status;
}
...
}
Error Message:
System.ArgumentException was unhandled
Message="The specified structure must be blittable or have layout information.\r\nParameter name: structure"
Source="mscorlib"
ParamName="structure"
StackTrace:
at System.Runtime.InteropServices.Marshal.PtrToStructureHelper(IntPtr ptr, Object structure, Boolean allowValueClasses)
at System.Runtime.InteropServices.Marshal.PtrToStructure(IntPtr ptr, Object structure)
at DSI.DS.MarshalIncommingData() in DS.cs:line 827
at DSI.DS.handle_data() in DS.cs:line 847
at DSI.DS.handle_event(ds_dv_event_type evt, StringBuilder errmsgbuf) in DS.cs:line 980
at DSI.DS.ds_evt_deamon() in DS.cs:line 1024
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: