A Simple DLL Wrapper

torhu fake at address.dude
Fri Feb 23 07:36:26 PST 2007


oldrev wrote:
<snip>
> 
> void main()
> {
>     
>     auto dll = new Module!("User32.dll", 
>             Symbol!("MessageBoxW", int, HWND, LPCWSTR, LPCWSTR, UINT), 
>             Symbol!("MessageBoxA", int, HWND, LPCSTR, LPCSTR, UINT) 
>             ); 
> 
>     dll.MessageBoxW(null, "Hello! DLL! W", "Title W", MB_OK);
>     dll.MessageBoxA(null, "Hello! DLL! A", "Title A", MB_OK);
> 
> }


Pretty cool.  Reminds me abit of the python module called ctypes. 
Here's the equivalent of your main() using that:

from ctypes import windll
MB_OK = 0
windll.user32.MessageBoxA(None, "Hello! DLL! ", "Hello from 
MessageBoxA", MB_OK)
windll.user32.MessageBoxW(None, u"Hello! DLL! ", u"Hello from 
MessageBoxW", MB_OK)


windll is an object that loads a dll file, and expects functions to use 
the stdcall conventions.  cdll excpects regular C calling conventions.

Arguments are converted correctly according to their types, only works 
for ansi and utf8 string, and ints for now.  You have to specify other 
types explicitly.  Functions are assumed to return int unless otherwise 
specified.

You can do pointless stuff like this too:
cdll.msvcrt.strlen("123") # returns 3

http://python.net/crew/theller/ctypes/tutorial.html  (use google cache 
if down)
http://docs.python.org/lib/module-ctypes.html


More information about the Digitalmars-d-learn mailing list