-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(win32-api): merge files into index.winspool.ts
- Loading branch information
1 parent
7136eb3
commit 1e410d2
Showing
4 changed files
with
58 additions
and
67 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|