Solved Error creating window handle

Posted on 2006-04-16
Last Modified: 2013-11-13
Hi All expert,
I have no ideal why and when this error happen every day. I cannot similate the error. Do your have any ideal?

Error creating window handle.System.Windows.FormsSystem.ComponentModel.Win32Exception: Error creating window handle.
   at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
   at System.Windows.Forms.Control.CreateHandle()
   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.CreateGraphicsInternal()
   at System.Windows.Forms.ThreadExceptionDialog..ctor(Exception t)
   at System.Windows.Forms.ThreadExceptionDialog..ctor(Exception t)
   at System.Windows.Forms.ThreadContext.OnThreadException(Exception t)
   at System.Windows.Forms.Control.WndProcException(Exception e)
   at System.Windows.Forms.ControlNativeWindow.OnThreadException(Exception e)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
   at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Control.set_Visible(Boolean value)
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   at System.Windows.Forms.Form.ShowDialog()
Question by:delphikit
    LVL 3

    Expert Comment

    It's probably because a lack of memory... Try to cleanup as much as you can of what you allocate...

    Take a look at this:

    LVL 10

    Accepted Solution


    The problem is that your application is creating too many objects (windows NT/XP has a limit of 10,000 windwos handles per process). Try to redesing your application in order to create only the required objects for a certain step. Also, try to avoid to use too many global objects.

    Author Comment

    Dear NeoEGM,

    It could be this reason. Anywhere I will try it 1st. But i notice that the memory Usage always go between 40k to 60k. Is there any where to cut it down?
    LVL 3

    Expert Comment

    A way to know more about your process is by going to Windows Task Manager->"Processes" tab->"View" menu->Select columns->Check:

    - USER objects
    - Handle Count
    - Thread Count
    - GDI Objects

    You should try to keep them as low as possible...

    Don't worry so much about the memory usage of the .NET Framework... VB.NET uses to allocate lots of memory and it isn't always a problem... you should reduce the amount of handles/objects in use; it's the best place to start from...


    Author Comment

    Dear NeoEGM,

    Thank you again for your reply. I had tested few transaction in my application and this is the result i get from the Windows Task Manager.
     USER objects = 307
     Handle Count =485
     Thread Count =11
     GDI Objects =679

    It is normal?.. What is the limit for thoese usage?


    LVL 3

    Assisted Solution

    Dear dephikit,

    It don't know what kind of application you're developing, but I dare to say that the GDI Objects, the USER Objects and the Handle Count are too high...

    If I were you I'd take a look at all my code to see if I close all the handles I open/get, if I free all the GDI objects I get allocated and if I do the needed cleanup after any operation I finish...

    Probably, when those values get higher, the app will throw the error you described...

    Keep monitoring the values as you do specific operations on your app and try to cleanup your code as much as you can...

    LVL 10

    Assisted Solution

    FYI. I was getting this error repeatedly and intermittently as well. I was not creating any wnew window handles, the only window was the single dialog.  but for my application I would, for each file in a queue...

    a. position the cursor offscreen
    b. lock the cursor using a windows API
    c. call an external application that I also wrote

    It was this context swtching that caused the problem, occasionally a vibration would cause the mouse to try to move while it was closing the external application and switching back to my dialog application - outside of the context of my application the mouse was no longer locked, if it was moving when it switched back i somethimes got this error. By disabling the mouse lock the problem completely disappeared.

    So it was while my window was taking the focus that this error occurred for me and you should be aware that it can be dificult to determine the root cause. I have put it down as a dotnet foible.

    LVL 25

    Assisted Solution

    You aren't by any chance placing a private class in the form1.vb file above the definition of the form are you?  This has bitten me before, but I don't recall the error message.

    Author Comment

    Hi Jobrienct,

    How to position the cursor offscreen and how to lock the cursor using windows API?
    Can you send me the code how to do that?

    LVL 10

    Expert Comment

    sure, sounds a bit OT for your Q but ...

    Dim iWidth As Integer = 720
    Dim iHeght As Integer = 480
    Cursor.Position = PointToScreen(New Point(iWidth + 10, iHeight + 10))

    will position your cursor off and to the bottom right of your screen. I use an API to set the screen resolution when the prgram starts.

    more later
    LVL 10

    Expert Comment

    In declarations add:

    Friend Mouse As New MouseTrap

    Then use the following class to disable...


    or enable...


    your mouse - which either allows or disallows mouse movements while your application has the focus.

    Public Class MouseTrap

    #Region "Declarations - Constants"

        Private Const HC_ACTION As Integer = 0
        Private Const WH_MOUSE_LL As Integer = 14
        Private Const WM_MOUSEMOVE As Integer = &H200
        Private Const WM_LBUTTONDOWN As Integer = &H201
        Private Const WM_LBUTTONUP As Integer = &H202
        Private Const WM_LBUTTONDBLCLK As Integer = &H203
        Private Const WM_RBUTTONDOWN As Integer = &H204
        Private Const WM_RBUTTONUP As Integer = &H205
        Private Const WM_RBUTTONDBLCLK As Integer = &H206
        Private Const WM_MBUTTONDOWN As Integer = &H207
        Private Const WM_MBUTTONUP As Integer = &H208
        Private Const WM_MBUTTONDBLCLK As Integer = &H209
        Private Const WM_MOUSEWHEEL As Integer = &H20A

    #End Region

    #Region "Declarations - Structures"

        Private Structure POINT

            Private x As Integer
            Private y As Integer

        End Structure

        Private Structure MSLLHOOKSTRUCT

            Private pt As POINT
            Private mouseData As Integer
            Private flags As Integer
            Private time As Integer
            Private dwExtraInfo As Integer

        End Structure

    #End Region

    #Region "Declarations - Win32 API's"

        Private Declare Function SetWindowsHookEx Lib "user32" _
            Alias "SetWindowsHookExA" ( _
            ByVal idHook As Integer, _
            ByVal lpfn As LowLevelMouseProcDelegate, _
            ByVal hmod As Integer, _
            ByVal dwThreadId As Integer) As Integer

        Private Declare Function CallNextHookEx Lib "user32" ( _
            ByVal hHook As Integer, _
            ByVal nCode As Integer, _
            ByVal wParam As Integer, _
            ByVal lParam As MSLLHOOKSTRUCT) As Integer

        Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
            ByVal hHook As Integer) As Integer

    #End Region

        Private hhkLowLevelMouse As Integer

    #Region "Declarations - Functions"

        Private Function LowLevelMouseProc( _
            ByVal nCode As Integer, _
            ByVal wParam As Integer, _
            ByVal lParam As MSLLHOOKSTRUCT) As Integer

            If (nCode = HC_ACTION) Then

                If wParam = WM_MOUSEMOVE Or _
                    wParam = WM_LBUTTONDOWN Or _
                    wParam = WM_LBUTTONUP Or _
                    wParam = WM_LBUTTONDBLCLK Or _
                    wParam = WM_RBUTTONDOWN Or _
                    wParam = WM_RBUTTONUP Or _
                    wParam = WM_RBUTTONDBLCLK Or _
                    wParam = WM_MBUTTONDOWN Or _
                    wParam = WM_MBUTTONUP Or _
                    wParam = WM_MBUTTONDBLCLK Or _
                    wParam = WM_MOUSEWHEEL Then

                    Return 1

                End If

                Return CallNextHookEx(hhkLowLevelMouse, _
                    nCode, wParam, lParam)

            End If

        End Function

        Private Delegate Function LowLevelMouseProcDelegate( _
            ByVal nCode As Integer, _
            ByVal wParam As Integer, _
            ByVal lParam As MSLLHOOKSTRUCT) As Integer

    #End Region

    #Region "Declarations - Mouse Public Class Methods"

        Public Sub Disable()

            'Set MouseTrap
            hhkLowLevelMouse = SetWindowsHookEx(WH_MOUSE_LL, _
                AddressOf LowLevelMouseProc, _
                Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, _

        End Sub

        Public Sub Enable()

            'Release MouseTrap

        End Sub

    #End Region

    End Class ' Class MouseTrap

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    NotAlone Challenge 20 65
    scoresAverage challenge 8 59
    bunnyEars challenge 6 46
    groovy example issue 10 35
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    This is about my first experience with programming Arduino.
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now