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 8250617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:57:13+00:00 2026-06-07T23:57:13+00:00

I have the folowing unmanaged structure. struct MyNativeStruct_Foo { char product_name[4]; char product_model[2]; }

  • 0

I have the folowing unmanaged structure.

struct MyNativeStruct_Foo
{   
 char product_name[4];
 char product_model[2];
}

And the managed equivalent

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct MyManagedStruct_Foo
{
    [MarshalAs(UnmanagedType.LPStr, SizeConst = 4)]
    public string product_name;

    [MarshalAs(UnmanagedType.LPStr, SizeConst = 2)]
    public string product_model;
}

Unfortunately the stings are not null terminated. All 4 characters for the product name are used. If I define the managed structure with “LPStr” the string have to be 0 terminated.

Is there another way to define the managed structure? It is possible to define a custom Marshaller attribute? Or do you have other Ideas?

Note;
The native structure can not be changed.

Thanks

Amberg

[EDIT]
changed to LPStr (comment Jason Larke)

  • 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-07T23:57:15+00:00Added an answer on June 7, 2026 at 11:57 pm

    You can change your marshalling to treat the string as a fixed-size array of bytess and then using the Encoding class to convert the bytes into a managed string.

    i.e

    [StructLayout(LayoutKind.Sequential)]
    public struct MyManagedStruct_Foo
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
        public byte[] product_name;
    
        [MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
        public byte[] product_model;
    
        public string GetProductName()
        {
            return System.Text.Encoding.ASCII.GetString(this.product_name);
        }
    
        public string GetProductModel()
        {
            return System.Text.Encoding.ASCII.GetString(this.product_model);
        }
    }
    

    Not exactly the sexiest struct in the world, but it should be fine for your needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following .NET value types: [StructLayout(LayoutKind.Sequential)] public struct Date { public UInt16
I have a unmanaged C++ function with the following signature: int function(char* param, int
I have an unmanaged library which I want to use from a managed class.
I have a fairly old unmanaged C++ program that called managed c# code (vs2003)
I have a mixed assambly written in C++ managed and unmanaged. To sign this
I have two communicating components - one managed, the other unmanaged. The managed needs
I have folowing code in which i am using some conditions on page to
I have folowing MYsql Query And Trying to right outer join but unable to
I have the folowing array: outerarray{ id: 20154, from: { name: xyz, id: 10004
i'm having a problem with my database. I have the folowing: create table book

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.