Using Winscard.lib in Qt
-
Hello all,
I am creating a c++ application that uses the winscard library (I want to use smartcards and readers). I've created working code in Visual Studio 2017 but decided to switch over to Qt to create a decent UI. However, the code that runs 100% fine in VS, doesn't run in Qt and I've got no idea why. Please keep in mind that I'm new to Qt, this is what I've done.-
I've installed QT and used the maintenance tool, this is how it looks.
-
I've started a new project and copied my files into the new project.
-
I linked the library as an external library, and I linked it dynamically (I also tried static, didn't help).
-
These are my build settings:
-
This is a zip I created of the project. Its not the actual project, but its a simple/small one that demonstrates the problem.
-
Building and running the application works, so I suppose that the link to the library was successful?
-
I do need to note that I get a warning when right clicking the project and "Run qmake", but I'm guessing it only has to do with debugging. The waring is "warning: winrt_manifest_install.path is not defined: install target not created"
So, the problem: when running the application all goes well, the application shows a button you can click. The code behind the button tries to run "SCardEstablishContext" from the winscard library and displays the return value on the button. The return value is 5, but it should be 0 to be successful.
A return value of 5 is not a Smart Card Error Code, but a Windows System Error Code. The error code stands for ERROR_ACCESS_DENIED.
I've been looking around what the problem could be but I honestly have no idea. Again, I'm new to Qt so i may have missed something obvious.
To create the working Visual Studio 2017 project:
- Create a new c++ Windows Console Application
- Add the "Winscard.LIB" and "winscard.h" file to the directory.
- Add "winscard.h" to the project (In solution explorer, right click the solution -> Add -> existing item -> winscard.h)
- Go to properties (In solution explorer, right click the solution -> properties), then go to Linker -> Input -> Additional Dependencies and add "Winscard.lib".
- This is how my main looks (stdafx was auto generated when creating the project):
#include "stdafx.h" #include "winscard.h" #include <conio.h> #include <iostream> int main() { SCARDCONTEXT _context; LONG lRetValue = SCardEstablishContext(SCARD_SCOPE_USER, nullptr, nullptr, &_context); std::cout << lRetValue; _getch(); return 0; }
When running this, it outputs 0 (which is what I want. The VS project works but the Qt one doesn't, does anyone know what I'm doing wrong?
Thanks in advance,
Thomas -
-
Hi @Thomas_._
you selected Qt for UWP as your Kit. That builds an app for Windows RT, is that really what you want?
You have the other Kits installed too, they will generate code for "classic" Windows API.
-
Hi @aha_1980 , thanks for your reply.
It seems that I cannot use these kits, it says "Error: no Qt version set in kit" when I hover over them.
The Qt versions for these kits did not get auto detected for some reason, I tried adding the Qt versions manually but It doesn't allow me to select a Qt version.
-
I'd suggest to remove everything and install again from scratch:
- You don't need the Preview Creator
- You don't need MinGW if you want to use MSVC
- You don't need the Android and UWP versions
I'd just install the one Qt 5.11 for your Visual Studio C++ compiler (you need to expand the Qt 5.11.1 branch in the installer for that.
That would reduce some confusion when searching for problems.
Regads
-
@Thomas_._
can you expand the qt 5.11.1 tree ? seems like you did not select the correct qt versions, that way we could verify that assumption -
@Thomas_._ Why did you select complete Qt 5.11.1?!
You installed all Qt 5.11.1 builds (for MinGW, for all supported Visual Studio versions, for Android,...).
Only install Qt versions you really need. -
@aha_1980, @J-Hilk and @jsulm, thanks for the quick responses.
So I removed Qt and reinstalled it, this time I only installed the MSVC 2017 64bit. I also installed one of the tools as it was selected by default. The image shows I also installed MSVC 2015 32bit, but I only did this afterwards.
However, I got the following when trying to build.
There's the strange error on the bottom, but also the warning that Winscard.LIB is a x86 library and I'm targeting x64. It seems that there is no 32bit version for MSVS2017?
Furthermore, Qt is giving an error in the winscard.h file, but I guess this may have something to do with the x86 and x64 mismatch? "Error: unterminated conditional directive" it says. but the #ifndef is definitely closed with an #endif at the end.So I decided that maybe I should try a 32-bit version, and thus I installed MSVC 2015 32-bit aswell. When trying to build this, I got the error " error: LNK1158: cannot run 'rc.exe' ". I do not have Visual studio 2015 installed tho, so this probably is the problem here.
Do I install MSVC 2015 and try the MSVC2015 32-bit? Or should the 2017 work despite the x86 and x64 incompatibility?
Thanks,
Thomas -
Hi @Thomas_._
There's the strange error on the bottom, but also the warning that Winscard.LIB is a x86 library and I'm targeting x64. It seems that there is no 32bit version for MSVS2017?
No precompiled, at least, but see below.
Furthermore, Qt is giving an error in the winscard.h file, but I guess this may have something to do with the x86 and x64 mismatch? "Error: unterminated conditional directive" it says. but the #ifndef is definitely closed with an #endif at the end.
I think we can ignore that in first place.
So I decided that maybe I should try a 32-bit version, and thus I installed MSVC 2015 32-bit aswell.
That should work if you have a MSVC 2017 32 bit compiler (please check in Tools > Options > Kits > Compilers) as MSVC 2015 and 2017 are compatible.
When trying to build this, I got the error " error: LNK1158: cannot run 'rc.exe' ". I do not have Visual studio 2015 installed tho, so this probably is the problem here.
I remember that I have heard this problem before... but MSVC 2017 should also contain rc.exe...
Or should the 2017 work despite the x86 and x64 incompatibility?
No, you cannot mix 32 and 64 bit DLLs and apps. But can you get a 64 bit version of that DLL?
Regards
-
@aha_1980, thanks, you solved my problem.
I'ts quite stupid that I did not think of this myself, but I do indeed have a 64 bit version of the DLL, using this fixed my problem for 64bit.I also have a 32 bit compiler, using this compiler and the MSVC 2015 32-bit Qt version, I also managed to get the 32-bit Winscard lib working.
My problem is fixed, thank you!
Kind regards,
Thomas -
Glad you got it working. Good luck with your project!