stdlib issue for CryptoSys API

This solves two issues for C and C++ programmers with CryptoSys API:
  1. Why won't it compile with Visual C++ 2005 Express?
  2. 'Unresolved external ... referenced from xxx.OBJ' error with Borland C++

Symptoms

In Borland C++:
[Linker Error] Unresolved external '__stdcall API_Function(...) referenced from xxx.OBJ
or
Error: Unresolved external '_API_Function' referenced from xxx.OBJ'
In Visual C++ 2005 Express:
error LNK2019: unresolved external symbol _API_Function referenced in function _main
or
fatal error C1083: Cannot open include file: 'windows.h': No such file or directory

The problem

The C include file diCryptoSys.h for version 3.2.0 of CryptoSys API contains the following at lines 19-27:-
/* We only need windows.h for _stdcall convention */
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
/* Ignore for Linux */
#define _stdcall
#endif
The preprocessor #ifdef directive was introduced in version 3.2 so we could use the same include file for the Linux distribution.

The first error is that this should have read

/* We only need windows.h for __stdcall convention */
#if defined(_WIN32) || defined(WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
/* Ignore for Linux */
#define _stdcall
#endif

The preprocessor constant WIN32 is defined by default in earlier versions of MSVC++ but not in Borland C++ or in Visual C++ 2005 Express (MSVC++8). Hence the "unresolved external" problems with those compilers.

In the manual we suggested one solution for Visual C++ 2005 Express (MSVC++8):
Set the Calling Convention to __stdcall (/Gz) using Project>Properties>Configuration Properties>Advanced>Calling Convention.
This is now redundant and can be ignored. However, users who had downloaded the vanilla version of Visual C++ 2005 Express would also end up with the "missing windows.h" problem (see below on how to solve).

The Solution

In fact, in both MSVC and Borland, you do not need the reference to windows.h at all. _stdcall (or __stdcall) is a standard keyword for both compilers. You can delete all the 8 lines above from the CryptoSys .h include files and both the "unresolved external" and "missing windows.h" problems go away.

A better, Linux-compatible solution is to replace the 8 lines above with:

/* __stdcall convention required for Win32 DLL only */
#if defined(unix) || defined (linux) || defined(__linux)
#define _stdcall
#endif	

Remarks

This page last updated 1 January 2008

CryptoSys Home | Search | Cryptography Software Code | Contact us
Copyright © 2006-8 D.I. Management Services Pty Limited ABN 78 083 210 584, Sydney, Australia. All rights reserved.
<www.di-mgt.com.au>   <www.cryptosys.net>