This article is compilation of all "How To Determine" list. For example "How to find the computer name", "how to find number of fonts installed on system". As you know the list is endless, and this would be regularly updated, updated on reader's demands and as new technology evolve.

Every effort it taken to use both C++and C#. C++ code will be on top of native Windows API, and optionally using MFC. C# code will be on top of .NET framework, and if not available using DllImports of native Windows API. Content and examples on .NET may get delayed, but they would be added on regular basic. For all "how-to-retrieve" operation, there would be C++. For C++, wherever applicable, Unicode strings and functions are used.

Following is list of all "how-to" that is explained in this article. Linked or Bold content is mentioned here, and other how-to is scheduled for discussion. As the article would grow up, I would logically structure it.

C++

There are multiple ways to retrieve the date/time. It depends if you need System Time (UTC), or the local time; the precision level of time and the preference with the time-retrieval function.

To retrieve Local Time, use can use GetLocalTime API, which would return time adjusted as per your location. This is the date/time you see everywhere and should display to end user. It retrieves local-time in SYSTEMTIME structure:

You may retrieve non-adjusted System Time using GetSystemTime API. A UTC time may be useful for time-stamping in file or database, or to let Time travel across globe with consistency. It also takes SYSTEMTIME structure.

My friendly MFC programmers can use static method of CTime, named GetCurrentTime, which returns a CTime object. This class provides many Get functions to retrieve different components of date/time. The method Format, which formats the time as per the format string given (same as strftime), can be used to generate user friendly time.

Though not the time par se, GetTickCount[64] can be used to retrieve number of milliseconds elapsed since system was started. The old version, GetTickCount, can work flawlessly up to 49.7 days. For Windows Vista and higher OS, you should use GetTickCount64.

The C Runtime Library lovers can use _ftime and _ftime_s family of functions to retrieve current System Time; and localtime and localtime_s family of functions to retrieve Local Time. The time family of functions returns number of seconds elapsed* since 1-Jan-1970 midnight. All these functions internally use GetSystemTimeAsFileTime.

Though not mentioned in MSDN, all other Windows API functions also use GetSystemTimeAsFileTime to retrieve current system time. GetSystemTimeAsFileTime calls the not exposed (via header/library) function NtQuerySystemTime. The function GetSystemTimeAsFileTime returns the most precise time among all other functions. It presumably returns timing in 100-nanoseconds precision. The precision depends on the hardware, the Operating System and the timing resolution set by calling process (use undocumented NtSetTimerResolution to set timer/timing resolution).

To retrieve only the date, you can use static property Today, which keeps the time component to be 00:00:00. The date component is retrieve out of Local Time, and not of System Time.

Just for completeness, the public property Kind (of type DateTimeKind) can be used to know if time represented by DateTime object is local, system or undefined type. Thus, when you retrieve local date/time using Now or Today, it would be DateTimeKind.Local; and when you retrieve it using UtcNow, it would be DateTimeKind.Utc. This kindness is available in .NET Framework 2.0 and above.

The Ticks property returns number of 100-nanoseconds for this instance. A Tick is actually 0.1 microsecond, or 1/10000 (0.0001) milliseconds. Ticks property is 64-bit integer. The following code calculates the microseconds out of 100-nanoseconds, and displays.

C++

Technically it's not the username who is logged in, but the process or thread which is running under a user. A user or programmer may launch a process into another logon-credential using "RunAs" verb, or spawn a process using CreateProcessAsUser function. A running thread might be impersonated using ImpersonateLoggedOnUser API, or other impersonating functions. Well, this section is not about these tricks, but to retrieve the username under which current thread is running (or impersonating).

To retrieve fullname of user, you need to use GetUserNameEx function. This function is declared in header Security.h (actually in secext.h), thus you must #include<Security.h>. Including this header requires either of these to be defined:

SECURITY_WIN32

SECURITY_KERNEL

SECURITY_MAC

Since our application is Windows' User-mode application, use just define the first one. Further, we need to include the library Secur32.lib in Linker settings. Anyway, here is code:

Yes, GetUserNameEx takes one extra parameter, which is of EXTENDED_NAME_FORMAT enum type. The symbol NameSamCompatible is one of them enum of this type. On non-domain connected computers, only this enum is supported. Please explore other members of EXTENDED_NAME_FORMAT yourself, with which you can retrieve full name of user, the display name, the GUID of user; to name a few. The following is output of above' code:

Username: HERCULES\Ajay

Following is just for relevant discussion. As you know a process may be running in another Logon Session (i.e. sharing the Desktop/Console of other user), and the methods described above would just locate the username and not any information about the session under which process/thread is running. Whilst, I would elaborate about this sooner or later, the function you can use is: WTSGetActiveConsoleSessionId.

To get computer name in different formats you can use GetComputerNameEx function. Since my personal machine is not connected to any domain, it would always show the same name, or would return empty name when I call GetComputerNameEx with different format specifiers. Anyway, here is sample code.

C++

The function GetSystemInfo can be used to retrieve number of logical processors installed on system, including other processor related information. The GetSystemInfo takes SYSTEM_INFO and fills it. Following code displays number of processors installed on system.

GetLogicalDrives returns a 32-bit (DWORD) value, which is just is just mask of drives present on system. For example, if a system as A:, C: and D: drives, the return values would be 13, which is, in binary 1101. The last bit digit in binary represents that system as A: drive, and so on. Following is simplified approach to get and display drive letters.

This may not be intuitive for most readers. The another function GetLogicalDriveStrings, generates set of nullterminated strings, with two null terminated characters at end. As you can guess this misfortune, this function is also not straightforward. Here is an example that displays drives.

