Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用 electron13 + electron-react-boilerplate 一些包报错+native包问题 #61

Open
Topppy opened this issue Jan 11, 2022 · 6 comments
Milestone

Comments

@Topppy
Copy link
Owner

Topppy commented Jan 11, 2022

能run起来的最粗暴的方式就是
把nodeIntegration打开,把contextIsolation关掉。

const config: BrowserWindowConstructorOptions = {
    show: false,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      webSecurity: false,
      preload: preloadPath,
      enableRemoteModule: true,
    }
  }
@Topppy
Copy link
Owner Author

Topppy commented Jan 11, 2022

如果装native包,务必在 app/release/下安装,并且要使用 npm 而不是yarn.

electron-react-boilerplate 使用的rebuild版本有问题,直接升级到最新版本。

@Topppy
Copy link
Owner Author

Topppy commented Jan 11, 2022

未解决问题。 基于ffi-napi 多次调用win32的方法,QueryDisplayConfig,node进程会直接中断退出。

import log from 'electron-log'

const ffi = require('ffi-napi')
const ref = require('ref-napi') // 对接C++指针
const refStruct = require('ref-struct-napi') // 对接C++结构体
const refArray = require('ref-array-napi') // 对接C++数组

// 屏幕的四种模式

export const sdcApplay = 0x80 // 屏幕设置辅助参数

export enum DisplayMode {
  internal = 1, // 仅电脑屏
  clone = 2, // 复制
  extend = 4, // 扩展
  external = 8, // 仅第二屏幕
}

// 这两个结构体的内容太复杂,而且实际项目用不到,所以直接算一下结构体的大小填充个无用数据
const DISPLAYCONFIG_PATH_INFO = refStruct({
  unuse: refArray(ref.types.int, 72 / 4),
})
const DISPLAYCONFIG_MODE_INFO = refStruct({
  unuse: refArray(ref.types.int, 64 / 4),
})

const PathArrayType = refArray(ref.refType(DISPLAYCONFIG_PATH_INFO))
const ModeArrayType = refArray(ref.refType(DISPLAYCONFIG_MODE_INFO))

const libm = ffi.Library('user32', {
  SetDisplayConfig: ['long', ['int', 'pointer', 'int', 'pointer', 'int']],
  GetDisplayConfigBufferSizes: [
    'long',
    ['int', ref.refType(ref.types.int), ref.refType(ref.types.int)],
  ],
  QueryDisplayConfig: [
    'long',
    [
      'int',
      ref.refType(ref.types.int),
      PathArrayType,
      ref.refType(ref.types.int),
      ModeArrayType,
      ref.refType(ref.types.int),
    ],
  ],
})

export default {
  setDisplayMode: (mode: DisplayMode) => {
    log.info(
      '=====================================================================setDisplayMode',
      mode,
    )
    // eslint-disable-next-line no-bitwise
    const re = libm.SetDisplayConfig(0, null, 0, null, sdcApplay | mode)
    return re
  },
  getDisplayMode: () => {
    const requiredPaths = ref.alloc(ref.types.int)
    const requiredModes = ref.alloc(ref.types.int)
    // 获取QueryDisplayConfig的2-5参数,不用动
    const result2 = libm.GetDisplayConfigBufferSizes(
      2,
      requiredPaths,
      requiredModes,
    )
    log.info(result2, requiredPaths.deref(), requiredModes.deref())

    const PathArray = refArray(DISPLAYCONFIG_PATH_INFO, requiredPaths.deref())
    const ModeAyyay = refArray(DISPLAYCONFIG_MODE_INFO, requiredModes.deref())
    const paths = new PathArray()
    const modes = new ModeAyyay()

    // 查询结果存储在flags
    log.info('new flags')
    const flags1 = ref.alloc(ref.types.int)
    // 2到5参数没有用到,参数1是常量
    log.info('start get')
    libm.QueryDisplayConfig(
      4,
      requiredPaths,
      paths.ref(),
      requiredModes,
      modes.ref(),
      flags1,
    )
    // log.info('save re')
    // const re = flags1.deref()
    log.info(
      '=====================================================================getDisplayMode',
      flags1.deref(),
    )
    return flags1.deref()
  },
}

@Topppy
Copy link
Owner Author

Topppy commented Jan 11, 2022

mac需要动态不引入win32的native包(会报错)

@Topppy
Copy link
Owner Author

Topppy commented Jan 11, 2022

需要安装node-gyp构建环境(开发机 & 打包机)

https://github.com/nodejs/node-gyp#on-windows
python 3.9 安装 + 环境变量(要选给all users 安装)
npm config set python "C:\Programs\Python\Python39\python.exe"
npm install windows-build-tools -g

卡住解决:https://www.jianshu.com/p/e2f12fab2b78

@Topppy
Copy link
Owner Author

Topppy commented Jan 11, 2022

@Topppy
Copy link
Owner Author

Topppy commented Jan 12, 2022

锁版本, 统一 编译需要的C++版本,python版本

@Topppy Topppy added this to the publish milestone Oct 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant