Found the following interesting discussion in the Newsgroups:
| api call using struct* by:Anonymous | |
| Hail to the Gurus, :) I just read some of the 'api call to tchar*' thread but this is not about my problem. I'm using a dll (eyelink_exptkit20.dll) that I've succesfully used from vb6. Among others I declare: Public Declare Function StartRecording Lib "eyelink_exptkit20.dll" _ Alias "start_recording" (ByVal file_samples As Integer, ByVal file_events As Integer, _ ByVal link_samples As Integer, ByVal link_events As Integer) As Short Public Declare Sub StopRecording Lib "eyelink_exptkit20.dll" Alias "stop_recording" () Public Declare Function NewestFloatSample Lib "eyelink_exptkit20.dll" _ Alias "eyelink_newest_float_sample" (ByRef NewSample As TrackerSample) As Int16 The original c struct of 'TrackerSample' looks like this: typedef struct { UINT32 time; /* time of sample */ INT16 type; /* always SAMPLE_TYPE */ UINT16 flags; /* flags to indicate contents */ // binocular data: indices are 0 (LEFT_EYE) or 1 (RIGHT_EYE) float px[2], py[2]; /* pupil xy */ float hx[2], hy[2]; /* headref xy */ float pa[2]; /* pupil size or area */ float gx[2], gy[2]; /* screen gaze xy */ float rx, ry; /* screen pixels per degree (angular resolution) */ UINT16 status; /* tracker status flags */ UINT16 input; /* extra (input word) */ UINT16 buttons; /* button state & changes */ INT16 htype; /* head-tracker data type (0=noe) */ INT16 hdata[8]; /* head-tracker data (not prescaled) */ } FSAMPLE; My VB6 version of this (which works just fine) looks like this: Public Type TrackerSample Ttime As Long TSampleType As Integer TFlags As Integer TPupilX(0 To 1) As Single TPupilY(0 To 1) As Single THeadX(0 To 1) As Single THeadY(0 To 1) As Single TPupilA(0 To 1) As Single TGazeX(0 To 1) As Single TGazeY(0 To 1) As Single TAngX As Single TAngY As Single TStatus As Integer TInput As Integer TButtons As Integer TDataType As Integer TData(0 To 7) As Integer End Type My VB.NET Structure that I'm Trying to use looks like this: Public Structure TrackerSample Public Ttime As UInt32 Public TSampleType As Int16 Public TFlags As UInt16 Public TPupilX() As Single Public TPupilY() As Single Public THeadX() As Single Public THeadY() As Single Public TPupilA() As Single Public TGazeX() As Single Public TGazeY() As Single Public TAngX As Single Public TAngY As Single Public TStatus As UInt16 Public TInput As UInt16 Public TButtons As UInt16 Public TDataType As Int16 Public TData() As Int16 Public Sub New(ByVal Dummy As Integer) Ttime = Convert.ToUInt32(1) TSampleType = 2 TFlags = Convert.ToUInt16(3) TAngX = 4 TAngY = 5 TStatus = Convert.ToUInt16(6) TInput = Convert.ToUInt16(7) TButtons = Convert.ToUInt16(8) TDataType = 9 Dim Arr() As Single = {-1.1, -1.1} Dim Arr8() As Int16 = {2, 2, 2, 2, 2, 2, 2, 2} ReDim TPupilX(1) Array.Copy(Arr, TPupilX, 2) ReDim TPupilY(1) Array.Copy(Arr, TPupilY, 2) ReDim THeadX(1) Array.Copy(Arr, THeadX, 2) ReDim THeadY(1) Array.Copy(Arr, THeadY, 2) ReDim TPupilA(1) Array.Copy(Arr, TPupilA, 2) ReDim TGazeX(1) Array.Copy(Arr, TGazeX, 2) ReDim TGazeY(1) Array.Copy(Arr, TGazeY, 2) ReDim TData(7) Array.Copy(Arr8, TData, 8) End Sub End Structure I've added the 'New' sub to be able to check on the creation of the structure and it's contents... Now here's the problem: I create an instance of a TrackerSample and a var to get the return values: dim MySample as New TrackerSample(0) dim MyVar as Int16 = -99 When I 'forget' to StartRecording, I correctly get a return value of -1, indicating that there are no samples available. When I do 'StartRecording' (the EyeLink starts tracking correctly!) and call: MyVar = NewestFloatSample(MySample) I get an ERROR stating that: "a reference is not pointing to an instance of an object" ... in other words, the system is complaining that I'm feeding it a NULL pointer. In my debugger Watch-window I can however see that MyVar and all members of MySample have the assigned values, therefore they all do exist. Some of the members of MySample are actually changed by the call before the program bangs-out. WHhat is going on??? I first tried all the type assignments in 'native-VB', using Short instead of INT16 and so on ... No difference... and I've tried an older version of the dll... Same problem..................... HEEEEEEEEEEEEELLLP ! greetz, Peter | |
| Reply: by:Mattias Sjögren | |
Add the attribute <MarshalAs(UnmanagedType.ByValArray, SizeConst:=2)> to the array elements in TrackerSample. Mattias | |
Posted by Xander Zelders

0 Comments:
Post a Comment
<< Home