Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Cannot load user32.dll from extern C file

  1. #1
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Cannot load user32.dll from extern C file

    In a C file I load into my QT app:
    Qt Code:
    1. extern "C"{
    2. #include "raw_mouse.h"
    3. }
    To copy to clipboard, switch view to plain text mode 
    When it tries to load user32.dll:
    Qt Code:
    1. HMODULE user32 = LoadLibrary("user32.dll");
    2. if (!user32) return 0;
    To copy to clipboard, switch view to plain text mode 
    It fails. Why?

  2. #2
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cannot load user32.dll from extern C file

    What does GetLastError() say?

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cannot load user32.dll from extern C file

    I think you'll find your application already has a dependency on user32.dll and so is already linked to your project - there's no need to load it again.

  4. #4
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Cannot load user32.dll from extern C file

    If it is already linked, how can I assign it to (HMODULE user32)?

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Cannot load user32.dll from extern C file

    Why do you need to?

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cannot load user32.dll from extern C file

    What functions are you trying to use? Most already have thunks, so you can just call them directly.

  7. #7
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Cannot load user32.dll from extern C file

    Why do you need to?
    Because the rest of the C app needs HMODULE user32.
    Qt Code:
    1. // Return 0 if rawinput is not available
    2. HMODULE user32 = LoadLibrary("user32.dll");
    3. if (!user32) return 0;
    4. _RRID = (pRegisterRawInputDevices)GetProcAddress(user32,"RegisterRawInputDevices");
    5. if (!_RRID) return 0;
    6. _GRIDL = (pGetRawInputDeviceList)GetProcAddress(user32,"GetRawInputDeviceList");
    7. if (!_GRIDL) return 0;
    8. _GRIDIA = (pGetRawInputDeviceInfoA)GetProcAddress(user32,"GetRawInputDeviceInfoA");
    9. if (!_GRIDIA) return 0;
    10. _GRID = (pGetRawInputData)GetProcAddress(user32,"GetRawInputData");
    11. if (!_GRID) return 0;
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  8. #8
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cannot load user32.dll from extern C file

    The library having been loaded by the pe loader already (quite possibly the case) will not make LoadLibrary fail. Again, what does GetLastError() say?

  9. #9
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cannot load user32.dll from extern C file

    In a Windows application, I can just use RegisterRawInputDevices(), I've never needed to do LoadLibrary/GetProcAddress, as they are standard functions.

    This is why I'm confused. It's a standard library.

  10. #10
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cannot load user32.dll from extern C file

    I'm thinking that this code was originally written to allow an assembly program to use the interface too (meaning it's probably quite old). Assembly modules carry no implied dependencies, so the linker does not automatically bind to any external modules.

  11. #11
    Join Date
    Feb 2010
    Posts
    52
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Cannot load user32.dll from extern C file

    user32.dll should already be loaded and the msdn documentation doesn't actually specify what happens if you call LoadLibrary on a dll which is already loaded, though I see no reason why it would not work ok. Did you ever try using GetLastError ?

    You can always try using GetModuleHandle to get the handle to the already loaded dll if you really do want to use GetProcAddress. (though I do agree with squidge that this really isn't necessary).

    Chris.

  12. #12
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cannot load user32.dll from extern C file

    Quote Originally Posted by chris_helloworld View Post
    the msdn documentation doesn't actually specify what happens if you call LoadLibrary on a dll which is already loaded
    It does. It just takes a closer read.

    The system maintains a per-process reference count on all loaded modules. Calling LoadLibrary increments the reference count.

  13. #13
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Cannot load user32.dll from extern C file

    You can always try using GetModuleHandle to get the handle to the already loaded dll if you really do want to use GetProcAddress.
    If there is a better way I would definitely go for it.

    Did you ever try using GetLastError ?
    Yes:
    Qt Code:
    1. qDebug() << init_raw_mouse(1, 0, 1) // Outputs 0;
    2. qDebug() << GetLastError(); // Outputs 0
    To copy to clipboard, switch view to plain text mode 

    Here they say that if I recompile QT passing -D _WIN32_WINNT=0×501 as arguments, I could filter WM_INPUT messages.
    But I still would have to use that C file for the rest of the implementation(to get the different input's and their respective x & y axis).
    How can I rebuild QT? Can that happen trough QtCreator? I have VS2010, but don't use it.


    Added after 4 minutes
    :

    Quote Originally Posted by squidge View Post
    In a Windows application, I can just use RegisterRawInputDevices(), I've never needed to do LoadLibrary/GetProcAddress, as they are standard functions.

    This is why I'm confused. It's a standard library.
    So if I included windows.h I could trim the code a bit? (I have no idea on Win32 programming.)


    Added after 6 minutes
    :
    The website I downloaded the demo was here. I'm including the whole app in the attachment.
    Attached Files Attached Files
    Last edited by been_1990; 22nd October 2010 at 20:30.

  14. #14
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cannot load user32.dll from extern C file

    GetLastError returns the last error message, it must be called immediately after the function that has failed, with no other function calls in-between. This is not the case for your code (you even have a qDebug between them), so it's useless in this case.

    Put the call after the LoadLibrary, assign it to a variable, and either output it there, or put a breakpoint and check the value in the debugger.

  15. #15
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cannot load user32.dll from extern C file

    Your call to GetLastError is too far away from the call site to be of any benefit (Qt is probably reseting it). You need to place the call immediately after a winapi call. In this case, it's a pain in the arse because that would require you to modify raw_mouse.cpp
    Last edited by Timoteo; 22nd October 2010 at 20:35. Reason: hah! beaten!

  16. #16
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Cannot load user32.dll from extern C file

    Ok, I just saw I get some warnings:
    Qt Code:
    1. E:/QT Projects/rawInput-build-desktop/../rawInput/raw_mouse.c:154: warning: passing argument 1 of 'LoadLibraryW' from incompatible pointer type
    2. c:\qt\2010.05\mingw\bin\../lib/gcc/mingw32/4.4.0/../../../../include/winbase.h:1727: expected 'LPCWSTR' but argument is of type 'char *'
    To copy to clipboard, switch view to plain text mode 

    PS: Im trying to use GetLastError, but Im getting some problems fromQtCreator, so it will take a while.

  17. #17
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cannot load user32.dll from extern C file

    That's probably why. You have unicode defined, but the source (raw_mouse.cpp) is using ANSI. I'm guessing that LoadLibrary has been failing all along with "no such file or directory". To continue as you are, you need to set your project up to use single-byte characters or you can patch raw_mouse.cpp to use the _TEXT() macro around the string literals. (e.g. _TEXT("user32.dll")).

    Edit: After looking at it again, I can see that "char" is used too extensively to bother making it unicode compliant. Unless you have a compelling reason otherwise, you would be better served to just use ANSI encoding.
    Last edited by Timoteo; 22nd October 2010 at 21:05.

  18. #18
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Cannot load user32.dll from extern C file

    How do I set the rest of my application to ANSI?

    Edit:
    GetLastError() outputs '5716627'.

  19. #19
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cannot load user32.dll from extern C file

    5716627 doesn't sound like a valid error code, how do you get that?

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Cannot load user32.dll from extern C file

    Sorry to cut into the discussion but wouldn't it be simpler to rewrite this init_raw_mouse function to do the following instead of the "LoadLibrary" part?

    Qt Code:
    1. _RRID = RegisterRawInputDevices;
    2. if (!_RRID) return 2;
    3. _GRIDL = GetRawInputDeviceList;
    4. if (!_GRIDL) return 3;
    5. _GRIDIA = GetRawInputDeviceInfoA;
    6. if (!_GRIDIA) return 33;
    7. _GRID = GetRawInputData;
    8. if (!_GRID) return 4;
    To copy to clipboard, switch view to plain text mode 

    The rest of the code could stay the same.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  21. The following user says thank you to wysota for this useful post:

    been_1990 (22nd October 2010)

Similar Threads

  1. Can Qwebview load xml/xsl file?
    By richardander in forum Qt Programming
    Replies: 3
    Last Post: 26th August 2015, 23:36
  2. Save/Load variables to FIle
    By Jordan in forum Qt Programming
    Replies: 2
    Last Post: 26th May 2010, 12:35
  3. How to load a .h or .cpp file in designer?
    By srohit24 in forum Qt Programming
    Replies: 2
    Last Post: 18th February 2009, 14:33
  4. extern class
    By zorro68 in forum Qt Programming
    Replies: 2
    Last Post: 5th October 2007, 22:08
  5. Load ActiveX from file
    By Passer_dj in forum Qt Programming
    Replies: 1
    Last Post: 14th August 2007, 00:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.