在C#中正确使用IntPtr [英] Proper IntPtr use in C#

查看:192
本文介绍了在C#中正确使用IntPtr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

认为我理解IntPtr的用法,尽管我不确定.

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

我从MSDN复制了IDisposable模式只是为了看看我能从中得到什么,尽管我在很大程度上理解了它,但我不知道如何正确地实现IntPtr,甚至不知道它是什么意思. 指向"或引用.最重要的是,我什至不知道如何为IntPtr分配或强制转换整数,字符串,char,double等以创建指针.

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.

此外,IntPtr是否要求使用不安全的代码?

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.

推荐答案

您可以通过以下方式使用IntPtr对象:

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);

您可以探索其他 Marshal 方法来了解将字符串,双打等写入IntPtr.

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.

这篇关于在C#中正确使用IntPtr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