2

Closed

VS2010, Windows 7 PRO - Error

description

Everything looks ok in design mode, but whenever I've tried to run project I get this exception :
 
File: GlobalHook.cs, Line: 446
Win32Exception was unhandled
Invalid window handle.

file attachments

Closed May 16, 2013 at 12:22 PM by adriancs
Please visit the new development site.
http://officeribbon.codeplex.com/

comments

isotoxin wrote Aug 2, 2010 at 8:20 AM

OK, I get rid of that crap. But now I've got rendering problems. See attached screenshot.

bpell wrote Nov 22, 2010 at 7:01 PM

I don't know if this helps or not, but I commented out that line that throws the exception and the Ribbon appears to work fine, or at least I haven't run into any scenarios yet that are problematic.

wrote Mar 6, 2011 at 8:46 AM

saamorim wrote Dec 27, 2011 at 9:45 PM

Hi. Actually there is a solution for this posted on the codeproject page (http://www.codeproject.com/Messages/4020406/Re-Here-is-a-Solution-that-works-for-2-0-and-4-0-n.aspx). Credits foes to han6man.

On the GlobalHook.cs file
add:

using System.Diagnostics;

replace InstallHook with

/// <summary>
    /// Installs the actual unsafe hook
    /// </summary>
    private void InstallHook()
    {
        /// Error check
        if (Handle != 0) throw new Exception("Hook is already installed");

        #region htype
        int htype = 0;

        switch (HookType)
        {
            case HookTypes.Mouse:
                htype = WinApi.WH_MOUSE_LL;
                break;
            case HookTypes.Keyboard:
                htype = WinApi.WH_KEYBOARD_LL;
                break;
            default:
                throw new Exception("HookType is not supported");
        }
        #endregion

        /// Delegate to recieve message
        _HookProc = new HookProcCallBack(HookProc);

        /// Hook
        _hHook = WinApi.SetWindowsHookEx(htype, _HookProc, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
        _hHook = WinApi.SetWindowsHookEx(htype, _HookProc, GetCurrentModule().BaseAddress, 0);

        /// Error check
        if (Handle == 0) throw new Win32Exception(Marshal.GetLastWin32Error());
    }
add the method:

public static ProcessModule GetCurrentModule()
    {
        // need instance handle to module to create a system-wide hook
        Module[] list = System.Reflection.Assembly.GetExecutingAssembly().GetModules();
        System.Diagnostics.Debug.Assert(list != null && list.Length > 0);

        var currentProcess = Process.GetCurrentProcess();
        var modules = currentProcess.Modules;
        ProcessModule mod = null;
        foreach (ProcessModule m in modules)
            /*
            //for .net 2 we will find module here
            if (m.ModuleName == list[0].Name)
            {
                mod = m;
                break;
            }
            */
        //for .net 4 take current module
        if (mod == null)
            mod = Process.GetCurrentProcess().MainModule;

        return mod;
    }

adriancs wrote Apr 29, 2012 at 1:21 AM

dont inherit RibbonForm

adriancs wrote Apr 29, 2012 at 1:21 AM

Dont inherit RibbonForm

wrote Feb 21, 2013 at 11:18 PM

adriancs wrote May 16, 2013 at 12:19 PM

Hi, new version of Ribbon had released. Please visit the new development site of this project for latest fixes, info and downloads.
New site: https://officeribbon.codeplex.com/

wrote May 16, 2013 at 12:22 PM