cmd.exe 是用什么语言写的?

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.

来源:Wikipedia

这个问题我到处找都找不到答案。

每个网站都关注 batch language is used in cmd.exe 但我找不到它所用的语言。


所以我的问题是:

在 Windows 中用于编写命令提示符或 cmd.exe 的语言是什么?

该文件在文本编辑器中打开时包含路径 onecore\base\cmd\maxpathawarestring.cpp,这表明至少有一个源文件是用 C++ 编写的。

来自 的附录:

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.

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.

所以我会混合使用 C 和 C++。

正如 MS 的 Rich Turner 官方确认最初它是用 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/

但部分内容已迁移到 C++ 中

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 ~/MB (yes … 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

如果您查看 latest Windows Console’s internals structure you can see that it uses Map, Collection which suggests that it likely uses some C++/CX

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

如果您有兴趣,那么 conhost.exe 也已开源,采用新的 Windows 终端

的形式

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

您可以在Windows command line blog

中找到更多详细信息的不错的文章