Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2003

Tip: Looking for answers? Try searching our database.

Help with MarshalAs statements

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tony Farrell - 13 Sep 2003 16:36 GMT
Hi Everyone -

I have two questions about MarshalAs

1)  I need to setup the correct MarshalAs for this  setup...

   [StructLayout(LayoutKind.Sequential)]
     public struct DateTimeStruct
   {
     public int year;
     public int month;
     public int day;
     public int hour;
     public int minute;
     public int second;
   }

What would the correct MarshalAs statement be?

This is what i'm trying...
     [MarshalAs( UnmanagedType.Struct)]
     DateTimeStruct startTime;

2) Given this struct element -
int samplingInterval[5]

What would the correct MarshalAs statement be?

This is what i'm thinking...
     [MarshalAs( UnmanagedType.ByValArray, SizeConst=5)]
     int samplingInterval;

Would someone please correct my direction on these two questions??

thanks
tony
Kevin Westhead - 13 Sep 2003 16:54 GMT
> I have two questions about MarshalAs
>
[quoted text clipped - 27 lines]
>
> Would someone please correct my direction on these two questions??

1) The struct information alone isn't enough; what is the unmanaged
signature of the function that consumes this struct?

2) Again the unmanaged definition for the callee would be required before
any useful advice could be offered.

Signature

Kevin Westhead

Tony Farrell - 14 Sep 2003 01:15 GMT
> > I have two questions about MarshalAs
> >
[quoted text clipped - 33 lines]
> 2) Again the unmanaged definition for the callee would be required before
> any useful advice could be offered.

Hi Kevin and everyone -

Here is the answer to your first question -
   [DllImport("CarChipSDK", EntryPoint="GetTripRecord")]
   public static extern bool GetTripRecord
     (
       int index,
       TripRecord_struct trip_record
     );

Here is the full struct that i am trying to work with....

   [StructLayout(LayoutKind.Sequential)]
   public struct TripRecord_struct
   {
     [MarshalAs( UnmanagedType.Struct)]
     public DateTimeStruct startTime;
     [MarshalAs( UnmanagedType.Struct)]
     DateTimeStruct endTime;
     [MarshalAs( UnmanagedType.Struct)]
     DateTimeStruct tripDuration;    
     double maxSpeed;
     double avgSpeed;
     double distance;
     int hardBrakeCount;
     int extremeBrakeCount;
     int hardAccelerationCount;
     int extremeAccelerationCount;
     [MarshalAs( UnmanagedType.ByValArray,  SizeConst=5)]
     int parameter;  /*!< int parameter[5] */
     [MarshalAs( UnmanagedType.ByValArray, SizeConst=5)]
     int samplingInterval;
     [MarshalAs( UnmanagedType.ByValArray,  SizeConst=5)]
     bool parameterSupported; /*!< bool parameterSupported[5] */
     [MarshalAs( UnmanagedType.ByValArray,  SizeConst=3)]
     double speedThreshold; /*!< double speedThreshold[3] */
     double hardBrakeThreshold;  
     double extremeBrakeThreshold;
     double hardAccelerationThreshold;
     double extremeAccelerationThreshold;
     bool disconnectedDuringTrip;  
   };

Thanks for taking the time to help!!

take care
tony
Kevin Westhead - 14 Sep 2003 18:46 GMT
[snip]

>     [DllImport("CarChipSDK", EntryPoint="GetTripRecord")]
>     public static extern bool GetTripRecord
[quoted text clipped - 30 lines]
>       bool disconnectedDuringTrip;
>     };

Mattias' response should provide what you need. I've modified the struct
above based on his answers.

Signature

Kevin Westhead

Mattias Sj?gren - 14 Sep 2003 17:02 GMT
Tony,

>What would the correct MarshalAs statement be?
>
>This is what i'm trying...
>      [MarshalAs( UnmanagedType.Struct)]
>      DateTimeStruct startTime;

If you have a DateTimeStruct member inside another struct, you don't
need any MarshalAs attribute on it.

UnmanagedType.Struct has nothing to do with C# structs, it should have
been called UnmanagedType.Variant instead.

>2) Given this struct element -
>int samplingInterval[5]
[quoted text clipped - 4 lines]
>      [MarshalAs( UnmanagedType.ByValArray, SizeConst=5)]
>      int samplingInterval;

[MarshalAs( UnmanagedType.ByValArray, SizeConst=5)]
int[] samplingInterval;

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.