Okay, now let determine the type of drive (not the file-system). The type could be hard-disk drive, CDROM drive, Network drive, USB drive etc. First, I would explore simple function named GetDriveType, which returns one of the seven values (see the link mentioned). The following example shows how to process the drive type (only two types are processed).

This function cannot determine extended information, like if drive is USB drive. For this we need to use SetupDiGetDeviceRegistryProperty function, which is declared in setupapi.h, exported via library setupapi.lib and resides in setupapi.dll. It is available on Windows 2000 and later systems. I am taking your implicit permission to delay writing about it, since it requires some effort on the Setup API ground.

Meanwhile, we proceed and now let's find out about capacity of drives. For this, we can use GetDiskFreeSpace or GetDiskFreeSpaceEx function. Former does not support reporting information above 2GB, thus should not be used. Following example display the sizes of all enumerated drives.

Finally let's retrieve File-System, VolumeLabel, Serial Number, Max File Length supported, and other Flags. For all this, you just need to use one function, GetVolumeInformation which takes eight parameters. The following code would help you to understand this function.

I have noticed that for CD/DVD drive, where no disc is inserted, this function fails. It cannot retrieve the information, since it is about drive/volume not about disk. It would present output in following form:

C++

To retrieve information about memory (RAM), you can either use GlobalMemoryStatus or GlobalMemoryStatusEx. They take MEMORYSTATUS and MEMORYSTATUSEX structures, respectively. The former has limitations and cannot return correct information on systems having more than 4GB, thus it should not be used. The following example shows how to get memory information, and displays it.

You should explore other members of this structure. One member of this structure is worth mentioning: dwMemoryLoad (DWORD), which returns percent of physical memory occupied. Thus, you need not to calculate the memory-load! For the above run, it was 57.

C++

First thing I should address - the "get monitor resolution" could mean multiple things depending on your need and perspective. These perspectives could be:

You need exact current resolution of monitor.

You need maximum client-area size for your application, that would exclude the taskbar.

You may need resolutions of multiple attached monitors/displays (if any).

Let's start with the first one - retrieve the actual current resolution of monitor. For this you need to use GetSystemMetrics function, passing SM_CXSCREEN and SM_CYSCREEN parameters, to get Width and Height of monitor's resolution.

You can retrieve the same information using GetDeviceCaps function, passing HORZRES and VERTRES arguments. This function requires you to pass a Device Context (HDC). This article won't describe about that, but I would just put the sample code to show how GetDeviceCaps can be used.

Now, let's determines full-screen client area size. For this you need to use GetSystemMetrics function, passing SM_CXFULLSCREEN and SM_CYFULLSCREEN parameters, to get Width and Height of workable area. This size excludes two things: the size (width) of Title bar of window, and size of taskbar. While most users have taskbar on bottom of screen, thus Height would be smaller than actual resolution of monitor - but, taskbar can be on any side of monitor, it can be any size or it doesn't occupy the desktop (hidden or disabled). The following code displays the possible client area for a full-screen window:

Okay, in the same perspective, you might want to know how to get dimensions of a full-screen application that would include the taskbar, and exclude the size of Title bar. Any media player with skin support may need this.

For this you need to use SystemParametersInfo function with SPI_GETWORKAREA action parameter. Since this function supports wide range of retrievals, with varying data-types, it takes LPVOID (void*) as argument. For our action parameter, we need to pass address of a RECT structure. Here is code:

It display following for my default resolution and normal size taskbar on bottom:

Possible work area: Top = 0, Left = 0, Right = 1920, Bottom = 1050

Well, you cannot ignore the top and left, since moving the taskbar on top would produce the following result (also, I doubled the task bar's size!):

Possible work area: Top = 62, Left = 0, Right = 1920, Bottom = 1080

Multiple monitors! To get information about monitors you need to use EnumDisplayMonitors, which would enumerate all monitors on system, via a callback. The callback would get HMONITOR, which can be passed to GetMonitorInfo function. GetMonitorInfo function would fill up MONITORINFO[EX], and that structure's member would have resolution of desire monitor! You see, this demands another section in this article, and I would soon put it!

C++

You can use GetVersion or GetVersionEx to retrieve the Windows Version. The former function returns a 32-bit value, whose lower 16-bit represents the major and minor version numbers. The higher order 16 bits represents the build number. Following example illustrates this.

GetVersion function has some concerns (not bugs), thus is is not suitable for recent applications. It cannot determine product information (Professional, Ultimate), service pack information etc, for example. It should not be used in new applications.

For new applications GetVersionEx function should be used. Exploring more about this function is beyond the scope of this article, only basic information is being provided. You should read this article on CodeGuru.com.

Just in brief; this function returns information in either OSVERSIONINFO or OSVERSIONINFOEX structure. No, this function is not overloaded - instead the dwOSVersionInfoSize member, which should be set to size of either of these structures, determines which type of information is requested.

Every effort is taken to make all internal and external links workable. For internal links, the CodeProject's editor has some bugs and it removes the link. For external links, the web page may open in same window, though I made it to open in another window/tab. Thus, if you note external link being opened in same window/tab, please let me know.

Comments and Discussions

Is it possible to retrieve ip, mac addr and other information about the devices connected to my home network. I am working on creating a dash board for my home with some realtime updates on my connected devices. And fetch information on my connected devices is my starting point.

Dear,
Thanks for your article. it is very interesting.
From your article, we could get File System name.
Then I have a question.
Is there any method to set the File System name? For example, assume that you have mounted a network share and so the file system could be NFS/NTFS/FAT. And I want to change NFS to MyFS for example.
Can you help me with this problem?
Looking forward your wonderful help
Thanks.
Steve.