|
-
March 10th, 2009, 07:53 AM
#1
Error marshalling data
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
Code:
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;
}
...
}
Code:
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:
-
March 10th, 2009, 05:51 PM
#2
Re: Error marshalling data
Basically the structure (which you haven't shown) has types in it that .NET can't automatically turn into native types.
Post the structure and maybe I can help further.
Darwen.
-
March 10th, 2009, 05:54 PM
#3
Re: Error marshalling data
Oops - sorry, just noticed your structure (or class) in your post above.
Firstly - it's a class which won't work with PtrToStructure because (and no prizes for guessing this one) ITS NOT A STRUCTURE ! ITS A CLASS !
OK you might not know that there are big differences between structures and classes in .NET - unlike C++ where they're the same things.
FYI PtrToStructure works with structures. Not classes.
Turn it into a struct and try again.
Darwen.
-
August 20th, 2009, 06:57 AM
#4
Re: Error marshalling data
Even after changing it to Struct if still the problem repeats, then there might be mismatch between the structure fields between application and the unmanaged side
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|