none
C# 3.0 - What is IntPtr

    Question

  • Hi,

    I am a begginer trying to understand what is IntPtr ?

     

    Thanks in advance for any help,

    Aldo.

    Monday, April 21, 2008 7:54 AM

Answers

  • It's a class that wraps a pointer that is used when calling Windows API functions. The underlying pointer may be 32 bit or 64 bit, depending on the platform.
    Monday, April 21, 2008 9:09 AM

  • HI Aldo,

    IntPtr is mostly used for interoperation with unmanage code.
    You can refer to MSDN http://msdn2.microsoft.com/en-us/library/system.intptr(VS.71).aspx .

    I here show example of using IntPtr. You can also download the project file at http://cid-fd3332cdd86da8fa.skydrive.live.com/self.aspx/Public/TestinIntPtr.rar

    Code Snippet

    namespace ConsoleApplication4
    {
        class Program
        {
            [DllImport("user32.dll")]
            static extern bool SetWindowText(IntPtr hWnd, string lpString);

            [STAThread()]
            static void Main()
            {
                Window win = new Window();
                Button btn = new Button { Content = "Click Me", Width=50,Height=50 };
                btn.Click += delegate(object sender, RoutedEventArgs args)
                {
                    IntPtr winPtr = new WindowInteropHelper(win).Handle;
                    SetWindowText(winPtr, "Testing SetWindowText Function");             
                };

                win.Content = btn;           
                win.ShowDialog();
                           
            }

          
        }


    It's a WPF sample. Get Handle from WPF window and change the title of it.

    In above example, i use SetWindowText win32 API.
    BOOL SetWindowText(      
        HWND hWnd,
        LPCTSTR lpString
    );
    You can see the parameter hWnd is type of HWND. HWND is actually void pointer.
    IntPtr is mostly used for unmanage pointer.

    Monday, April 21, 2008 10:25 AM

All replies

  • It's a class that wraps a pointer that is used when calling Windows API functions. The underlying pointer may be 32 bit or 64 bit, depending on the platform.
    Monday, April 21, 2008 9:09 AM
  • Maybe you could read this link.

     

    Hope this Helps!!

    Monday, April 21, 2008 9:59 AM

  • HI Aldo,

    IntPtr is mostly used for interoperation with unmanage code.
    You can refer to MSDN http://msdn2.microsoft.com/en-us/library/system.intptr(VS.71).aspx .

    I here show example of using IntPtr. You can also download the project file at http://cid-fd3332cdd86da8fa.skydrive.live.com/self.aspx/Public/TestinIntPtr.rar

    Code Snippet

    namespace ConsoleApplication4
    {
        class Program
        {
            [DllImport("user32.dll")]
            static extern bool SetWindowText(IntPtr hWnd, string lpString);

            [STAThread()]
            static void Main()
            {
                Window win = new Window();
                Button btn = new Button { Content = "Click Me", Width=50,Height=50 };
                btn.Click += delegate(object sender, RoutedEventArgs args)
                {
                    IntPtr winPtr = new WindowInteropHelper(win).Handle;
                    SetWindowText(winPtr, "Testing SetWindowText Function");             
                };

                win.Content = btn;           
                win.ShowDialog();
                           
            }

          
        }


    It's a WPF sample. Get Handle from WPF window and change the title of it.

    In above example, i use SetWindowText win32 API.
    BOOL SetWindowText(      
        HWND hWnd,
        LPCTSTR lpString
    );
    You can see the parameter hWnd is type of HWND. HWND is actually void pointer.
    IntPtr is mostly used for unmanage pointer.

    Monday, April 21, 2008 10:25 AM
  • thanks
    Friday, September 16, 2011 7:15 AM