PDA

View Full Version : Calling external DLL function with Qt



yaba
29th September 2011, 00:14
Hi all,
I bet this is an old question, and I did some research at StackOverflow and Google, however, I ended up more confused.
How do I simply call a dll function? without linking the dll or other mumbojumbo :)
something like:

rundll32 USER32.DLL,LockWorkStation
Thank you.

franz
29th September 2011, 06:18
To run a dll function you need to link against it, either at compile/link time or at run time. At build time you can configure your linker to link against user32.lib, which will automatically resolve the function for you. You can then just call

LockWorkStation()

If you want to link during run-time, use QLibrary.


BOOL (*lockWorkStation)();
...
QLibrary user32("user32.dll", this);
lockWorkStation = user32.resolve("LockWorkStation");
lockWorkStation();

Note that the above dry-coded and might contain errors.

Added after 5 minutes:

Maybe your suggestion could be done using QProcess:


QProcess::execute("rundll32", QStringList("USER32.DLL,LockWorkStation"));

ChrisW67
29th September 2011, 07:47
Any Win32 GUI app is already linked to User32.dll so you should be able to just call LockWorkstation() (http://msdn.microsoft.com/en-us/library/windows/desktop/aa376875%28v=vs.85%29.aspx) directly with no extra effort.