View mode: basic / threaded / horizontal-split · Log in · Help
July 20, 2012
More WinAPI problems
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 ----- >
July 20, 2012
Re: More WinAPI problems
On Fri, 20 Jul 2012 14:07:51 +0100, DLimited <tanojoshu@googlemail.com>  
wrote:

> 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?

How are you passing the DLL filename to LoadLibraryA/W?  Are you passing a  
string literal, i.e.

LoadLibraryW("C:\\folder\dllname.dll");

Have you tried the string literal suffix 'w', e.g.

LoadLibraryW("C:\\folder\dllname.dll"w);

R
July 20, 2012
Re: More WinAPI problems
Do you know DependencyWalker? It might help you to check dependencies.
Here is a screenshot:
http://oldcigaret.info/win2k/DependWguidepics/DependW_2.jpg and it's
freeware. http://www.dependencywalker.com/
July 21, 2012
Re: More WinAPI problems
INPUT doesn't match C declaration and probably has wrong size. 
Also to properly handle errors first check return code and then 
last error.
Top | Discussion index | About this forum | D home