确定当前的控制台代码页

时间:2018-01-12 08:58:58

标签: node.js windows

除了运行chcp并解析其自由文本本地化输出外,还有办法从Node脚本中以编程方式检测Windows控制台代码页吗?

C:\>chcp
Página de códigos activa: 850

1 个答案:

答案 0 :(得分:0)

简答:

但是,可以创建一个实现该功能的包,截至2018年4月,NPM注册表中有chcp

你现在可以停止阅读,除非你对内部感兴趣:)

该软件包包含一个native add-on和一个模块,用于导出要在JavaScript中使用的相应Win32 API方法。

配方创建自己的包:

  1. 从提升的提示符*:

    安装依赖项
    npm install --global --production windows-build-tools
    npm install --global node-gyp@latest
    

    (*)node-gyp@latest可能不需要提升提示,但我不是专家,这对我有用。

  2. 创建名为binding.gyp的文件:

    {
      "targets": [
        {
          "target_name": "addon",
          "sources": [ "chcp.cc" ]
        }
      ]
    }
    
  3. 创建构建文件:

    node-gyp configure
    
  4. 使用您的模块源代码创建一个文件,例如: chcp.cc

    #include <node.h>
    #include <windows.h>
    
    using namespace v8;
    
    void GET(const FunctionCallbackInfo<Value>& args) {
      Isolate* isolate = args.GetIsolate();
      Local<Number> chcp = Number::New(isolate, GetConsoleOutputCP());
      args.GetReturnValue().Set(chcp);
    }
    void init(Local<Object> exports) {
      NODE_SET_METHOD(exports, "get", GET);
    }
    NODE_MODULE(NODE_GYP_MODULE_NAME, init)
    

    注意:没有尾随;

  5. 编译:

    node-gyp build
    
  6. 最后,require()生成的*.node文件(不带扩展名,基本名称可以根据自己的喜好进行更改):

    const chcp = require('./chcp')
    console.log(chcp.get());
    
  7. .get()方法返回number(例如850)。

    此信息是通过support ticket收集的。