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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:35:52+00:00 2026-06-17T09:35:52+00:00

I have a HWND in my C++ MFC code, and I want to pass

  • 0

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.

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

My C++ MFC code:

HWND myHandle= this->GetSafeHwnd();
m_CLIDialog->UpdateHandle(myHandle);

My C# code:

public void UpdateHandle(IntPtr mHandle)
{
   ......
}

My CLI code:

void CLIDialog::UpdateHandle(HWND hWnd)
{
   System::IntPtr^ managedhWnd = gcnew System::IntPtr();
   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->CSharpControl->UpdateHandle(managedhWnd);
    }

Error (cannot convert from IntPtr^ to IntPtr) currently occurs on m_pManagedData->CSharpControl->UpdateHandle(managedhWnd);

if I change the CLI code to:

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->CSharpControl->UpdateHandle(managedhWnd);
    }

So in this case the value gotten in C# is 0.

How can I make it work properly?

  • 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-06-17T09:35:53+00:00Added an answer on June 17, 2026 at 9:35 am

    To convert from HWND (which is just a pointer) to IntPtr you have to invoke its constructor, and you do not need gcnew as it’s a value type.
    So this should work to pass a HWND from native to managed:

    void CLIDialog::UpdateHandle( HWND hWnd )
    {
      IntPtr managedHWND( hwnd );
      m_pManagedData->CSharpControl->UpdateHandle( managedHWND );
    }
    

    And this is a function you can invoke from managed code and get a native HWND from in native code:

    void SomeManagedFunction( IntPtr hWnd )
    {
      HWND nativeHWND = (HWND) hWnd.ToPointer();
      //...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have MessageBoxW(hwnd, LΚαλημέρα, LΤέστ, MB_OK|MB_APPLMODAL); That works, but now how can I pass
I have a System.Windows.Form and a IntPtr acting as HWND. I want each of
This is the code I have: HWND WebformCreate(HWND hParent, UINT id) { return CreateWindowEx(0,
I have the following code: use Imager::Screenshot 'screenshot'; my $img = screenshot(hwnd => 'active',
I have a MFC application, which has a worker thread, what I want to
I have a simple MFC program which displays the progressbar..I used the below code
I have an MFC application that uses an ancient (circa 1999) third-party ActiveX control.
With Windows Presentation Foundation, if I have an HWND, how can I capture it's
I was wondering how can I read text of a control that doesn't have
I have a dll which accepts HWND, (code in the dll); void VideoCapture::SetVideoWindow(HWND VidWind)

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.