August 1999

Ras update

by Kent Reisdorph

In our March and April issues we presented articles on using Microsoft’s Remote Access Services (RAS) to establish dial-up connections. Those articles were written using C++Builder 3. With C++Builder 4, however, I noticed that my RAS applications no longer worked as I expected. One of the symptoms was a runtime error from some of the RAS functions. The return value from those functions was ERROR_BUFFER_TOO_SMALL. The textual description of this error is, “The caller’s buffer is too small.” I spent more than a few hours trying to track down this problem. I started digging around in RAS.H and found some entries like this:

RASCONNA
{
  DWORD dwSize;
  HRASCONN hrasconn;
  CHAR szEntryName[ RAS_MaxEntryName + 1 ];
  #if (WINVER >= 0x400)
  CHAR szDeviceType[ RAS_MaxDeviceType + 1];
  CHAR szDeviceName[ RAS_MaxDeviceName + 1];
  #endif
  #if (WINVER >= 0x401)
  CHAR szPhonebook [ MAX_PATH ];
  DWORD dwSubEntry;
  #endif
};

Hmm… that could certainly lead to a “buffer too small error” if the WINVER macro were defined as something other than 0x0400. Curious, I started hunting in the Windows headers. Here’s what I found in WINDEF.H:

#ifndef WINVER
#define WINVER 0x0500
#endif /* WINVER */

Aha! Presumably Microsoft and Borland were building support for Windows 2000 (which includes many enhancements to RAS) into the Windows headers and those headers shipped with C++Builder 4. In C++Builder 3, WINVER was defined as 0x0400 so obviously something changed.
Once I figured out the problem, it did not take long to fix it. If you are building applications that use RAS in C++Builder 4 you must use this construct when including the RAS headers:

#pragma warn -dup
#define WINVER 0x400
#include <ras.h>
#include <raserror.h>
#define WINVER 0x500

This code temporarily redefines the WINVER macro to 0x400 and then sets it back to 0x500 after including the RAS headers. The line that reads, #pragma warn -dup suppresses the warning the compiler will issue that says:

Redefinition of ‘WINVER’ is not identical.

It’s not only RAS.H that suffers from this problem. For a list of affected headers, search the Windows headers for these lines of text:

WINVER >= 0x401
WINVER >= 0x0500
WINVER >= 0x500

You will no doubt find several places where problems may arise with your existing code. 
As I have said, this problem caused me a few hours of aggravation before I figured out what was going wrong. I hope that this information will help you avoid the frustration I experienced before I discovered the solution.