64-Bit Programming with Visual C++

Why 64-bit?

Today, 64-bit processors have become the standard for systems ranging from the most scalable servers to desktop PCs. The way to take full advantage of these systems is with 64-bit editions of Microsoft Windows products.

The 64-bit systems offer direct access to more virtual and physical memory than 32-bit systems and process more data per clock cycle, enabling more scalable, higher performing computing solutions. There are two 64-bit Windows platforms: x64-based and Itanium-based.

x64 solutions are the direct descendants of x86 32-bit products, and are the natural choice for most server application deployments—small or large. Itanium-based systems offer alternative system designs and a processor architecture best suited to extremely large database and custom application solutions.

You will need a 64-bit compiler

To develop 64-bit applications you must install one or both of the Visual C++ 64-bit compilers, otherwise 64-bit project configurations will not be available in the IDE.

Visual Studio provides 64-bit versions of some components, and also provides tools that are specific to 64-bit development.

Feature  

Microsoft Visual C++ Express Edition

Visual Studio 2008 Standard  

Visual Studio 2008 Professional  

Visual Studio 2008 Team System  

32-bit x86 compiler  

X  

X  

X  

X  

64-bit x64 compiler and cross-compiler  

   

X  

X  

X  

64-bit Itanium compiler and cross-compiler  

   

   

   

X  

 

Targeting Itanium or AMD

/MACHINE (Specify Target Platform) is set to /MACHINE:IA64 or /MACHINE:X64.

Target Environment is set to /env x64 or /env ia64.

There are two major 64 bit architectures available ,one is x64 and other is IA64.

X64 refers to 64 bit descendant of x86 family.

These architectures are totally different architectures .

The Itanium processor

It has a different instruction set from its x86 predecessors; in fact, it had a new kind of instruction set altogether, a kind which it called EPIC, which stands for, Explicitly Parallel Instruction Computing, it is not backwardly compatible with the established x86 instruction set.

Opteron from AMD

The instruction set for the x64 is a superset of that the x86 instruction set. In fact, only two instructions were added in Opteron from AMD!

Capability to run x86 code.

The Itanium processor incorporates a decoder that translates x86 instructions into EPIC instructions .That decoding process takes time, and, as a result, x86 applications perform relatively poorly on Itanium processors: they run at the speed they typically would on a 1.5 GHz Xeon processor.

Crucially, a machine with an Itanium processor cannot boot an x86 operating system..

Second, the processor has a switch to be flipped by the operating system that determines whether it is executing instructions in 32-bit or 64-bit mode. If the operating system is itself 32-bit, then the processor goes into 32-bit mode for the duration of its execution of that O/S. If the operating system is 64-bit, but a 32-bit x86 application is executed, then the processor goes into 32-bit mode as it executes that application's instructions.

Common Visual C++ 64-bit Migration Issues

Hard to believe - I thought word size changes on 64-bit OS's

An int and a long are 32-bit values on 64-bit Windows operating systems. For programs that you plan to compile for 64-bit platforms, you should be careful not to assign pointers to 32-bit variables. Pointers are 64-bit on 64-bit platforms, and you will truncate the pointer value if you assign it to a 32-bit variable.

Easy to believe

size_t, time_t, and ptrdiff_t are 64-bit values on 64-bit Windows operating systems.

time_t is a 32-bit value on 32-bit Windows operating systems in Visual C++ versions before Visual C++ 2005. In Visual C++ 2005 and later, time_t is a 64-bit integer by default.

The %x (hex int format) printf modifier will not work as expected on a 64-bit Windows operating system. It will only operate on the first 32 bits of the value that is passed to it.

Use %I32x to display an integer on a Windows 32-bit operating system.

Use %I64x to display an integer on a Windows 64-bit operating system.

The %p (hex format for a pointer) will work as expected on a 64-bit Windows operating system.

Products To Help Migration

Viva64 Tool To Help With Migration

Viva64 is a tool designed for software developers. With this tool, you can easily convert 32-bit applications to 64-bit ones.

Viva64 is integrated into Microsoft Visual Studio 2005 as its extension module. Below is the appearance of Microsoft Visual Studio 2005 with Viva64 integrated into it.

https://www.viva64.com/content/articles/64-bit-development/?f=Viva64_-_what_is_and_for.html&lang=en&content=64-bit-development

Other Tools

Parasoft C++test (www.parasoft.com);

Gimpel Software PC-Lint (www.gimpel.com).

Wizard Generated Code Conversion Issues - Managed Extensions Only    

Visual Studio wizards generate code in your source files, either when the project is created, or when functionality is added via a code wizard.

