GetConsoleAliases Unable to get complete data #478

Closed
opened 2026-01-30 21:53:17 +00:00 by claunia · 2 comments
Owner

Originally created by @gucong3000 on GitHub (Dec 11, 2018).

This bug-tracker is monitored by Windows Console development team and other technical types. We like detail!

If you have a feature request, please post to the UserVoice.

Important: When reporting BSODs or security issues, DO NOT attach memory dumps, logs, or traces to Github issues. Instead, send dumps/traces to secure@microsoft.com, referencing this GitHub issue.

Please use this form and describe your issue, concisely but precisely, with as much detail as possible

  • Your Windows build number: (Type ver at a Windows Command Prompt)

Microsoft Windows [Version 10.0.17134.345]

  • What you're doing and what's happening: (Copy & paste specific commands and their output, or include screen shots)

I tried using the Windows API by Node.js C++ addon to get aliases

#include <node.h>
#include <windows.h>
#include <iostream>

namespace alias {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Boolean;
using v8::Value;
using namespace std;

void GetAliases(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();
  char* exeName = "cmd.exe";
  DWORD aliasesLength = GetConsoleAliasesLength(exeName);
  LPSTR aliases;

  // https://docs.microsoft.com/windows/console/getconsolealiases
  GetConsoleAliases(
    aliases,
    // https://docs.microsoft.com/zh-cn/windows/console/getconsolealiaseslength
    aliasesLength,
    exeName
  );
  cout << "string: " << aliases << endl;
  cout << "GetConsoleAliasesLength(): " << aliasesLength << endl;
  cout << "strlen(): " << strlen(aliases) << endl;

  args.GetReturnValue().Set(String::NewFromUtf8(isolate, aliases));
}

void Init(Local<Object> exports) {
  NODE_SET_METHOD(exports, "getAliases", GetAliases);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Init)

}  // namespace alias

  • What's wrong / what should be happening instead:

Console output:

string: cd=cd /d $* && if exist bin\git-bash-shell.cmd call bin\git-bash-shell.cmd
GetConsoleAliasesLength(): 857
strlen(): 75

I always only get the first alias, and all the other is lost.

The format of the data is as follows: Source1=Target1\0Source2=Target2\0... SourceN=TargetN\0, where N is the number of console aliases defined.

How do I handle \0 in string?

Originally created by @gucong3000 on GitHub (Dec 11, 2018). This bug-tracker is monitored by Windows Console development team and other technical types. **We like detail!** If you have a feature request, please post to [the UserVoice](https://wpdev.uservoice.com/forums/266908). > **Important: When reporting BSODs or security issues, DO NOT attach memory dumps, logs, or traces to Github issues**. Instead, send dumps/traces to secure@microsoft.com, referencing this GitHub issue. Please use this form and describe your issue, concisely but precisely, with as much detail as possible * Your Windows build number: (Type `ver` at a Windows Command Prompt) Microsoft Windows [Version 10.0.17134.345] * What you're doing and what's happening: (Copy & paste specific commands and their output, or include screen shots) I tried using the Windows API by Node.js C++ addon to get aliases ```C++ #include <node.h> #include <windows.h> #include <iostream> namespace alias { using v8::FunctionCallbackInfo; using v8::Isolate; using v8::Local; using v8::Object; using v8::String; using v8::Boolean; using v8::Value; using namespace std; void GetAliases(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); char* exeName = "cmd.exe"; DWORD aliasesLength = GetConsoleAliasesLength(exeName); LPSTR aliases; // https://docs.microsoft.com/windows/console/getconsolealiases GetConsoleAliases( aliases, // https://docs.microsoft.com/zh-cn/windows/console/getconsolealiaseslength aliasesLength, exeName ); cout << "string: " << aliases << endl; cout << "GetConsoleAliasesLength(): " << aliasesLength << endl; cout << "strlen(): " << strlen(aliases) << endl; args.GetReturnValue().Set(String::NewFromUtf8(isolate, aliases)); } void Init(Local<Object> exports) { NODE_SET_METHOD(exports, "getAliases", GetAliases); } NODE_MODULE(NODE_GYP_MODULE_NAME, Init) } // namespace alias ``` * What's wrong / what should be happening instead: Console output: ```bash string: cd=cd /d $* && if exist bin\git-bash-shell.cmd call bin\git-bash-shell.cmd GetConsoleAliasesLength(): 857 strlen(): 75 ``` I always only get the first alias, and all the other is lost. > The format of the data is as follows: Source1=Target1\0Source2=Target2\0... SourceN=TargetN\0, where N is the number of console aliases defined. How do I handle `\0` in string?
claunia added the Issue-Question label 2026-01-30 21:53:17 +00:00
Author
Owner

@bitcrazed commented on GitHub (Dec 11, 2018):

You have to iterate the returned aliases char buffer for aliasesLength chars, splitting into one or more strings if you find a \0 before the end of the buffer.

If you simply try to print the returned string, the printing function will assume that the first \0 is a string terminator so you won't see aliases 2 ... n.

@bitcrazed commented on GitHub (Dec 11, 2018): You have to iterate the returned `aliases` char buffer for `aliasesLength` chars, splitting into one or more strings if you find a `\0` before the end of the buffer. If you simply try to print the returned string, the printing function will assume that the first \0 is a string terminator so you won't see aliases 2 ... n.
Author
Owner

@zadjii-msft commented on GitHub (Dec 11, 2018):

Also, you never initialize aliases. So GetConsoleAliases is just going to write aliasesLength bytes somewhere. Don't do that.

edit: I'm closing this because this is a discussion regarding how to use the console API, and not a bug/feature request upon the console.

@zadjii-msft commented on GitHub (Dec 11, 2018): Also, you never initialize `aliases`. So `GetConsoleAliases` is just going to write `aliasesLength` bytes *somewhere*. Don't do that. edit: I'm closing this because this is a discussion regarding how to use the console API, and not a bug/feature request upon the console.
Sign in to join this conversation.
No Label Issue-Question
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#478