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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:59:25+00:00 2026-05-24T21:59:25+00:00

I created the type IntPtr<T> to act like a generic pointer that c/c++ have:

  • 0

I created the type IntPtr<T> to act like a generic pointer that c/c++ have:

public struct IntPtr<T> : IDisposable where T : struct
{
    private static Dictionary<IntPtr, GCHandle> handles = new Dictionary<IntPtr, GCHandle>();

    private IntPtr ptr;

    public IntPtr(ref T value)
    {
        GCHandle gc = GCHandle.Alloc(value, GCHandleType.Pinned);

        ptr = gc.AddrOfPinnedObject();
        handles.Add(ptr, gc);
    }

    public IntPtr(ref T[] value)
    {
        GCHandle gc = GCHandle.Alloc(value, GCHandleType.Pinned);

        ptr = gc.AddrOfPinnedObject();
        handles.Add(ptr, gc);
    }

    public IntPtr(IntPtr value)
    { ptr = value; }

    public IntPtr(IntPtr<T> value)
    { ptr = value.ptr; }

    public void Dispose()
    {
        if (handles.ContainsKey(ptr))
        {
            GCHandle gc = handles[ptr];
            gc.Free();
            handles.Remove(ptr);
            ptr = IntPtr.Zero;
        }
    }

    public T? this[int index]
    {
        get
        {
            if (ptr == IntPtr.Zero) return null;
            if (index < 0) throw new IndexOutOfRangeException();

            if (handles.ContainsKey(ptr))
            {
                GCHandle gc = handles[ptr];

                if (gc.Target is Array) return ((T[])gc.Target)[index];

                return (T)gc.Target;
            }

            return null;

        }
        set
        {
            if (index < 0) throw new IndexOutOfRangeException();
            // not yet implemented
        }
    }

    private T[] getArray()
    {
        if (handles.ContainsKey(ptr)) return (T[])handles[ptr].Target;

        return null;
    }

    public int Count
    {
        get
        {
            if(handles.ContainsKey(ptr))
            {
                GCHandle gc = handles[ptr];
                if (gc.Target is Array) return ((T[])gc.Target).Length;

                return 1;
            }

            return 0;
        }
    }

    public static implicit operator IntPtr(IntPtr<T> value) { return value.ptr; }

    public static implicit operator T(IntPtr<T> value) { return (T)value[0]; }

    public static implicit operator T[](IntPtr<T> value) { return value.getArray(); ; }

    public static implicit operator T?(IntPtr<T> value) { return value[0]; }

}

It’s not complete yet but for now it works, the problem is i keep track of GCHandle by storing them in handles now i need to free the GCHandle once it no more needed so i have to declare a destrcutor but c# don’t allow struct to have destrcutor or to override ‘Finalize’ method and if the such variable of type IntPtr<T> goes out of scope the destruction take place but the GCHandle won’t be free.

UPDATE

As an example of this class usage, suppose we going to interpo COAUTHIDENTITY and COAUTHINFO from COM, here what it will look like:

[StructLayout(LayoutKind.Sequential)]
struct COAUTHIDENTITY
{
    [MarshalAs(UnmanagedType.LPWStr)] string User;
    uint UserLength;
    [MarshalAs(UnmanagedType.LPWStr)] string Domain;
    uint DomainLength;
    [MarshalAs(UnmanagedType.LPWStr)] string Password;
    uint PasswordLength;
    uint Flags;
}

[StructLayout(LayoutKind.Sequential)]
struct COAUTHINFO
{
    uint dwAuthnSvc;
    uint dwAuthzSvc;
    [MarshalAs(UnmanagedType.LPWStr)] string pwszServerPrincName;
    uint dwAuthnLevel;
    uint dwImpersonationLevel;
    IntPtr<COAUTHIDENTITY> pAuthIdentityData;
    uint dwCapabilities;
}

Instead to make pAuthIdentityData an IntPtr and use Marshal member functions to get object of type COAUTHIDENTITY, IntPtr<T> will make it more simple.

The question is: where should i write the code to free GCHandle when the IntPtr<T> is released?

  • 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-24T21:59:26+00:00Added an answer on May 24, 2026 at 9:59 pm

    You’re reinventing the wheel. Look at the SafeHandle class. Use an existing descendant or create your own descendant.

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

Sidebar

Related Questions

I've created a Range type: public class Range<T> where T : IComparable<T> { public
HI i have created a content type call mycontent it has 2 fields Label:
i created a FTS by this article i have a column with type of
is it possible to somehow cast the type of a pointer created by the
When I use reflection in this case, the created type can be many generic
public partial class Form1 : Form { [DllImport(coredll.dll)] static extern int SetWindowLong(IntPtr hWnd, int
I have created a Type Library out of a .NET DLL I created. I
I have created this type: create or replace type PRODTABLE as table of PROD_OBJ;
I want to retrieve all objects (not DOM elements) of a given type created
In my app, i've created the TList type list where i store the pointers

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.