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

  • Home
  • SEARCH
  • 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 94665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:32:14+00:00 2026-05-10T23:32:14+00:00

The current declaration of SendMessage over at PInvoke.net is: [DllImport(user32.dll, CharSet = CharSet.Auto, SetLastError

  • 0

The current declaration of SendMessage over at PInvoke.net is:

[DllImport('user32.dll', CharSet = CharSet.Auto, SetLastError = false)] static extern IntPtr SendMessage(HandleRef hWnd, uint Msg,        IntPtr wParam, IntPtr lParam); 

Note: The hWnd is no longer an IntPtr, and has been replaced with HandleRef. A very loose explanation for the change is given:

You can replace ‘hWnd’ with ‘IntPtr’ instead of ‘HandleRef’. However, you are taking a risk in doing this – it may cause your code to crash with race conditions. The .NET runtime can, and will, dispose your window handles out from under your message – causing all sorts of nasty problems!

Someone wiki’d a followup question:

Question: Can’t this last issue be addressed with marshaling, specifically pinning?

And someone answered:

Answer: You can use GC.KeepAlive() right after the SendMessage() with the Form object as the parameter to KeepAlive().

All this ‘disposing your form underneath you’ seems odd to me. SendMessage is a synchronous call. It will not return until until the sent message has been processed.

The implication then is that a form handle can be destroyed at any time. For example:

private void DoStuff() {    //get the handle    IntPtr myHwnd = this.Handle;      //Is the handle still valid to use?    DoSomethingWithTheHandle(myHwnd); //handle might not be valid???      //fall off the function } 

This means that the window handle can become invalid between the time i use it and the time the method ends?


Update One

i understand the notion that once a form goes out of scope, it’s handle is invalid. e.g.:

private IntPtr theHandle = IntPtr.Zero;  private void DoStuff() {    MyForm frm = new MyForm())     theHandle = frm.Handle;     //Note i didn't dispose of the form.    //But since it will be unreferenced once this method ends    //it will get garbage collected,    //making the handle invalid } 

It’s obvious to me that the form’s handle is not valid once DoStuff has returned. The same would be true no matter what the technique – if the form is not held in some scope, it is not valid to use.

i would disagree with (todo link guy) in that a form will stick around until all send messages have been received. The CLR does not know who may have been given my form’s window handle, and has no way of knowing who may call SendMessage() in the future.

In other words, i cannot imagine that calling:

IntPtr hWnd = this.Handle; 

will now prevent this from being garbage collected.


Update Two

i cannot imagine having a window handle around will keep a form from being garbage collected. i.e.:

Clipboard.AsText = this.Handle.ToString(); IntPtr theHandle = (IntPtr)(int)Clipboard.AsText; 

Answer

But these are illevant points – the original question still is:

Can the runtime dispose a form’s handle out from under me?

The answer, it turns out, is no. The runtime will not dispose of a form out from under me. It will dispose of an unreferenced form – but unreferenced forms are not under me. ‘Under Me’ means i have a reference to the form.

On the other hand, a Form object’s underlying Windows window handle can be destroy out from under me (and really how could it not – window handles are not reference counted – nor should they be):

IntPtr hwnd = this.Handle; this.RightToLeft = RightToLeft.Yes; //hwnd is now invalid 

It’s also important to note that HandleRef will not help prevent the problems caused by creating object wrappers around Windows window handles:

Reason 1 If a form object is being destroyed because you didn’t have a reference to it – then you’re just stupid for trying to talk to a form that by rights should no longer exist. Just because the GC hasn’t gotten around to it yet doesn’t make you smart – it makes you lucky. A HandleRef is a hack to keep a reference to the form. Rather than using:

HandleRef hr = new HandleRef(this, this.Handle); DoSomethingWithHandle(this.Handle); 

you could easily use:

Object o = this; DoSomethingWithHandle(this.Handle); 

Reason 2 HandleRef will not prevent a form from re-creating it’s underlying window handle, e.g.:

HandleRef hr = new HandleRef(this, this.Handle); this.RightToLeft = RightToLeft.Yes; //hr.Hande is now invalid 

So while the original modifier of SendMessage on P/Invoke did point out a problem, his solution isn’t a solution.

  • 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. 2026-05-10T23:32:14+00:00Added an answer on May 10, 2026 at 11:32 pm

    Often when you are calling SendMessage, you are doing so from another thread or at least another component separate from your Form. I assume the point being made is that just because you have an IntPtr which at one point contained a valid window handle, you cannot assume that it still is a valid.

    Say you had this class:

    class MyClass {    IntPtr hwnd;    public MyClass(IntPtr hwnd) {       this.hwnd = hwnd;    }    ...     private void DoStuff()    {        //n.b. we don't necessarily know if the handle is still valid        DoSomethingWithTheHandle(hwnd);    } } 

    and somewhere else:

     private void DoOtherStuff() {      Form f = new Form();      mc = new MyClass(f.Handle);  } 

    then because f has gone out of scope, its Dispose will eventually get called by the GC finalizer. That’s why you might need to use Gc.KeepAlive in this type of situation. f must stay alive until mc is finished with the handle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 74k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I would port them to ... um ... COBOL. Yes,… May 11, 2026 at 2:18 pm
  • added an answer You could use a virtual machine to host your Windows… May 11, 2026 at 2:18 pm
  • added an answer When you shelve the changes, there is a checkbox towards… May 11, 2026 at 2:18 pm

Related Questions

I'm looking for a .NET Add-in that reads in the contents of the Current
The following for creating a Global Object is resulting in compilation errors. #include stdafx.h
I am hosting SpiderMonkey in a current project and would like to have template
Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.