title: Convert HWND to IntPtr (CLI)
body: <p>I have a HWND in my C++ MFC code, and I want to pass this HWND to a C# control and get it as IntPtr.</p>
  
  <p>What Is wrong in my code, and how can I do it correctly?
  (I think it's something with wrong using of the CLI pointers, because I get an error that it cannot convert from System::IntPtr^ to System::IntPtr. But I don't know how exactly to make it all to work properly...)</p>
  
  <p>My C++ MFC code:</p>
  
  <pre><code>HWND myHandle= this-&gt;GetSafeHwnd();
  m_CLIDialog-&gt;UpdateHandle(myHandle);
  </code></pre>
  
  <p>My C# code:</p>
  
  <pre><code>public void UpdateHandle(IntPtr mHandle)
  {
     ......
  }
  </code></pre>
  
  <p>My CLI code:</p>
  
  <pre><code>void CLIDialog::UpdateHandle(HWND hWnd)
  {
     System::IntPtr^ managedhWnd = gcnew System::IntPtr();
     HWND phWnd; // object on the native heap
  
     try
     {
  
         phWnd = (HWND)managedhWnd-&gt;ToPointer();
          *phWnd = *hWnd; //Deep-Copy the Native input object to Managed wrapper.
  
         m_pManagedData-&gt;CSharpControl-&gt;UpdateHandle(managedhWnd);
      }
  </code></pre>
  
  <p>Error (cannot convert from IntPtr^ to IntPtr) currently occurs on <code>m_pManagedData-&gt;CSharpControl-&gt;UpdateHandle(managedhWnd);</code></p>
  
  <p>if I change the CLI code to:</p>
  
  <pre><code>void CLIDialog::UpdateHandle(HWND hWnd)
  {
     System::IntPtr managedhWnd;
     HWND phWnd; // object on the native heap
  
     try
     {
  
         phWnd = (HWND)managedhWnd.ToPointer();
          *phWnd = *hWnd; //Deep-Copy the Native input object to Managed wrapper.
  
         m_pManagedData-&gt;CSharpControl-&gt;UpdateHandle(managedhWnd);
      }
  </code></pre>
  
  <p>So in this case the value gotten in C# is 0.</p>
  
  <p>How can I make it work properly?</p>