Skip to content

Commit

Permalink
feat(win32-api): retriveStruct_PRINTER_INFO() accept 3th param pcbNeeded
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingsong committed Jul 22, 2022
1 parent 89abed9 commit 2588e65
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
16 changes: 12 additions & 4 deletions packages/win32-api/src/func/winspool/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@ export function retriveStruct_PRINTER_INFO<L extends M.PRINTER_INFO_LEVEL>(
pPrinter: Buffer,
Level: L,
maxCount = 1,
pcbNeeded?: number,
): M.PRINTER_INFO_X[L][] {

assert(maxCount >= 1, 'maxCount must be >= 1')

const pcb: number = typeof pcbNeeded === 'number'
? pcbNeeded
: pPrinter.byteLength
assert(pcb >= 16, 'Buffer too small')

switch (Level) {
case 1:
return retriveStruct_PRINTER_INFO_1(pPrinter, maxCount) as M.PRINTER_INFO_X[L][]
return retriveStruct_PRINTER_INFO_1(pPrinter, maxCount, pcb) as M.PRINTER_INFO_X[L][]
case 4:
return retriveStruct_PRINTER_INFO_4(pPrinter, maxCount) as M.PRINTER_INFO_X[L][]
return retriveStruct_PRINTER_INFO_4(pPrinter, maxCount, pcb) as M.PRINTER_INFO_X[L][]
default:
throw new Error(`Level not implemented:${Level}`)
}
Expand All @@ -44,10 +50,11 @@ export function retriveStruct_PRINTER_INFO<L extends M.PRINTER_INFO_LEVEL>(
function retriveStruct_PRINTER_INFO_1(
pPrinter: Buffer,
maxCount: number,
pcbNeeded: number,
): M.PRINTER_INFO_1[] {

const ret: M.PRINTER_INFO_1[] = []
const blen = pPrinter.byteLength
const blen = pcbNeeded

const keys = 4
const itemLen = keys * 8 // byes
Expand Down Expand Up @@ -96,10 +103,11 @@ function rpi1(
function retriveStruct_PRINTER_INFO_4(
pPrinter: Buffer,
maxCount: number,
pcbNeeded: number,
): M.PRINTER_INFO_4[] {

const ret: M.PRINTER_INFO_4[] = []
const blen = pPrinter.byteLength
const blen = pcbNeeded

for (let i = 0; i < maxCount; i += 1) {
const buf = Buffer.alloc(maxCount * 24) // 3key * 8byte * maxCount
Expand Down
5 changes: 3 additions & 2 deletions packages/win32-api/src/func/winspool/index.winspool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ export async function winspoolEnumPrinters<Level extends M.EnumPrinters_Level>(
)

const count = pcReturned.readUInt32LE()
const pcb = pcbNeeded.readUInt32LE()

if (ret && count) {
const arr = retriveStruct_PRINTER_INFO(pPrinterEnum, level, count)
const arr = retriveStruct_PRINTER_INFO(pPrinterEnum, level, count, pcb)
return arr as unknown as Promise<M.PRINTER_INFO_X[Level][]>
}
return []
Expand Down Expand Up @@ -144,7 +145,7 @@ export async function winspoolGetPrinter<Level extends M.PRINTER_INFO_LEVEL>(
const pcb = pcbNeeded.readUInt32LE()

if (ret) {
const [struct] = retriveStruct_PRINTER_INFO(pPrinter, Level, 1)
const [struct] = retriveStruct_PRINTER_INFO(pPrinter, Level, 1, pcb)
// const pPrinter2 = Buffer.alloc(pcb)
// const cbBuf2 = pcb
// const pcbNeeded2 = Buffer.alloc(8)
Expand Down

0 comments on commit 2588e65

Please sign in to comment.