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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:28:48+00:00 2026-06-11T08:28:48+00:00

I have a struct in my C# as follows: [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] public struct UserProfileData

  • 0

I have a struct in my C# as follows:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct UserProfileData
{
    int userProfileRevision;
    [MarshalAs(UnmanagedType.LPStr)] 
    public String firstName;
    [MarshalAs(UnmanagedType.LPStr)] 
    public String lastName;
    [MarshalAs(UnmanagedType.LPStr)] 
    public String memberids;
    [MarshalAs(UnmanagedType.LPStr)] 
    public String emailAddress;
}

I pass a reference to this

typedef struct userProfile
{
    int profileRevision;
    char *firstName;
    char *lastName;
    char *memberids;
    char *emailAddress;
} userProfile_t;

My C .dll has function like this

int getUserProfileData(userProfile_t *pUserProfile);

to get the values for the strings in the struct above. I call this function from C# code and the int value ‘profileRevision’ is properly populated. The strings like ‘firstname’ are properly dynamically allocated and filled within the above C function but when the code returns to the C# environment all the strings in the struct are null. What’s the best way to handle this?

  • 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-11T08:28:50+00:00Added an answer on June 11, 2026 at 8:28 am

    The way you have written it, the char* buffers are allocated on the managed side. But that’s the wrong place. The allocation happens on the unmanaged side. Declare the struct in C# like this:

    [StructLayout(LayoutKind.Sequential)]
    public struct UserProfileData
    {
        int userProfileRevision;
        public IntPtr firstName;
        public IntPtr lastName;
        public IntPtr memberids;
        public IntPtr emailAddress;
    }
    

    Then call getUserProfileData, passing the struct as an out parameter. Or possibly a ref parameter. I can’t tell from here which it should be.

    Your DllImport will look like this (with the correct calling convention specified):

    [DllImport(@"mydll.dll", CallingConvention=CallingConvention.???)]
    private static extern int getUserProfileData(out UserProfileData userProfile);
    

    Then convert the returned pointers to strings like this:

    string firstName = Marshal.PtrToStringAnsi(userProfile.firstName);
    

    and so on for the other fields.

    Presumably the unmanaged code also exposes a function that deallocates the memory returned in the struct. Call that once you are done with the struct.

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

Sidebar

Related Questions

I have the following structure: [StructLayout(LayoutKind.Sequential)] public struct TCurve { public int fNumItems; /*
I have a Struct as follows, struct Location { public int Row; public int
I have a struct defined as follows: template <typename T> struct data { int
I have a class as follows: typedef struct grid_cell_type { int x; int y;
I have a class as follows struct CliHandler { CliHandler(int argc, char** argv); ~CliHandler();
I have structure as follows: struct data {int no; string name; int id}; I
I have a set of structs, defined as follows: typedef struct { int index;
I have a struct as follows: struct Node{ int *arr; int *sol; struct Node
I have a testing struct definition as follows: struct test{ int a, b, c;
I have a struct called node as follows: struct node { int data; }

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.