diff --git a/packages/win32-api/src/func/user32/FindWindowEx.ts b/packages/win32-api/src/func/user32/FindWindowEx.ts deleted file mode 100644 index c5b2c87e..00000000 --- a/packages/win32-api/src/func/user32/FindWindowEx.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { - DModel as M, - genUcsBufferFrom, -} from '../../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 user32FindWindowEx( - hwndParent: M.HWND, - hwndChildAfter: M.HWND, - lpszClass: string | null, - lpszWindow: string | null, -): Promise { - - const mod = getMod(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 -} - diff --git a/packages/win32-api/src/func/user32/helper.ts b/packages/win32-api/src/func/user32/helper.ts index d95624a9..cab02c38 100644 --- a/packages/win32-api/src/func/user32/helper.ts +++ b/packages/win32-api/src/func/user32/helper.ts @@ -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 diff --git a/packages/win32-api/src/func/user32/index.user32.ts b/packages/win32-api/src/func/user32/index.user32.ts index dbd5d12b..949946c0 100644 --- a/packages/win32-api/src/func/user32/index.user32.ts +++ b/packages/win32-api/src/func/user32/index.user32.ts @@ -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 { + + const mod = getMod(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 { + + const mod = getMod(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 '' +} diff --git a/packages/win32-api/src/func/winspool/helper.ts b/packages/win32-api/src/func/winspool/helper.ts index 80f9d222..33062be2 100644 --- a/packages/win32-api/src/func/winspool/helper.ts +++ b/packages/win32-api/src/func/winspool/helper.ts @@ -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 diff --git a/packages/win32-api/src/func/winspool/index.winspool.ts b/packages/win32-api/src/func/winspool/index.winspool.ts index fa6e89e0..fff0c9d7 100644 --- a/packages/win32-api/src/func/winspool/index.winspool.ts +++ b/packages/win32-api/src/func/winspool/index.winspool.ts @@ -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' /** diff --git a/packages/win32-api/test/70.user32.test.ts b/packages/win32-api/test/70.user32.test.ts index ec3a94a7..8429d393 100644 --- a/packages/win32-api/test/70.user32.test.ts +++ b/packages/win32-api/test/70.user32.test.ts @@ -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, @@ -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(DS.POINT) point.x = 101