From 1e410d275e9b9fa079c1f69c21f7e44753b879c2 Mon Sep 17 00:00:00 2001 From: waiting <1661926154@qq.com> Date: Wed, 20 Jul 2022 21:55:06 +0800 Subject: [PATCH] refactor(win32-api): merge files into index.winspool.ts --- .../src/func/winspool/GetDefaultPrinter.ts | 35 ------------ .../src/func/winspool/OpenPrinter.ts | 30 ---------- .../win32-api/src/func/winspool/helper.ts | 5 ++ .../src/func/winspool/index.winspool.ts | 55 ++++++++++++++++++- 4 files changed, 58 insertions(+), 67 deletions(-) delete mode 100644 packages/win32-api/src/func/winspool/GetDefaultPrinter.ts delete mode 100644 packages/win32-api/src/func/winspool/OpenPrinter.ts diff --git a/packages/win32-api/src/func/winspool/GetDefaultPrinter.ts b/packages/win32-api/src/func/winspool/GetDefaultPrinter.ts deleted file mode 100644 index 77e09d42..00000000 --- a/packages/win32-api/src/func/winspool/GetDefaultPrinter.ts +++ /dev/null @@ -1,35 +0,0 @@ -import assert from 'node:assert' - -import { getMod } from '../func.helper.js' - -import { Win32Fns, dllName } from './helper.js' - - -/** - * Retrieves the printer name of the default printer for the current user on the local computer. - * @docs https://docs.microsoft.com/en-us/windows/win32/printdocs/getdefaultprinter - */ -export async function winspoolGetDefaultPrinter(maxNameLength = 256): Promise { - const mod = getMod(dllName) - - assert(maxNameLength > 2) - - const len = maxNameLength + 1 - - const pszBuf = Buffer.alloc(len * 2) - const pcchBuf = Buffer.alloc(4) - pcchBuf.writeUint32LE(len) - - const ret = await mod.GetDefaultPrinterW(pszBuf, pcchBuf) - if (! ret) { - return - } - - const pcch = pcchBuf.readUInt32LE() - if (pcch > 0) { - const size = pcch - 1 - const psz = pszBuf.toString('ucs2', 0, size * 2).replace(/\0+$/u, '') - return psz - } -} - diff --git a/packages/win32-api/src/func/winspool/OpenPrinter.ts b/packages/win32-api/src/func/winspool/OpenPrinter.ts deleted file mode 100644 index ac56e7b2..00000000 --- a/packages/win32-api/src/func/winspool/OpenPrinter.ts +++ /dev/null @@ -1,30 +0,0 @@ -import assert from 'node:assert' - -// eslint-disable-next-line import/no-extraneous-dependencies -import ref from 'ref-napi' - -import { DModel as M } from '../../../src/index.js' -import { getMod } from '../func.helper.js' - -import { Win32Fns, dllName } from './helper.js' - - -/** - * Retrieves a handle to the specified printer or print server or other types of handles in the print subsystem. - * @docs https://docs.microsoft.com/en-us/windows/win32/printdocs/openprinter - * @docs https://docs.microsoft.com/zh-cn/windows/win32/printdocs/openprinter - */ -export async function winspoolOpenPrinter(printerName: string): Promise { - const mod = getMod(dllName) - - assert(printerName) - - const nameBuf = Buffer.from(printerName + '\0', 'ucs2') - const ptr = Buffer.alloc(8) - const ret = await mod.OpenPrinterW(nameBuf, ptr, ref.NULL) - if (ret) { - const hWnd = ptr.readBigInt64LE() - return hWnd - } -} - diff --git a/packages/win32-api/src/func/winspool/helper.ts b/packages/win32-api/src/func/winspool/helper.ts index f97c1bc5..80f9d222 100644 --- a/packages/win32-api/src/func/winspool/helper.ts +++ b/packages/win32-api/src/func/winspool/helper.ts @@ -1,7 +1,12 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +import ref from 'ref-napi' + import { DllNames } from '../../index.js' import { Winspool as DLL } from '../../index.promise.js' +export { ref } + export const dllName = DllNames.winspool export type Win32Fns = DLL.Win32Fns diff --git a/packages/win32-api/src/func/winspool/index.winspool.ts b/packages/win32-api/src/func/winspool/index.winspool.ts index ebd93ddb..fa6e89e0 100644 --- a/packages/win32-api/src/func/winspool/index.winspool.ts +++ b/packages/win32-api/src/func/winspool/index.winspool.ts @@ -1,4 +1,55 @@ +import assert from 'node:assert' -export * from './GetDefaultPrinter.js' -export * from './OpenPrinter.js' +import { getMod } from '../func.helper.js' + +import { Win32Fns, dllName, ref } from './helper.js' + + +/** + * Retrieves the printer name of the default printer for the current user on the local computer. + * @docs https://docs.microsoft.com/en-us/windows/win32/printdocs/getdefaultprinter + */ +export async function winspoolGetDefaultPrinter(maxNameLength = 256): Promise { + const mod = getMod(dllName) + + assert(maxNameLength > 2) + + const len = maxNameLength + 1 + + const pszBuf = Buffer.alloc(len * 2) + const pcchBuf = Buffer.alloc(4) + pcchBuf.writeUint32LE(len) + + const ret = await mod.GetDefaultPrinterW(pszBuf, pcchBuf) + if (! ret) { + return + } + + const pcch = pcchBuf.readUInt32LE() + if (pcch > 0) { + const size = pcch - 1 + const psz = pszBuf.toString('ucs2', 0, size * 2).replace(/\0+$/u, '') + return psz + } +} + + +/** + * Retrieves a handle to the specified printer or print server or other types of handles in the print subsystem. + * @docs https://docs.microsoft.com/en-us/windows/win32/printdocs/openprinter + * @docs https://docs.microsoft.com/zh-cn/windows/win32/printdocs/openprinter + */ +export async function winspoolOpenPrinter(printerName: string): Promise { + const mod = getMod(dllName) + + assert(printerName) + + const nameBuf = Buffer.from(printerName + '\0', 'ucs2') + const ptr = Buffer.alloc(8) + const ret = await mod.OpenPrinterW(nameBuf, ptr, ref.NULL) + if (ret) { + const hWnd = ptr.readBigInt64LE() + return hWnd + } +}