Skip to content

Commit

Permalink
feat(win32-api): fun/user32GetWindowText()
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingsong committed Jul 20, 2022
1 parent 2484706 commit e672a2a
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 45 deletions.
37 changes: 0 additions & 37 deletions packages/win32-api/src/func/user32/FindWindowEx.ts

This file was deleted.

6 changes: 6 additions & 0 deletions packages/win32-api/src/func/user32/helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import ref from 'ref-napi'

import { DllNames } from '../../index.js'
import { User32 as DLL } from '../../index.promise.js'


export { DModel as M } from '../../index.js'
export { ref }

export const dllName = DllNames.user32
export type Win32Fns = DLL.Win32Fns

57 changes: 56 additions & 1 deletion packages/win32-api/src/func/user32/index.user32.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,58 @@
import {
genUcsBufferFrom,
ucsBufferToString,
} from '../../index.js'
import { getMod } from '../func.helper.js'

export * from './FindWindowEx.js'
import { Win32Fns, dllName, M } 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 user32FindWindowEx(
hwndParent: M.HWND,
hwndChildAfter: M.HWND,
lpszClass: string | null,
lpszWindow: string | null,
): Promise<M.HWND | undefined> {

const mod = getMod<Win32Fns>(dllName)

const lpszClassBuf = genUcsBufferFrom(lpszClass)
const lpszWindowBuf = genUcsBufferFrom(lpszWindow)

const hWnd = await mod.FindWindowExW(
hwndParent,
hwndChildAfter,
lpszClassBuf,
lpszWindowBuf,
)

const ret = hWnd ? hWnd : undefined
return ret
}


/**
* @docs https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowtextw
*/
export async function user32GetWindowText(
hWnd: M.HWND,
nMaxCount: M.INT,
): Promise<string> {

const mod = getMod<Win32Fns>(dllName)

const len = nMaxCount
const buf = Buffer.alloc(len * 2)

const ret = await mod.GetWindowTextW(hWnd, buf, len)
if (ret > 0) {
const str = ucsBufferToString(buf)
return str
}
return ''
}
1 change: 1 addition & 0 deletions packages/win32-api/src/func/winspool/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DllNames } from '../../index.js'
import { Winspool as DLL } from '../../index.promise.js'


export { DModel as M } from '../../index.js'
export { ref }

export const dllName = DllNames.winspool
Expand Down
2 changes: 1 addition & 1 deletion packages/win32-api/src/func/winspool/index.winspool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'node:assert'

import { getMod } from '../func.helper.js'

import { Win32Fns, dllName, ref } from './helper.js'
import { Win32Fns, dllName, M, ref } from './helper.js'


/**
Expand Down
12 changes: 6 additions & 6 deletions packages/win32-api/test/70.user32.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { fileShortPath } from '@waiting/shared-core'
import ffi from 'ffi-napi'
import { sleep } from 'zx'

import { user32FindWindowEx } from '../src/index.fun.js'
import {
user32FindWindowEx,
user32GetWindowText,
} from '../src/index.fun.js'
import {
DModel as M,
DTypes as W,
Expand Down Expand Up @@ -41,12 +44,9 @@ describe(fileShortPath(import.meta.url), () => {

const len = title.length + 1
assert(len > 0)
const buf = Buffer.alloc(len * 2)
let str = ''

await user32.GetWindowTextW(hWnd, buf, len)
str = buf.toString('ucs2').replace(/\0+$/, '')
assert(str === title, `title should be changed to ${title}, bug got ${str}`)
const text = await user32GetWindowText(hWnd, len)
assert(text && text === title, `title should be changed to ${title}, bug got ${text ?? 'n/a'}`)

const point = StructFactory<M.POINT>(DS.POINT)
point.x = 101
Expand Down

0 comments on commit e672a2a

Please sign in to comment.