Skip to content

Commit

Permalink
refactor(win32-api): merge files into index.winspool.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingsong committed Jul 20, 2022
1 parent 7136eb3 commit 1e410d2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 67 deletions.
35 changes: 0 additions & 35 deletions packages/win32-api/src/func/winspool/GetDefaultPrinter.ts

This file was deleted.

30 changes: 0 additions & 30 deletions packages/win32-api/src/func/winspool/OpenPrinter.ts

This file was deleted.

5 changes: 5 additions & 0 deletions packages/win32-api/src/func/winspool/helper.ts
Original file line number Diff line number Diff line change
@@ -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

55 changes: 53 additions & 2 deletions packages/win32-api/src/func/winspool/index.winspool.ts
Original file line number Diff line number Diff line change
@@ -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<string | undefined> {
const mod = getMod<Win32Fns>(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<M.HANDLE | undefined> {
const mod = getMod<Win32Fns>(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
}
}

0 comments on commit 1e410d2

Please sign in to comment.