Errors when including winscard.h

Hi All, Im after some advice.

Im trying to connect a cardreader to our UE4.27 project, the SDK for the cardreader uses.

winscard.h

When i import it the following compile errors

C:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\um\winnt.h(584): warning C4005: ‘TEXT’: macro redefinition
C:\Program Files\Epic Games\UE_4.27\Engine\Source\Runtime\Core\Public\HAL/Platform.h(1085): note: see previous definition of ‘TEXT’
C:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\um\winioctl.h(7542): error C4668: ‘_WIN32_WINNT_WIN10_TH2’ is not defined as a preprocessor macro, replacing with ‘0’ for ‘#if/#elif
C:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\um\winioctl.h(7548): error C4668: ‘_WIN32_WINNT_WIN10_RS1’ is not defined as a preprocessor macro, replacing with ‘0’ for ‘#if/#elif
C:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\um\winioctl.h(7553): error C4668: ‘_WIN32_WINNT_WIN10_TH2’ is not defined as a preprocessor macro, replacing with ‘0’ for ‘#if/#elif
C:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\um\winioctl.h(7557): error C4668: ‘_WIN32_WINNT_WIN10_TH2’ is not defined as a preprocessor macro, replacing with ‘0’ for ‘#if/#elif
C:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\um\winioctl.h(7564): error C4668: ‘_WIN32_WINNT_WIN10_RS2’ is not defined as a preprocessor macro, replacing with ‘0’ for ‘#if/#elif
C:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\um\winioctl.h(7574): error C4668: ‘_WIN32_WINNT_WIN10_RS2’ is not defined as a preprocessor macro, replacing with ‘0’ for ‘#if/#elif
C:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\um\winioctl.h(7578): error C4668: ‘_WIN32_WINNT_WIN10_TH2’ is not defined as a preprocessor macro, replacing with ‘0’ for ‘#if/#elif
C:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\um\winioctl.h(7583): error C4668: ‘_WIN32_WINNT_WIN10_RS3’ is not defined as a preprocessor macro, replacing with ‘0’ for ‘#if/#elif
C:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\um\winioctl.h(7777): error C4668: ‘_WIN32_WINNT_WIN10_RS2’ is not defined as a preprocessor macro, replacing with ‘0’ for ‘#if/#elif
C:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\um\winioctl.h(12249): error C4668: ‘_WIN32_WINNT_WIN10_RS3’ is not defined as a preprocessor macro, replacing with ‘0’ for ‘#if/#elif
C:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\um\winioctl.h(12266): error C4668: ‘_WIN32_WINNT_WIN10_RS3’ is not defined as a preprocessor macro, replacing with ‘0’ for ‘#if/#elif
C:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\um\winioctl.h(12285): error C4668: ‘_WIN32_WINNT_WIN10_RS2’ is not defined as a preprocessor macro, replacing with ‘0’ for ‘#if/#elif
C:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\um\winioctl.h(12404): error C4668: ‘_WIN32_WINNT_WIN10_RS3’ is not defined as a preprocessor macro, replacing with ‘0’ for ‘#if/#elif

After some searches have tried

so i have
#ifdef TEXT
#undef TEXT // Delete macro UE4
#endif
include <winscard.h>
#ifdef TEXT
#undef TEXT // Delete macro Win32
#endif
#define TEXT(quote) __ TEXT(quote) // Define macro UE4

and in my build.cs file

new string
{
“_WIN32_WINNT_WIN10_TH2”,“_WIN32_WINNT_WIN10_RS1”,
“_WIN32_WINNT_WIN10_RS2”,
“_WIN32_WINNT_WIN10_RS3”,
“_WIN32_WINNT_WIN10_RS4”,
“_WIN32_WINNT_WIN10_RS5”
}
);

so now it compiles with an empty file but when i do anything like

bool UACSCardReader::EstablishContext()
{
retCode = SCardEstablishContext(SCARD_SCOPE_USER,NULL,NULL, &hContext);
if (retCode != SCARD_S_SUCCESS)
{
return false;
}
else
{
return true;
}
}

it throws this error

ACSCardReader.cpp.obj : error LNK2019: unresolved external symbol SCardEstablishContext referenced in function “public: static bool __cdecl UACSCardReader::EstablishContext(void)” (?EstablishContext@UACSCardReader@@SA_NXZ)

Am i missing something or doing something stupid

The only other option i can think of using WinSCard.dll using FPlatformProcess::GetDllHandle and using FPlatformProcess::GetDllExport to get the methods

I have done this before to connect to other hardware to connect to a plc and usb devices, however in this situation i would prefer to find a solution to use include <winscard.h>

Thanks

1 Like

OK i have started with the DLL exporting but i’m having issues

When i play i now get a message every time i stop it saying.

“source2 warning”
“Only one instance of the game can be running at one time”

this happens in editor or after a build.

also im tring to FPlatformProcess::GetDllExport the function
SCardConnectW function (winscard.h) - Win32 apps | Microsoft Learn

i currently get Unhandled Exception: EXCEPTION_ACCESS_VIOLATION 0x0000000000000000 when i run it which normally means i have screwed something up in the reference i have change what i have written so many times, i will tidy it up and post it, but any advice would be good.

Yeah, this is a necro - but recently had to do this for a project and this is one of the first things that pops up in search engines so wanted to post an answer.

In order to include winscard on Windows, you’ll need to add an additional library from the Windows SDK in your build.cs. Once you do that you can include winscard.h as a third party include and your all set.

In build.cs add:

PublicAdditionalLibraries.Add(Path.Combine(Target.WindowsPlatform.WindowsSdkDir,
                                        "Lib",
                                        Target.WindowsPlatform.WindowsSdkVersion,
					"um/x64", // Probably a way to get this dynamically, but I didn't bother since it's a short run one off.
					"winscard.lib"));

in your cpp add:

THIRD_PARTY_INCLUDES_START
#include <winscard.h>
THIRD_PARTY_INCLUDES_END

Once that’s in place the UnrealBuildTool will pull the library in, and you should be all set. I did this for blueprint library plugin recently and it worked well enough.

1 Like

Sorry to reply to an old post but I wanted to say thank you so much that worked.

Card reader is on site and everyone is happy.

Thank you for your help.