When upgrading a project, it is usually necessary to upgrade the wizard-generated code as well. For Microsoft Visual C++ 2005, a common scenario is upgrading code written with Managed Extensions for C++ to use new Visual C++ language features instead.

https://msdn.microsoft.com/en-us/library/ms173356(VS.80).aspx

How to: Modify WINVER - MFC Issue

When you build an MFC project from a previous release in Visual C++ .NET, you may see compilation errors because your project is now using a different value for WINVER.

Remarks

When your project was being built in a previous version of Visual C++, WINVER may have been set to 400, to allow compilation using an earlier version of Platform SDK header files, such as may have been found on Windows 95 or Windows NT 4.0.

Standard C++ Library Changes

The Standard C++ Library encompasses the latest ANSI C++ extensions, including the Standard Template Library and a new iostream library. The Standard C++ Library provides new functionality, such as numerous algorithms that manipulate C++ objects, and a migration path for developers who want to move to the standard iostream.

Starting in Visual Studio .NET 2003, Visual C++ no longer ships the old iostream libraries.

The Standard C++ Library is a set of header files. A list of the header files can be found in the Standard C++ Library Overview. The new header files do not have the .h extension.

 

https://msdn.microsoft.com/en-us/library/8h8eh904(VS.80).aspx

 

Differences in iostream Implementation

The main difference between the Standard C++ Library and previous run-time libraries is in the iostream library. Details of the iostream implementation have changed, and it may be necessary to rewrite parts of your code that use iostream if you want to link with the Standard C++ Library.

You will have to remove any old iostream headers (fstream.h, iomanip.h, ios.h, iostream.h, istream.h, ostream.h, streamb.h, and strstrea.h) you have included in your code and add one or more of the new Standard C++ iostream headers (<fstream>, <iomanip>, <ios>, <iosfwd>, <iostream>, <istream>, <ostream>, <sstream>, <streambuf>, and <strstream>, all without the .h extension).

Some of the differences

The following list describes behavior in the new Standard C++ iostream library that differs from behavior in the old iostream library.

In the new Standard C++ iostream library:

open functions do not take a third parameter (the protection parameter).

You cannot create streams from file handles.

Earlier Projects Built with No Default Libraries

You can build a project without default libraries by selecting /NODEFAULTLIB. If your previous project was built with no default libraries and you want to make iostream calls, you must name one of the new Standard C++ run-time libraries (Libcp.lib, Libcpmt.lib, Msvcprt.lib, and so on) or one of the old iostream run-time libraries (Libci.lib, Libcimt.lib, Msvcirt.lib, and so on) in order to link with the proper library.

In previous Visual C++ versions (4.1 and earlier), the run-time library names were Libc.lib, Libcmt.lib, and Msvcrt.lib. These libraries included the old iostream library. The old iostream library has now been removed from these libraries. If you do not choose to ignore default libraries and you include old iostream header files in your code, the old iostream run-time libraries (Libci.lib, Libcimt.lib, Msvcirt.lib, and so on) are linked by default. However, if you have chosen to ignore default libraries and have manually added one of the early run-time libraries, your iostream calls will now break.

 

C++ Exception Handling Must Be Enabled for the Standard C++ Library

Any file that uses the Standard C++ Library must be compiled with C++ exception handling enabled. For more information, see /GX.

ATL and MFC Version Numbers

ATL version  

Visual C++ version  

1.0, 1.1, 2.0 

None. Released to Web in Visual C++ 4.x time frame. 

3.0 

Visual C++ 6.0 

7.0 

Visual C++ .NET 2002 

7.1 

Visual C++ .NET 2003 

8.0 

Visual C++ 2005  

The versions of MFC shipped with Visual C++ as listed in the following table.

MFC version  

Visual C++ version  

1.0 

Microsoft C/C++ 7.0 

2.0 

Visual C++ 1.0 

2.5 

Visual C++ 1.5 

3.0 

Visual C++ 2.0 

3.1 

Visual C++ 2.1 

3.2 

Visual C++ 2.2 

4.0 

Visual C++ 4.0 

4.1 

Visual C++ 4.1 

4.2 

Visual C++ 4.2 

4.21 (mfc42.dll) 

Visual C++ 5.0 

6.0 (mfc42.dll) 

Visual C++ 6.0 

7.0 (mfc70.dll) 

Visual C++ .NET 2002 

7.1 (mfc71.dll) 

Visual C++ .NET 2003

8.0 (mfc80.dll) 

Visual C++ 2005  

Breaking Changes in the Visual C++ 2005 Compiler

To create a pointer to a member function, the address of operator (&) must be used with the fully qualified name of the method.

