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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:45:15+00:00 2026-05-19T04:45:15+00:00

In a C# project, I need to pass object parameters by putting references in

  • 0

In a C# project, I need to pass object parameters by putting references in a structure.
i.e. I have a structure passed to a dispatcher

struct SOMESTRUCT
{
    public int lpObject;
}

Where lpObject holds a pointer to a custom object like

class SomeClass
{
    private string foo;
}

And the SOMESTRUCT structure is passed from method to method to finally reach my code. I cannot modify the execution flow nor the strange SOMSTRUCT system, so I guessed the only solution was to cast my object to a pointer like this :

var myObject = new SomeClass();
GCHandle GC = GCHandle.Alloc(myObject, GCHandleType.Pinned);
int myRef = GC.AddrOfPinnedObject().ToInt32();
GC.Free();

SOMESTRUCT struct;
struct.lpObject = myRef;
someMethod(struct);

However, I can’t figure out how to retrieve the myObject members from the lpObject fields. Something like this:

SomeClass myObject = CastPointerToObject(struct.myRef) as SomeClass;

Is there a way to do it, or is it impossible ? How can I tell the garbage collector to handle the object ? Should I create a new Garbage-collected object and copy the data field by field ?

TYIA,

  • 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-19T04:45:15+00:00Added an answer on May 19, 2026 at 4:45 am

    no No NO NO NO!!!

    This

    struct SOMESTRUCT
    {
        public SomeClass object_ref;
    }
    

    Is the correct way to store a reference in a struct.

    The code you have written, and the accepted answer, are 100% broken.

    The address returned by GC.AddrOfPinnedObject(GCHandle) is only valid while the GCHandle is intact. You must not call GCHandle.Free, and you must not let the GCHandle get collected. In your code, the address is already meaningless by the time you store it.

    But you should just let .NET take care of managing the pointer during garbage collection, by using a variable of reference type. Then you don’t need to jump through hoops. The only reason to take the address of a managed object is when passing it to an existing native DLL function that will save the pointer after it returns. For example, it’s necessary with OpenGL buffer arrays. It is NOT necessary, ever, when calling other C# methods.

    If SOMESTRUCT is actually a native data type used by some DLL function you haven’t mentioned, then you’ll need to make sure to keep the GCHandle alive. Only as long as the GCHandle exists will the pointer you got remain valid.

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

Sidebar

Related Questions

I want to pass a C# object between win forms. At the moment, I
In a project that I'm about to wrap up, I've written and implemented an
The project currently I am working in requires a lot of searhing/filtering pages. For
I have the SQL2008 version of SMO and although it uses this class internally
I wanted to create a simple paginated search for my project. I'm having trouble
There are a lot of such classes in my project (very old and stable
I am in the completion stage of a large project that has several large
I have a re-occurring design problem with certain classes which require one-off initialization with
I have a rather peculiar data source I have to work with (an interface
I'm going to create statistics based on information what builds were success or not

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.