On Tue, 14 May 2019 19:44:01 +0200, Mike Wey wrote:

On 14-05-2019 05:10, Alex X wrote:

Any news on this?

https://forum.dlang.org/thread/bznpylcjostbrrwzhmst@forum.dlang.org

It's severely cramping my style ;/

Unfortunately no.

Ok, So I figured you probably don't care enough to invest your time to fix it so I went ahead and looked in to it and was able to get it fixed:

// The following code bypasses GTK windows hooking that causes problems with visual studio debugging(slow keyboard, it is a hack and may not work in general)
debug {
	import core.sys.windows.windows, core.stdc.string;
	alias HHOOK function(int, HOOKPROC, HINSTANCE, DWORD) SetWindowsHookExAProc;
	alias HHOOK function(int, HOOKPROC, HINSTANCE, DWORD) SetWindowsHookExWProc;
	static extern (Windows) HHOOK KBHook(int, HOOKPROC, HINSTANCE, DWORD)
	{
		asm
		{
			naked;
			ret;
		}
	}

	DWORD old;
	auto err = GetLastError();

	auto hModule = LoadLibrary("User32.dll");
	auto proc = cast(SetWindowsHookExAProc)GetProcAddress(hModule, "SetWindowsHookExA");
	err = GetLastError();
	VirtualProtect(proc, 40, PAGE_EXECUTE_READWRITE, &old);
	err = GetLastError();

	memcpy(proc, &KBHook, 7);
	// Cleanup	
	//FreeLibrary(hModule);
}


The code above is based on the idea from

https://github.com/microsoft/Detours

I didn't feel like investing the time to integrate it in to my code.

The code I've included is a hack that simply bypasses all hooking routines and hence the screwed up keyboard hook.

It only works for x64 since ret works to return properly(x86 messages with the stack and requires a bit more code).

You could easily make it work in general by using Detours or modify the code to work more generally. It works for my purpose. I do not know if it will allow keyboard input in to the gtk app though but it is irrelevant for my purposes now.

The main issues are:

  1. It must be able to decode the instruction set properly so it can a proper overwrite without screwing things up. Detours does this and works for a range of architectures.

  2. It should detect the keyboard hooks so others are not prevented.

  3. Ideally it will still call the keyboard hooks but prevent them from slowing things down(possibly use a thread).

If you could get Detours to work then you could include it and simply hook the function and write a little code to get everything to work.