Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / January 2005

Tip: Looking for answers? Try searching our database.

Bug in NativeWindow.ReleaseHandle?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Claes Bergefall - 20 Dec 2004 10:08 GMT
I'm trying to temporarily subclass a window and tried using
the NativeWindow class, but...

I found some weird behaviour with the ReleaseHandle method
in the NativeWindow class. It seems as if that method messes
up the internal state of the application.

To reproduce the problem I created a simple Form with a
button on it, and placed the following code in the button click
event handler:

Dim h As IntPtr
Dim ctrl As Control
Dim wnd As New NativeWindow
wnd.AssignHandle(Me.Handle)
h = Me.Handle
ctrl = Control.FromHandle(h)
wnd.ReleaseHandle()
h = Me.Handle
ctrl = Control.FromHandle(h)

The first call to Control.FromHandle works as expected
and returns the form instance (Me). The second one doesn't; it
returns null. Both calls to Me.Handle works correctly though.

If you call the above code twice (i.e. press the button twice)
the message handling for the form breaks down! It doesn't paint
correctly, you can't close it fully etc

Found a thread about this that states that this problem exists in 1.0
but has been fixed in 1.1. Well, I use 1.1 and it doesn't look like
it has been fixed. Here is the thread:
http://groups.google.se/groups?hl=sv&lr=&threadm=bdsiao%24oc%2403%241%40news.t-o
nline.com&rnum=4&prev=/groups%3Fas_q%3DNativeWindow%2520ReleaseHandle%26as_ugrou
p%3D*dotnet*%26lr%3D%26hl%3Dsv


Ideas or comments anyone?

  /claes
"Jeffrey Tan[MSFT]" - 21 Dec 2004 08:41 GMT
Hi Claes,

I have reproduced out this behavior, I will spend some more time on this
issue. I will reply to you ASAP. Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

"Jeffrey Tan[MSFT]" - 23 Dec 2004 06:18 GMT
Hi Claes,

Sorry for letting you wait for so long!

If we use the Lutz Roeder's .Net Reflector to view Control.FromHandle()
method, we can see that it calls Control.FromHandleInternal(handle). Then
Control FromHandleInternal(IntPtr handle)'s code lists below:

internal static Control FromHandleInternal(IntPtr handle)
{
     NativeWindow window1 = NativeWindow.FromHandle(handle);
     while ((window1 != null) && !(window1 is Control.ControlNativeWindow))
     {
           window1 = window1.PreviousWindow;
     }
     if (window1 is Control.ControlNativeWindow)
     {
           return ((Control.ControlNativeWindow) window1).GetControl();
     }
     return null;
}

As we can see that, Control.FromHandle() actually returns
NativeWindow.FromHandle(handle) to get the reference. So if we called
ReleaseHandle method, the handle is removed from the internal window hooked
table, then the NativeWindow.FromHandle can not get any NativeWindow object
from the internal window hooked table, so the null is return.

So I think this is by design.

Hope this informatin makes sense to you.
====================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Claes Bergefall - 03 Jan 2005 09:28 GMT
Yes, I looked at it using .NET Reflector aswell.

This looks like a bug to me. ReleaseHandle should restore
the window procedure to what it was before calling
AssignHandle. How is it supposed to be used if it doesn't?

If this is by design you should really consider redesigning it.
Perhaps a RestoreHandle method could be provided.

Do you know of any other way of temporarily subclassing
a window (apart from writing the PInvoke stuff myself)?

Thanks

   /claes

> Hi Claes,
>
[quoted text clipped - 37 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
"Jeffrey Tan[MSFT]" - 04 Jan 2005 05:50 GMT
Hi Claes,

Thanks for your feedback!

I am glad my reply makes sense to you.

Actually, for this issue, I have helped you to forward it to the product
group. If you like, you may also feedback this issue through the link below:
http://register.microsoft.com/mswish/suggestion.asp

So far as I know, NativeWindow is the standard .Net way of doing subclass.
Without P/invoke, I can not think of another way to workaround
NativeWindow class, as .Net did not expose another way.

HTH.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Claes Bergefall - 04 Jan 2005 13:08 GMT
Thanks for the help

I've worked around the issue for now. If I need it
again in the future I'll just write my own subclassing code.

  /claes

> Hi Claes,
>
[quoted text clipped - 17 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
"Jeffrey Tan[MSFT]" - 05 Jan 2005 06:07 GMT
Ok, if you need further help, please feel free to tell me. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.