Calling COM from a hookproc

0

I have an app Winform which hooks the keyboard with a global hook to catch the Ctrl+Win+left or Ctrl+Win+right keystrokes

private void SetHook()
{
    // Set system-wide hook.
    _hookHandle = SetWindowsHookEx(
        Constants.WHKEYBOARDLL,
        KbHookProc,
        (IntPtr)0,
        0);
}

then from KBHookProc I call another procedure in which I call the property Desktop.Count defined as follow

public class Desktop
{
    public static int Count
    {
        // Returns the number of desktops
        get { return DesktopManager.Manager.GetCount(); }
    }
}

internal static class DesktopManager
{
    internal static IVirtualDesktopManagerInternal Manager = null;
    internal static IVirtualDesktopManager WManager = null;

    static DesktopManager()
    {
        var shell = (IServiceProvider10)Activator.CreateInstance(Type.GetTypeFromCLSID(Guids.CLSID_ImmersiveShell));
        _ = (IVirtualDesktopManagerInternal)shell.QueryService(Guids.CLSID_VirtualDesktopManagerInternal, Guids.IID_IVirtualDesktopManagerInternal);
        _ = (IVirtualDesktopManager)Activator.CreateInstance(Type.GetTypeFromCLSID(Guids.CLSID_VirtualDesktopManager));
    }
}

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("F31574D6-B682-4CDC-BD56-1827860ABEC6")]    //[Guid("AF8DA486-95BB-4460-B3B7-6E7A6B2962B5")] obsolete GUID
internal interface IVirtualDesktopManagerInternal
{
    int GetCount();
}

in this scenario, when the execution of the code reach this Desktop.Count evaluation inside the KBHookpoc, I get the error message "error: 8001010d (Exception from HRESULT: 0x8001010D (RPC_E_CANTCALLOUT_ININPUTSYNCCALL))". this error does not happen if I call the same KBHookProc outside the hook. I have been told to use postmessage but I have no clue how to implement it and cannot find any doc or example.

c#
hook
hotkeys
postmessage
asked on Stack Overflow Jul 6, 2020 by Federico Bianchi • edited Jul 6, 2020 by Jason Aller

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0