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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:33:04+00:00 2026-05-30T21:33:04+00:00

After posting a question yesterday I thought I had this cleared up but I’m

  • 0

After posting a question yesterday I thought I had this cleared up but I’m still having problems, I have a C++/CLI wrapper for a C++ class, some functions of the C++ class take buffers for recv as parameters, the packet structures are defined as C++ structs and that is what is taken as a parameter.

In C# I have replicated these C++ structs using structlayout so that I have equivalent structs in C# which are laid out the same in memory as my C++ structs. In my C++/CLI code I attempted the following

UINT GetValues(value class^ JPVals) // value class, as C# structs are value types
{
IntPtr ptr;
Marshal::StructureToPtr(JPVals,ptr,false);
return m_pComms->GetValues(ptr,0); // m_pComms is a wrapped unmanaged class 
    //GetValues takes a pointer to a C++ struct
}

The error I get is cannot convert parameter 1 from ‘System::IntPtr’ to ‘SJPVal *’, why is it not possible to Marshall from value class to C++ struct pointer? And in this case what should I be passing in and how should I be marshalling 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-30T21:33:05+00:00Added an answer on May 30, 2026 at 9:33 pm

    You didn’t get the serialization process:

    // !! Note the % !!
    UINT GetValues(value class% JPVals) // value class, as C# structs are value types 
    { 
        // Allocate a buffer for serialization, pointer to NULL otherwise
        IntPtr ptr = Marshal::AllocHGlobal(Marshal::SizeOf(JPVals));
    
        try {
            // Serialize the managed object to "static" memory (not managed by the GC)
            Marshal::StructureToPtr(JPVals, ptr, false); 
    
            // Pass it to unmanaged code that will modify it.
            auto ret = m_pComms->GetValues(reinterpret_cast<SJPVal*>(ptr.ToPointer()), 0);
    
            // Copies the modifications back
            Marshal::PtrToStructure(ptr, JPVals);
    
            // Free resources
            Marshal::FreeHGlobal(ptr);
    
            return ret;
        } catch (...) {
            // Make sure we free the memory
            Marshal.FreeHGlobal(ptr);
            throw; 
        }
    } 
    

    EDIT: shown how to copy back the value.

    As you are using a C# struct you need to pass it by reference to make sure the changes are copied back. Alternatively, the code will work the same with a C# class. The first step (StructureToPtr) is probably useless, now, since you probably don’t care about what was in there before your call to GetValues.

    By the way your naming convention is a bit bad. You should NOT start variable names by a capital letter in C++.

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

Sidebar

Related Questions

I am posting this question after reading other similar questions about my problem but
EDIT: I solved it seconds after posting the question (sorry!) but can't accept an
I have seen the following links before posting this question http://www.devx.com/wireless/Article/40792/1954 Saving Android Activity
After posting this question and reading that one I realized that it is very
Drawing a blank with finalizing my deploy scheme here. After posting this question: Migrating
I noticed prior to posting this question that there have been similar questions posted
After posting this question MySQL update or insert or die query I've change to
After posting a form having two fields named 'id' and 'url' I have the
After I posting this question I tried to reproduce the problem of accidental rvalue
Stackoverflow is definetly the fastest forum so after posting this question in the WCF

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.