Class must be accessible to a friend declaration. Visual C++ compilers prior to Visual C++ 2005 allowed a friend declaration to a class that was not accessible in the scope of the class containing the declaration. Now, the compiler will give Compiler Error C2248.

For more…

https://msdn.microsoft.com/en-us/library/ms177253(VS.80).aspx

ATL and MFC Changes 

Link to read about

https://msdn.microsoft.com/en-us/library/y4wwyyk7(VS.80).aspx

Covers

ATL and MFC Version Numbers

ATL 7.1 and MFC 7.1

ATL 7.0 and MFC 7.0

Migrating From Older Versions of Visual Studio

If you do decide to upgrade these older applications to Visual Studio 2003/2005, I would strongly recommend going to 2005 or 2008.

By going to the latest version of Visual Studio, you are that much farther away from going out of the mainstream support cycle for Visual Studio.

For this .NET development, you would get the improvements and feature additions that were made to .NET in version 2.0 that comes with 2005 and .NET 3.5 from 2008.

Finally, it is recommended to use VS Team Suite or Developer editions of Visual Studio 2005.

It comes with really nice tools for profiling code performance and coverage and other features that do not come in the standard edition.

You will want to test each application that is migrated to VS 2005/2008 and there may be some compiler errors (places where the compiler was not following the correct standards in older version of VC++).

Compiler Changes

Breaking Changes

https://msdn.microsoft.com/en-us/library/bb531344.aspx

The Windows 95, Windows 98, Windows ME, and Windows NT platforms are no longer supported. These operating systems have been removed from the list of targeted platforms.

The compiler no longer supports multiple attributes that were directly associated with ATL Server. The following attributes are no longer supported:

perf_counter

perf_object

perfmon

request_handler

soap_handler

soap_header

soap_method

tag_name

FAQ for Development on 64-bit Windows    

This section contains frequently asked questions, links to Knowledge Base articles, and links to reference documentation to help you develop applications for 64-bit editions of Windows. If you have questions about pointer sizes and alignment, memory alignment, security issues, drivers, install and backup, application performance, or porting your 32-bit applications to 64-bits, then this section is for you.

Link

https://msdn.microsoft.com/en-us/isv/bb190527.aspx

Questions Answered

Alignment/Memory Questions

What tools are available to determine the alignment size for a data type?

What tools are available to determine the alignment size for a data type?

How is a data type's alignment size calculated?

How is the alignment size of a compound data type calculated?

Why is memory alignment an issue in 64-bit?

What are my options to handle unaligned access to memory and prevent stability and performance issues?

Why does the alignment policy I've defined appear not to be making an impact?

How do I fix data misalignment problems?

What can I do to correct data alignment and memory issues when porting a 32-bit application to run on 64-bit?

What are the incorrect assumptions made about memory allocation?

What are the incorrect assumptions made about Compound Type member allocation?

How can I optimize my 64-bit C/C++ application?

How do I ensure a pointer is addressing the memory allocation needed without committing reserved memory?

Will I need special hardware for memory top-down testing?

What are Polymorphic Data Types?

What integer data types are available to help port 32-bit applications to 64-bit?

How do I investigate and resolve pointer truncation problems?

What can I do to correct Pointer Truncation and Corruption issues when porting a 32-bit application to 64-bit?

How can I verify if changes I made fixed the pointer truncation errors?

Will using the new data types break existing code?

Security Questions

Why am I prompted to restart my computer after a security update is installed?

Why are some Web-based applications not working correctly after applying security update MS05-001?

How do I decommission the Microsoft enterprise certification authority CA and remove all related objects from the Active Directory service?

Networking

Why am I getting an "Access Denied" error message when trying to access remote resources?

Why do I receive the error message "The system cannot change your password because the domain 'MIT Realm' is not available" when trying to change my password?

Where can I learn more about Remote Desktop Protocol settings in Windows Server 2003 and in Windows XP?

Why might some Active Directory services, such as Lightweight Directory Access Protocol (LDAP) services, not function correctly?

Why do I loose my ATM network connection and am not able to re-establish a connection?

When using Asynchronous Transfer Mode (ATM) for network connections, you might lose your connections, and not be able to re-establish them.

Device Drivers

Where can I learn more about hardware and drivers for the 64-bit platform?

What drivers come with Windows XP x64 and where do I go to get other drivers?

Are ODBC or OLE DB supported in 64-bit Windows?

Why do drivers that install on x64 (i.e., AMD and Intel EM64T) need to be decorated to work?

Where can I learn about decorating 64-bit INF drivers?

Network

Why can't I install my network adapter after upgrading to Windows XP Professional 64-bit Edition?

Why don't the printer drivers automatically update when installed in a clustered environment?

Why can't I get the watermark, print multiple pages of a document on a single piece of paper, or collate features to work when printing a document?

Storage Management

After using F6 to successfully install storage drivers, why do I receive a bug check 7B on restart?

Performance

Why is the "Hibernate" tab not available in the Power Options Properties?

Why am I seeing high CPU usage when running the Spooler service?

Why does my computer with a 64-bit edition of Windows take a significant amount of time to boot?

What kind of performance can I expect when running 32-bit applications on 64-bit editions of Windows?

What kind of performance improvements can I expect when running 64-bit applications on a 64-bit edition of Windows over running 32-bit applications on a 32-bit operating system?

 

32-bit Applications

What do I need to do differently to get my 32-bit application to install correctly on 64-bit Windows?

How does File System Redirection work when loading libraries?

What happens if my application uses the %Program Files% environment variable on 64-bit Windows?

If I'm writing a 64-bit application how do I get the name of the 32-bit system directory?

What happens if my 32-bit application writes REG_EXPAND_SZ keys containing %ProgramFiles% to the registry, do I need to manually replace this?

How can I run 32-bit applications in a separate process space than a 64-bit application or migrate a component to 64-bit that depends on 32-bit DLLs or is served by a 32-bit DLL?

What are some obstacles to running a 32-bit application in 64-bit mode?

WoW64 (WoW64 will allow to execute 32-bit applications on 64-bit OS)

How does WoW64 work?

What are the components of WoW64 that are used to make 32-bit DLLs work in 64-bit Windows?

How do I get my 32-bit application to use the file systems folders reserved for 64-bit applications?

How do I make my 32-bit application aware of itself running on a 64-bit architecture?

Will WoW64 redirect system calls to C:\Program Files (x86)?

What should I do if my application uses data files and configurations that are shared between 32-bit and 64-bit applications?

64-Bit Compilation Settings To Specify

Set the 64-Bit Build Environment Variables

Start Visual C++ .NET with a 64-Bit Build Environment

Add a 64-Bit Debug Configuration

Modify Compiler or Linker Options

 

Description of the changes in MFC between Visual C++ 5.0 and Visual C++ 6.0

Link

https://support.microsoft.com/kb/194298

There are many new classes and member functions in the libraries that provide for integrated access to new operating system features.

Libarary Changes

CArchive Changes

Collection Classes

ASyncSocket

CCheckListBox

…and many others.

Visual C++ Versions

Visual C++ 2005 (known also as Visual C++ 8.0), which included MFC 8.0, was released in November 2005. This version supports .NET 2.0 and dropped Managed C++ for C++/CLI. It also introduced OpenMP. With Visual C++ 2005, Microsoft also introduced Team Foundation Server. Visual C++ 8.0 has problems compiling MFC AppWizard projects that were created using Visual Studio 6.0, so maintenance of legacy projects can be continued with the original IDE if rewriting was not feasible.

Visual C++ 2008 (known also as Visual C++ 9.0) was released in November 2007. This version supports .NET 3.5, and it is currently the latest stable release. By default, all applications compiled against the Visual C++ 2008 Runtimes (static and dynamic linking) will only work under Windows 2000 and later.

Visual C++ 2010 (known also as Visual C++ 10.0) will be released in the 2009-2010 timeframe. It is currently in development, and available only in the form of a Community Technology Preview. This version adds a modern C++ parallel computing library called the Parallel Patterns Library, partial support for C++0x, and significantly improved IntelliSense. This version is built around .NET 4.0, but still supports native development.

Getting Ready for 64-bit Windows

https://msdn.microsoft.com/en-us/library/aa384198(VS.85).aspx

Migration Tips

https://msdn.microsoft.com/en-us/library/aa384214(VS.85).aspx

The two primary areas of concern when examining your code for 64-bit compatibility are as follows:

Address calculations

Pointer arithmetic

For many reasons, developers have stored addresses as a ULONG value. After all, on 32-bit Windows, an address, a pointer, and a ULONG value are all 32 bits long. However, on 64-bit Windows, an address and a ULONG are not the same length. While a ULONG remains a 32-bit value, all pointers are now 64-bit values.

General Porting Guidelines

Storing a 64-bit Value

Common Compiler Errors

Additional Considerations

Porting and Optimizing Applications on 64-bit Windowsâ for AMD64 Architecture

Please see https://download.microsoft.com/download/5/b/5/5b5bec17-ea71-4653-9539-204a672f11cf/AMD64_PortApp.doc