Dan Griffin's Blog
Comments on security, PKI, smart cards, cryptography, and entrepreneurship.
CardAcquireContext needs to be a dll export
August 7, 2007
Just ran into an interesting compatibility issue between SmartUtil (http://www.jwsecure.com/downloads.html) and a smart card minidriver under development. I suppose this is also at least partially due to the needlessly confusing way that Visual Studio handles dll exports.
In summary, CardAcquireContext (http://msdn2.microsoft.com/EN-US/library/aa375757.aspx) must be exported (un-mangled) from the minidriver dll. No big problem there. The challenge in building with Visual Studio is that the cardmod.h header (which is available via the CNG SDK, not the primary Windows SDK; there’s another Frequently Asked Question), like the majority of the Windows public headers, doesn’t use the __declspec(import/export) attributes. Thus - although I haven’t tried this personally - attempting to adorn a CardAcquireContext implementation with __declspec(dllexport) will probably result in a build failure, since it conflicts with the header.
The particular compatibility issue occurred when, as a work around, the developer modified the function declaration in cardmod.h to use __declspec(dllexport). More importantly, he also happened to drop the calling convention (i.e. WINAPI). The net result was that, during testing, CardAcquireContext would return 0 (success) but, under the covers, SCardUIDlgSelectCard would fail. Thus, SmartUtil would report an error.
The moral of the story is that, in most cases, and always in this one, you should use a .def file to define your exports. A typical minidriver should have three - CardAcquireContext, DllRegisterServer, and DllUnregisterServer. But the latter two are optional. Anyway, this approach fixed the issue above.
One final editorial note: in my opinion, exposing the other minidriver/CARD_DATA interface routines via dll export is an implementation bug, since they’re only supposed to be accessible via function pointers. But perhaps that’s a debate to be had over beers after hours …
CNG SDK download - http://www.microsoft.com/downloads/details.aspx?familyid=1ef399e9-b018-49db-a98b-0ced7cb8ff6f&displaylang=en.
Permalink |No Comments »
No comments yet.
RSS feed for comments on this post. TrackBack URL