Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In which language is cmd.exe written?

Command Prompt, also known as cmd.exe or cmd (after its executable file name), is the command-line interpreter on Windows NT, Windows CE, OS/2 and eComStation operating systems. It is the counterpart of COMMAND.COM in DOS and Windows 9x systems (where it is also called "MS-DOS Prompt"), and analogous to the Unix shells used on Unix-like systems.

Source: Wikipedia

I have searched everywhere but could not get an answer for this question.

Each website focuses on the fact that batch language is used in cmd.exe but I could not find the language in which it is written.


So my question is:

What is the language that was used to write command-prompt or cmd.exe in Windows?

like image 253
Agile_Eagle Avatar asked Sep 02 '25 15:09

Agile_Eagle


2 Answers

The file, when opened in a text editor, contains the path onecore\base\cmd\maxpathawarestring.cpp, which would indicate that at least one source file is written in C++.

Addendums from Hans Passant:

The imports it depends on makes it is likely to be a mix of C and C++. CRT functions like longjmp, calloc, free indicate C code, might well be ancient and hark back to the command.com days. It clearly also uses C++ exception handling, C++ is their weapon of choice for all recent code development. Mixing is not uncommon.

And eryksun:

under a debugger it's obvious that recent additions to CMD have been written in C++. x cmd!*::* shows significant use of the C++ std namespace

[...]

But CMD is still mostly C, not C++. Its commands and support functions are implemented as C functions such as eExit, eChdir, ParseStatement, SearchForExecutable, and ExecPgm. They haven't ported all of this old C code to an OOP design.

So I'd go with a mix of C and C++.

like image 102
CodeCaster Avatar answered Sep 05 '25 08:09

CodeCaster


As officially confirmed by MS' Rich Turner, originally it's written in C

Cmd is a Win32 app written entirely in 'C' - this is important since one of the key goals of NT was to be portable across many different processor and machine architectures

https://devblogs.microsoft.com/commandline/rumors-of-cmds-death-have-been-greatly-exaggerated/

but parts of it were migrated to C++ as of now

Inside the Windows Console

Windows Console is a traditional Win32 executable and, though it was originally written in 'C', much of the code is being migrated to modern C++ as the team modernizes and modularizes Console's codebase.

For those who care about such things: Many have asked whether Windows is written in C or C++. The answer is that - despite NT's Object-Based design - like most OS', Windows is almost entirely written in 'C'. Why? C++ introduces a cost in terms of memory footprint, and code execution overhead. Even today, the hidden costs of code written in C++ can be surprising, but back in the late 1990's, when memory cost ~$60/MB (yes … $60 per MEGABYTE!), the hidden memory cost of vtables etc. was significant. In addition, the cost of virtual-method call indirection and object-dereferencing could result in very significant performance & scale penalties for C++ code at that time. While one still needs to be careful, the performance overhead of modern C++ on modern computers is much less of a concern, and is often an acceptable trade-off considering its security, readability, and maintainability benefits ... which is why we're steadily upgrading the Console’s code to modern C++.

Windows Command-Line: Inside the Windows Console

If you look into the latest Windows Console’s internals structure you can see that it uses Map, Collection which suggests that it likely uses some C++/CX

Windows Console Text Buffer Architecture

From the top (original buffer's blue boxes):

  • ScreenInfo – maintains information about the viewport, etc., and contains a TextBuffer
    • TextBuffer – represents the Console’s text area as a collection of rows
      • Row – uniquely represents each CharRow in the console and the formatting attributes applied to each row
        • CharRow – contains a collection of CharRowCells, and the logic and state to handle row wrapping & navigation
          • CharRowCell – contains the actual cell’s text, and a DbcsAttribute byte containing cell-specific flags

In case you're interested then conhost.exe has also been open sourced, under the form of the new Windows terminal

The Windows console host, conhost.exe, is Windows' original command-line user experience. It implements Windows' command-line infrastructure, and is responsible for hosting the Windows Console API, input engine, rendering engine, and user preferences. The console host code in this repository is the actual source from which the conhost.exe in Windows itself is built.

https://github.com/microsoft/terminal

You can find more pretty good articles with detailed information in the Windows command line blog

like image 35
phuclv Avatar answered Sep 05 '25 08:09

phuclv