Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8245269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:07:39+00:00 2026-06-07T22:07:39+00:00

I am writing a C# application that communicates with an old C DLL. I

  • 0

I am writing a C# application that communicates with an old C DLL. I looked at the header of the C DLL and saw 2 structures that I need to create in C#:

typedef struct
{
    char    user_field_name[MAXLENGTH];
    WORD    user_field_length;
    char    user_field_contents[80];
    char    user_field_requested[1];
    char    user_field_clear[1];
    char    user_field_attribute[1];
} U_FIELD_STRUCT;
typedef U_FIELD_STRUCT FAR * P_U_FIELD_STRUCT;

typedef struct
{
    char                user_begin_literal[7];
    char                user_screen_name[MNFRMSCRNAMELENGTH];
    char                key_to_be_sent[256]; 
    WORD                num_of_user_fields;
    U_FIELD_STRUCT      user_field[MAXFIELDCOUNT];
    char                user_end_literal[7];
} USER_FIELD_STRUCT;

In my C# class, I wrote them as follows:

    public const Int32 MAXLENGTH = 51;
    public const Int32 MAXTAGLENGTH = 80;
    public const Int32 MNFRMSCRNAMELENGTH = 5;
    public const Int32 MAXFIELDCOUNT = 100;
    public const Int32 MAXTAGCOUNT = 40;
    public const Int32 MAXSCREENCOUNT = 150;

    public const Int32 NO_SCREEN_DATA = 99;

    [StructLayout(LayoutKind.Sequential, Pack=4, CharSet=CharSet.Ansi)]
    public struct U_FIELD_STRUCT
    {
        [ MarshalAs( UnmanagedType.ByValArray, SizeConst=MAXLENGTH)]
        public char [] user_field_name;
        public short user_field_length;
        [ MarshalAs( UnmanagedType.ByValArray, SizeConst=80)]
        public char [] user_field_contents;
        [ MarshalAs( UnmanagedType.ByValArray, SizeConst=1)]
        public char [] user_field_requested;
        [ MarshalAs( UnmanagedType.ByValArray, SizeConst=1)]
        public char [] user_field_clear;
        [ MarshalAs( UnmanagedType.ByValArray, SizeConst=1)]
        public char [] user_field_attribute;
    };

    [StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Ansi)]
    public struct USER_FIELD_STRUCT
    {
        [ MarshalAs( UnmanagedType.ByValArray, SizeConst=7)]
        public char [] user_begin_literal;
        [ MarshalAs( UnmanagedType.ByValArray, SizeConst=MNFRMSCRNAMELENGTH)]
        public char [] user_screen_name;
        [ MarshalAs( UnmanagedType.ByValArray, SizeConst=256)]
        public char [] key_to_be_sent;
        public short num_of_user_fields;
        [ MarshalAs( UnmanagedType.ByValArray, SizeConst=MAXFIELDCOUNT)]
        public U_FIELD_STRUCT [] user_field;
        [ MarshalAs( UnmanagedType.ByValArray, SizeConst=7)]
        public char [] user_end_literal;
    };

When I initialize the struct in C#, I do it as follows:

        USER_FIELD_STRUCT UserFieldsData = new USER_FIELD_STRUCT();
        UserFieldsData.user_begin_literal = new char[7];
        UserFieldsData.user_screen_name = new char[MNFRMSCRNAMELENGTH];
        UserFieldsData.key_to_be_sent = new char[256];
        UserFieldsData.user_field = new U_FIELD_STRUCT[MAXFIELDCOUNT];
        UserFieldsData.user_end_literal = new char[7];
        for (int i = 0; i < MAXFIELDCOUNT; i++)
        {
            UserFieldsData.user_field[i].user_field_name = new char[MAXLENGTH];
            UserFieldsData.user_field[i].user_field_contents = new char[80];
            UserFieldsData.user_field[i].user_field_requested = new char[1];
            UserFieldsData.user_field[i].user_field_clear = new char[1];
            UserFieldsData.user_field[i].user_field_attribute = new char[1];
        }

The problem is that when I pinvoke my method, I am getting an error message (basically the DLL has to validate the strings, and it sends a message that the strings are not what they are supposed to be). So I installed VS6 and I ran the DLL in Debug mode. I took a look at the struct I was sending and the strings did have the data that I put in them (some of the char[] had no data and other had a combination of 2 of the fields.)

For example, the field if I set the fields like this:

user_begin_literal=”T0k0a10”
user_end_literal=”T0k0b10”
user_screen_name=”SC00”

When I look at the object in VS6:

user_begin_literal=” T0k0a10”
user_end_literal=””
user_screen_name=”SC00”

Did I mess up my struct design?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:07:40+00:00Added an answer on June 7, 2026 at 10:07 pm

    In C, a char is only 1 byte, but in C# it is 2 bytes.

    In C# use sbyte to map to a C char (both are signed 8-bit values), and ushort to map to a C WORD (both are unsigned 16-bit values).

    If you want to assign a string value to one of your struct’s buffers, you should convert it to ASCII (or UTF8 if the native code can handle it), and then copy the bytes to the field:

    void CopyStringToField(string value, sbyte[] field)
    {
        int maxLength = field.Length;  // or field.Length - 1 if you need room for null terminator
    
        byte[] fieldValue = Encoding.ASCII.GetBytes(value);
        if (fieldValue.Length > maxLength)
            throw new ArgumentException("string too long for field.");
    
        int length = Math.Min(fieldValue.Length, maxLength);
        Array.Copy(fieldValue, 0, field, 0, length);
    
        // zero fill remaining bytes
        for (int i = length; i < field.Length; i++)
        {
            field[i] = 0;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an application that communicates with Active Directory and I need to
I'm writing an application that will create difficult passwords for the user. The user
I am in the process of writing an application that communicates with several devices
I'm writing an application for the iPhone that communicates with a SQLite database but
I'm writing an application that communicates with the serial port to control a device.
I am writing an application that communicates using sockets. I have a server running
I'm writing an application that communicates by sending bytes to the USB port. I'd
I'm writing an application that communicates through the Windows RPC mechanism with a hosted
I'm writing a simple application that communicates with an external server. The server currently
I am writing an application that communicates with other devices through SMSs. I was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.