Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper IntPtr use in C#

I think I understand the use of IntPtr, though I'm really not sure.

I copied the IDisposable pattern from MSDN just to see what I could get from it, and while I understand it for the most part, I have no idea how to implement an IntPtr properly, or even understand what it is that it's supposed to "point" to, or reference. On top of that, I have no idea how to even assign or cast an integer, string, char, double, etc. to an IntPtr to create a pointer out of it.

Also, does IntPtr require unsafe code use?

Anyway, here's some code just to paint a picture of what I'm talking about:

namespace Utilities
{   
    class Disposer : IDisposable
    {

        private IntPtr handle;

        private Component component = new Component(); 

        private bool disposed = false;

        public Disposer(IntPtr handle)
        {
            this.handle = handle;

        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if(!this.disposed)
            {
                if (disposing)
                {
                    component.Dispose(); 
                }
                CloseHandle(handle);

                handle = IntPtr.Zero;

                disposed = true;

            }
        }

        [System.Runtime.InteropServices.DllImport("Kernal32")]
        private extern static Boolean CloseHandle(IntPtr handle);
    }



    public unsafe class ExecuteMain
    {
        Object nuller = new Object();

        byte boa = 0;

        byte *blargh = boa;

        public static void Main()
        { 

        }
    }
}

Also, could someone tell me what the point of the component here is, exactly? I'm also having trouble wrapping my head around this concept as well.

like image 418
zeboidlund Avatar asked May 23 '11 06:05

zeboidlund


People also ask

When to use IntPtr c#?

An IntPtr is only a value type which size matches the size of a pointer on the target platform. You need to use it mainly when dealing with unmanaged pointers. An IntPtr itself cannot be disposed, because it only represents a location in memory.

How does IntPtr work?

The IntPtr type can be used by languages that support pointers and as a common means of referring to data between languages that do and do not support pointers. IntPtr objects can also be used to hold handles. For example, instances of IntPtr are used extensively in the System.

What IntPtr zero?

IntPtr. Zero is just a constant value that represents a null pointer.


1 Answers

You can use IntPtr objects this way:

        int test = 55;

        // Allocating memory for int
        IntPtr intPointer = Marshal.AllocHGlobal(sizeof(int));

        Marshal.WriteInt32(intPointer,test);

        // sending intPointer to unmanaged code here

        //Test reading of IntPtr object
        int test2 = Marshal.ReadInt32(intPointer); // test2 would be equal 55

        // Free memory
        Marshal.FreeHGlobal(intPointer);

You can explore other Marshal method to understand how to write string, doubles and etc to IntPtr.

So words about your sample code - its not a good idea to dispose external allocated unmanaged object. You should dispose only object which you have allocated in class constructor. This is not strict rule but some kind of good practice.

like image 97
Anton Semenov Avatar answered Oct 19 '22 21:10

Anton Semenov