Advertisement

04.25.2007 at 03:00AM PDT, ID: 22532670
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.8

structures, arrays marshalling from C#

Asked by alexcohn in Microsoft Visual C#.Net, C# Programming Language

Tags:

I want to get a bite array that represent the following structure (using pseudocode):

struct MSG {
   int32 msg_type;
   int32 size;
   int32 num;
   struct USER {
       int32 id;
       char name[16];
   } [4]
}

The C# way to define this is something like

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class USER
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=16)]
    public string name = string.Empty;
    public int id = 0;
};

[StructLayout(LayoutKind.Sequential)]
public class MSG_USERS
{
   public int msg_type = (int)Enums.MessageTypes.USERS;
   public int size = 3*4 + 4*(16+4);
   public int num;
   public USER[] user = new USER[4] { new USER(), new USER(), new USER(), new USER() };
}

and the way to convert it as follows:

MSG_USERS msg = new MSG_USERS();
msg.num = 1;
msg.USER[0].id = 22;
msg.USER[0].name = "First User";
byte[] msgarr = new byte[msg.size];
Marshal.StructureToPtr(msg, Marshal.UnsafeAddrOfPinnedArrayElement(msgarr, 0), false);

This is not enough. I also need the following:
Marshal.StructureToPtr(msg.USER[0], Marshal.UnsafeAddrOfPinnedArrayElement(msgarr, 12), false);

Please help me make this code cleaner. That is:
 - get rid of the second explicit call to System.Runtime.InteropServices.Marshal.StructureToPtr.
 - have size calculated automatically
 - what if I have flexible amount of usersStart Free Trial
[+][-]04.25.2007 at 07:26AM PDT, ID: 18973779

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.25.2007 at 08:48AM PDT, ID: 18974644

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.25.2007 at 11:05AM PDT, ID: 18975844

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.25.2007 at 11:25AM PDT, ID: 18976017

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.25.2007 at 10:35PM PDT, ID: 18979423

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.26.2007 at 05:56AM PDT, ID: 18980970

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual C#.Net, C# Programming Language
Tags: marshalling
Sign Up Now!
Solution Provided By: alexcohn
Participating Experts: 1
Solution Grade: B
 
 
[+][-]04.30.2007 at 05:54AM PDT, ID: 19000972

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]05.03.2007 at 04:13PM PDT, ID: 19027733

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32