📄 faq65.htm
字号:
<HTML>
<HEAD>
<TITLE>Determine which version of Windows the program is running on.</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Determine which version of Windows the program is running on.
</H3>
<P>
Use the <TT>GetVersionEx</TT> API function. <TT>GetVersionEx</TT> returns the OS version and a platform identifier that
indicates whether the OS is Windows 95/98 or Windows NT. Here is a code example that extracts version information
and dumps it into a <TT>TMemo</TT> control. The first three lines do most of the work. Figure 1 shows the output
from this code.</P>
<pre>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button1Click<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
<font color="navy">// GetVersionEx takes a pointer to an OSVERSIONINFO struct as</font>
<font color="navy">// its only argument. It will fill in the struct with version</font>
<font color="navy">// info. The struct has a size member that must be initialized</font>
<font color="navy">// to the size of the structure.</font>
OSVERSIONINFO info<b>;</b>
info<b>.</b>dwOSVersionInfoSize <b>=</b> <b>sizeof</b><b>(</b>OSVERSIONINFO<b>)</b><b>;</b>
GetVersionEx<b>(</b><b>&</b>info<b>)</b><b>;</b>
<font color="navy">// add version info to a TMemo control</font>
Memo1<b>-></b>Lines<b>-></b>Clear<b>(</b><b>)</b><b>;</b>
Memo1<b>-></b>Lines<b>-></b>Add<b>(</b><font color="blue">"Major Version:"</font> <b>+</b> IntToStr<b>(</b>info<b>.</b>dwMajorVersion<b>)</b><b>)</b><b>;</b>
Memo1<b>-></b>Lines<b>-></b>Add<b>(</b><font color="blue">"Minor Version:"</font> <b>+</b> IntToStr<b>(</b>info<b>.</b>dwMinorVersion<b>)</b><b>)</b><b>;</b>
Memo1<b>-></b>Lines<b>-></b>Add<b>(</b><font color="blue">"Build Number :"</font> <b>+</b> IntToStr<b>(</b>info<b>.</b>dwBuildNumber<b>)</b><b>)</b><b>;</b>
Memo1<b>-></b>Lines<b>-></b>Add<b>(</b><font color="blue">"Platform ID :"</font> <b>+</b> IntToStr<b>(</b>info<b>.</b>dwPlatformId<b>)</b><b>)</b><b>;</b>
Memo1<b>-></b>Lines<b>-></b>Add<b>(</b><font color="blue">"CSDVersion :"</font> <b>+</b> AnsiString<b>(</b>info<b>.</b>szCSDVersion<b>)</b><b>)</b><b>;</b>
Memo1<b>-></b>Lines<b>-></b>Add<b>(</b><font color="blue">""</font><b>)</b><b>;</b>
<font color="navy">// if the major version is less than 4, then you know that the OS</font>
<font color="navy">// is windows NT 3.X, since BCB can't create 16 bit programs</font>
<b>if</b><b>(</b>info<b>.</b>dwMajorVersion < <font color="blue">4</font><b>)</b>
Memo1<b>-></b>Lines<b>-></b>Add<b>(</b><font color="blue">"Windows NT 3.X detected"</font><b>)</b><b>;</b>
<b>switch</b> <b>(</b>info<b>.</b>dwPlatformId<b>)</b>
<b>{</b>
<b>case</b> VER_PLATFORM_WIN32s<b>:</b>
Memo1<b>-></b>Lines<b>-></b>Add<b>(</b><font color="blue">"Win32s detected"</font><b>)</b><b>;</b>
<b>break</b><b>;</b>
<b>case</b> VER_PLATFORM_WIN32_WINDOWS<b>:</b>
Memo1<b>-></b>Lines<b>-></b>Add<b>(</b><font color="blue">"Win95 or Win 98 detected"</font><b>)</b><b>;</b>
<b>break</b><b>;</b>
<b>case</b> VER_PLATFORM_WIN32_NT<b>:</b>
Memo1<b>-></b>Lines<b>-></b>Add<b>(</b><font color="blue">"Windows NT detected"</font><b>)</b><b>;</b>
<b>break</b><b>;</b>
<b>}</b>
<b>}</b>
</pre>
<BR>
<BR>
<IMG SRC="images/osversion.gif" ALT="OS Version" BORDER=0 HSPACE="0" ALIGN="CENTER" VALIGN="TOP">
<P><B>Figure 1: GetVersionEx Example.</B>
<BR>
<P>
<B>Note:</B> On Windows 95/98, <TT>GetVersionEx</TT> fills in the <TT>dwBuildNumber</TT> of the <TT>OSVERSIONINFO</TT>
with both the build number and the major and minor version numbers. The build number resides in the low word, and the
major and minor version numbers reside in the high word. Since the structure already contains separate members for the
major an minor versions, you can mask off the high word of <TT>dwBuildNumber</TT> to obtain the correct value
for the build number.
</P>
<pre>
OSVERSIONINFO info<b>;</b>
info<b>.</b>dwOSVersionInfoSize <b>=</b> <b>sizeof</b><b>(</b>OSVERSIONINFO<b>)</b><b>;</b>
GetVersionEx<b>(</b><b>&</b>info<b>)</b><b>;</b>
<font color="navy">// If Windows 95/98 is detected, mask off the junk in the build number</font>
<b>if</b> <b>(</b>info<b>.</b>dwPlatformId <b>==</b> VER_PLATFORM_WIN32_WINDOWS<b>)</b>
info<b>.</b>dwBuildNumber <b>&=</b> <font color="blue">0x0000FFFF</font><b>;</b>
</pre>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -