📄 faq28.htm
字号:
<HTML>
<HEAD>
<TITLE>Get the location of the Windows directory or the Windows system directory</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Get the location of the Windows directory or the Windows system directory
</H3>
<P>
Call the API <TT>GetWindowsDirectory</TT> function to get the windows
directory. Call <TT>GetSystemDirectory</TT> if you want to find the windows system
directory.
</P>
<pre>
<font color="navy">// Fetch the windows directory</font>
<b>char</b> WinDir<b>[</b>MAX_PATH <b>+</b><font color="blue">1</font><b>]</b><b>;</b>
WinDir<b>[</b>MAX_PATH<b>]</b> <b>=</b> <font color="blue">'\0'</font><b>;</b>
GetWindowsDirectory<b>(</b>WinDir<b>,</b>MAX_PATH<b>)</b><b>;</b>
<font color="navy">// fetch the system directory</font>
<b>char</b> SysDir<b>[</b>MAX_PATH <b>+</b><font color="blue">1</font><b>]</b><b>;</b>
SysDir<b>[</b>MAX_PATH<b>]</b> <b>=</b> <font color="blue">'\0'</font><b>;</b>
GetSystemDirectory<b>(</b>SysDir<b>,</b> MAX_PATH<b>)</b><b>;</b>
</pre>
<P>
<B>Note:</B> The windows directory is usually <TT>C:\WINDOWS</TT> or <TT>C:\WINNT</tt>.
The system directory is usually <TT>C:\WINDOWS\SYSTEM32</tt> or <TT>C:\WINNT\SYSTEM32</tt>.
</P>
<P>
<B>Note:</B> The second argument to both <tt>GetWindowsDirectory</tt> and
<TT>GetSystemDirectory</tt> specifies how many characters the OS can copy into
the string's buffer. If this argument is less than the number of characters
needed for the directory name, then both functions will return the size of the
buffer required to hold the entire string (including the null terminator).
No data is copied when the buffer size is too small.
</P>
<P>
Some people employ a technique that takes advantage of this fact. The technique
works like this. You call <tt>GetWindowsDirectory</tt> twice. The first time you
call it, you pass a <TT>NULL</TT> pointer, and 0 for the number of bytes that
can be written. The API will return how many bytes you need to allocate for the
string buffer. Using this information, you can call <TT>new</TT> to create a
string that is exactly the correct size. Here is how it works:
</P>
<pre>
UINT len <b>=</b> GetWindowsDirectory<b>(</b>NULL<b>,</b> <font color="blue">0</font><b>)</b><b>;</b> <font color="navy">// fetch size</font>
<b>char</b> <b>*</b>buf <b>=</b> <b>new</b> <b>char</b><b>[</b>len<b>]</b><b>;</b> <font color="navy">// allocate buffer</font>
GetWindowsDirectory<b>(</b>buf<b>,</b> len<b>)</b><b>;</b> <font color="navy">// call API again</font>
<b>...</b>
<b>delete</b> <b>[</b><b>]</b>buf<b>;</b> <font color="navy">// cleanup</font>
</pre>
<P>
Usually, it is easier to just use an array with a size of <TT>MAX_PATH</TT>.
However, this technique of calling an API function twice, once to calculate a
buffer size, and once to actually fetch the string, appears in other
situations where a constant such as MAX_PATH is not available.
</P>
<P>
<B>Note:</B> The documentation states that the buffer passed to
<tt>GetWindowsDirectory</tt> and <TT>GetSystemDirectory</tt> should be at least
<TT>MAX_PATH</TT> bytes in length to guarantee the that string will fit. Now does the
documentation mean that the buffer should be able to hold a string with
a length of <tt>MAX_PATH</tt> characters? If so, then the buffer should be
<tt>MAX_PATH+1</TT> bytes long, to accomodate the null terminator. Or does the
documentation mean that the buffer should be <TT>MAX_PATH</TT> bytes long,
because the largest possible string returned will be <TT>MAX_PATH-1</TT>
characters long?
</P>
<P>
I don't know the correct answer, so I usually error on the side of caution.
I make the buffer <tt>MAX_PATH+1</tt> bytes long, in case the directory has a
string length of <TT>MAX_PATH</TT>. <tt>MAX_PATH</tt> is defined as 260 in
<TT>windefs.h</TT>.
</P>
<P>
<B>Note:</B> The string returned by <tt>GetWindowsDirectory</tt> does not contain
a trailing slash. The return string will be <TT>C:\WINDOWS</TT> and not <TT>C:\WINDOWS\</TT>.
<TT>GetSystemDirectory</tt> works the same way.
</P>
<P>
Be aware that not all of the directory based API fucntions work the same way. For
example, <TT>GetTempPath</TT> does include a trailing slash.
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -