Nowadays, 64-bit systems are becoming more and more popular. For software developers, mastering 64-bit development technology will provide a better space for development. After several years of development, the 64-bit development tools are also mature, such as Visual Studio 2010.
How to configure the 64-bit platform is detailed on msdn --
Http://msdn.microsoft.com/zh-cn/library/9yb4317s.aspx
How to: Configure visual c ++ projects for 64-bit platforms
In many cases, we want to be compatible with the old system, so we have configured two compilation platforms-Win32 and x64.
However, this is not enough because the old system does not support some new features. Therefore, we need to dynamically and dynamically judge during runtime.
There are two most common judgments --
1. determine the number of digits of the program: the platform on which the program is compiled.
2. determine the number of operating systems: whether the current program runs on a 32-bit operating system or a 64-bit operating system.
For the first type-to determine the number of digits of a program, you can configure the preprocessing symbol to determine the number of digits during compilation. However, this method is a little cumbersome and is not suitable in some cases-for example, if I want to obtain the string of the number of digits of a program, I must use the "# If" statement to write multiple lines of code.
For the second type-determine the number of digits of the operating system, it is only determined at runtime.
The Code is as follows --
# Include <windows. h>
# Include <stdio. h>
# Include <tchar. h>
# Include <conio. h>
// Obtain the number of digits of the program (the number of digits of the Code Compiled)
Int getprogrambits ()
{
Return sizeof (int *) * 8;
}
// Securely obtain real system information
Void safegetnativesysteminfo (_ out lpsystem_info lpsysteminfo)
{
If (null = lpsysteminfo) return;
Typedef void (winapi * lpfn_getnativesysteminfo) (lpsystem_info lpsysteminfo );
Required fngetnativesysteminfo = (lpfn_getnativesysteminfo) getprocaddress (getmodulehandle (_ T ("Kernel32"), "getnativesysteminfo ");;
If (null! = Fngetnativesysteminfo)
{
Fngetnativesysteminfo (lpsysteminfo );
}
Else
{
Getsysteminfo (lpsysteminfo );
}
}
// Obtain the number of operating system digits
Int getsystembits ()
{
System_info Si;
Safegetnativesysteminfo (& Si );
If (Si. wprocessorarchitecture = processor_ubunture_amd64 |
Si. wprocessorarchitecture = processor_ubunture_ia64)
{
Return 64;
}
Return 32;
}
Int _ tmain (INT argc, _ tchar * argv [])
{
Const int nbitcode = getprogrambits ();
Const int nbitsys = getsystembits ();
_ Tprintf (_ T ("I Am a % dbit program, run on % dbit system."), nbitcode, nbitsys );
//
_ Getch ();
Return 0;
}
Running Effect on 32-bit WINXP --
Running Effect on 64-bit win7 --
Note:
1. getnativesysteminfo is a new API for Windows XP to obtain real system information. (When a 32-bit program runs on a 64-bit system, getsysteminfo returns the information modified by wow64)
2. iswow64process is used to determine whether a process runs under wow64. For 64-bit programs, the wow64process parameter returns false.
Download source code --
Http://files.cnblogs.com/zyl910/vcis64.rar