Home Articles Books Downloads FAQs Tips

Q: Get the location of the Windows directory or the Windows system directory

Answer: Call the API GetWindowsDirectory function to get the windows directory. Call GetSystemDirectory if you want to find the windows system directory.

// Fetch the windows directory
char WinDir[MAX_PATH +1];
WinDir[MAX_PATH] = '\0';
GetWindowsDirectory(WinDir,MAX_PATH);

// fetch the system directory
char SysDir[MAX_PATH +1];
SysDir[MAX_PATH] = '\0';
GetSystemDirectory(SysDir, MAX_PATH);

Note: The windows directory is usually C:\WINDOWS or C:\WINNT. The system directory is usually C:\WINDOWS\SYSTEM32 or C:\WINNT\SYSTEM32.

Note: The second argument to both GetWindowsDirectory and GetSystemDirectory 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.

Some people employ a technique that takes advantage of this fact. The technique works like this. You call GetWindowsDirectory twice. The first time you call it, you pass a NULL 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 new to create a string that is exactly the correct size. Here is how it works:

UINT len = GetWindowsDirectory(NULL, 0);  // fetch size
char *buf = new char[len];                // allocate buffer
GetWindowsDirectory(buf, len);            // call API again
...
delete []buf;                             // cleanup

Usually, it is easier to just use an array with a size of MAX_PATH. 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.

Note: The documentation states that the buffer passed to GetWindowsDirectory and GetSystemDirectory should be at least MAX_PATH 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 MAX_PATH characters? If so, then the buffer should be MAX_PATH+1 bytes long, to accomodate the null terminator. Or does the documentation mean that the buffer should be MAX_PATH bytes long, because the largest possible string returned will be MAX_PATH-1 characters long?

I don't know the correct answer, so I usually error on the side of caution. I make the buffer MAX_PATH+1 bytes long, in case the directory has a string length of MAX_PATH. MAX_PATH is defined as 260 in windefs.h.

Note: The string returned by GetWindowsDirectory does not contain a trailing slash. The return string will be C:\WINDOWS and not C:\WINDOWS\. GetSystemDirectory works the same way.

Be aware that not all of the directory based API fucntions work the same way. For example, GetTempPath does include a trailing slash. See the FAQ on how to locate the temp directory for more info on GetTempPath.



Copyright © 1997-2000 by Harold Howe.
All rights reserved.