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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:42:43+00:00 2026-05-31T19:42:43+00:00

In C#, I defined a struct: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct MyObject {

  • 0

In C#, I defined a struct:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct MyObject
{
   [MarshalAs(UnmanagedType.LPWStr)]
   public string var1;    

   [MarshalAs(UnmanagedType.LPWStr)]
   public string  var2;    
};

I have this struct in C++:

public value struct MyObject
{
    LPWSTR var1;    
    LPWSTR var2;    
};

And in the method of C++ which is a public class to be called from C#:

TestingObject(MyObject^ configObject)
{
   // convert configObject from managed to unmanaged.
}

The object is debugged correctly that I can see two strings var1 and var2. However, the problem now is that I need to marshal the object: configObject into an unmanaged object.

What I think of is to do something like this:

TestingObject(MyObject^ configObject)
{
   // convert configObject from managed to unmanaged.
   MyObject unmanagedObj = (MyObject)Marshal::PtrToStructure(configObject, MyObject);  
}

That is what I can think of but off course, I got this error:

Error 2 error C2275: ‘MyObject’ : illegal use of this type as an
expression

Is that right to convert the managed object into unmanaged object? If so, how can I can that Marshal::PtrToStructure correctly? If no, how can I do it?

  • 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-05-31T19:42:45+00:00Added an answer on May 31, 2026 at 7:42 pm

    Marshal::PtrToStructure does the opposite of what you want, it converts an unmanaged pointer to a managed object. You want Marshal::StructureToPtr.

    Also, you would need to define an unmanaged class, because MyObject is a Managed Type. Assuming you have done that, you could do it like this (just converted this from the C# sample):

    IntPtr pnt = Marshal::AllocHGlobal(Marshal::SizeOf(configObject)); 
    Marshal.StructureToPtr(configObject, pnt, false);
    

    You then have a pointer to the data, which you can memcpy or whatever into your native struct.

    But MyObject is and will stay a managed type. If you want a truly unmanaged type, you have to define one that matches the managed struct.

    Just wondering, why are you using unmanaged LPWSTR in a managed struct?

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

Sidebar

Related Questions

c# struct defined as: [StructLayout(LayoutKind.Sequential)] public struct RecognizeResult { /// float public float similarity;
I have the following structure: [StructLayout(LayoutKind.Sequential)] public struct TCurve { public int fNumItems; /*
I have defined my struct like this: struct Test { private string assayName; public
I have defined a struct ABC to contain an int ID, string NAME, string
I have a struct defined as the following: typedef struct { string foo; }
By default structs in C# are implemented with [StructLayout( LayoutKind.Sequential )] for reasons basically
I have a vector<data> info where data is defined as: struct data{ string word;
I am having a struct defined in C++ which contains int and std::string arrays
I've got a struct defined like this private struct Combinators { public const char
I have a struct defined like follows as part of an object. I'm trying

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.