More WinAPI problems

DLimited tanojoshu at googlemail.com
Fri Jul 20 06:07:51 PDT 2012


Hello everyone,

I encountered a few more problems while creating my system-wide 
makro program.

1)
I can't load my dll with LoadLibraryW, only LoadLibraryA. Why?

2)
The LoadLibraryA function "fails" with Error Code 127 - I still 
get a Handle and can register my function as a 
LowLevelKeyboardProc, so I'm not really sure what went wrong. 
Google tells me Code 127 means my .dll is dependant on other 
.dlls which can't be found, but I have no idea which/why/how.

3)
I intend to use the SendInput function to simulate Unicode 
character presses - to be precise, I want to inject this --> 
ಠ_ಠ <-- whenever I press CTRL+SHIFT+ALT+Q  (for example). 
Sadly SendInput gives me Error Code 87 ("Incorrect Parameter"), 
but I don't know what I'm doing wrong.

I hope you guys can help me out!

This is the .dll code:

< ----- CODE BEGIN ----- >
import std.c.windows.windows;
import core.sys.windows.dll;
import core.runtime;
import std.stdio;

//const auto INPUT_KEYBOARD = 1;
//const auto KEYEVENTF_UNICODE = 4;

extern (C) void gc_init();
extern (C) void gc_term();
extern (C) void _minit();
extern (C) void _moduleCtor();
extern (C) void _moduleDtor();

extern (Windows) struct KBDLLHOOKSTRUCT {
   DWORD     vkCode;
   DWORD     scanCode;
   DWORD     flags;
   DWORD     time;
   ULONG_PTR dwExtraInfo;
};

extern (Windows) struct KEYBDINPUT {
   WORD      wVk;
   WORD      wScan;
   DWORD     dwFlags;
   DWORD     time;
   ULONG_PTR dwExtraInfo;
};


extern (Windows) struct INPUT {
	DWORD type;

	KEYBDINPUT ki;
	
};

extern (Windows) LRESULT CallNextHookEx(
     HANDLE hhk,
       int nCode,
        WPARAM wParam,
         LPARAM lParam
);

extern (Windows) SHORT GetKeyState(
	int nVirtKey
);

extern (Windows) UINT SendInput(
	UINT nInputs,
	INPUT* pInputs,
	int cbSize
);


__gshared HINSTANCE g_hInst;


  extern (Windows) BOOL DllMain(HINSTANCE hInstance, ULONG 
ulReason, LPVOID pvReserved) {
	g_hInst = hInstance;
	switch (ulReason) {
		case DLL_PROCESS_ATTACH:
		g_hInst = hInstance;
		Runtime.initialize;
		//dll_process_attach( hInstance, true );
		break;
		
		case DLL_PROCESS_DETACH:
		//dll_process_detach( hInstance, true );
		break;

		case DLL_THREAD_ATTACH:
		//dll_thread_attach( true, true );
		break;
		case DLL_THREAD_DETACH:
		//dll_thread_detach( true, true );
		break;
		
		default:
		return true;
	}
	return true;
}

extern (Windows) LRESULT LowLevelKeyboardProc(int code, WPARAM 
wParam, LPARAM lParam)
{
	Runtime.initialize;
	//MessageBoxA(null, "HALLO", "OK",0);
     KBDLLHOOKSTRUCT* details = cast(KBDLLHOOKSTRUCT*) lParam;
	
     if(code == 0 && wParam == WM_KEYDOWN)
     {
         if(details.vkCode == 0x51) {
		
		SHORT shiftkey = GetKeyState( VK_SHIFT );
		SHORT ctrlkey = GetKeyState( VK_CONTROL );
		SHORT altkey = GetKeyState ( VK_MENU );
		if( ( shiftkey == -127 || shiftkey == -128 ) && ( ctrlkey == 
-127 || ctrlkey == -128 ) && ( altkey == -127 || altkey == -128 ) 
)
			{
				//MessageBoxA(null, "BOOM", "OK",0);
			
				INPUT[3] toSend = new INPUT[3];
				toSend[0].type = 1;
				toSend[0].ki.wVk = 0;
				toSend[0].ki.wScan = 0x0CA0;
				toSend[0].ki.dwFlags = 4;
				toSend[0].ki.time = 0;
				toSend[0].ki.dwExtraInfo = details.dwExtraInfo;
				
				toSend[1].type = 1;
				toSend[1].ki.wVk = 0;
				toSend[1].ki.wScan = 0x005F;
				toSend[1].ki.dwFlags = 4;
				toSend[1].ki.time = 0;
				toSend[1].ki.dwExtraInfo = details.dwExtraInfo;
				
				toSend[2].type = 1;
				toSend[2].ki.wVk = 0;
				toSend[2].ki.wScan = 0x0CA0;
				toSend[2].ki.dwFlags = 4;
				toSend[2].ki.time = 0;
				toSend[2].ki.dwExtraInfo = details.dwExtraInfo;
				SendInput( cast(UINT)3, toSend.ptr, toSend[0].sizeof );
				writeln(GetLastError());
				//MessageBoxA(null, cast(char*)GetLastError(), "OK",0);
				
				
				return 1;
			}
		}
     }

     return CallNextHookEx(null, code, wParam, lParam);
}

< ----- CODE END ----- >


More information about the Digitalmars-d-learn mailing list