-
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.
feat(win32-api): add UnregisterClassW(), update GetClassInfoExW()
- Loading branch information
1 parent
303e2d3
commit 93f7bb4
Showing
10 changed files
with
235 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
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
51 changes: 51 additions & 0 deletions
51
packages/win32-api/src/lib/user32/mapper/GetClassInfoExW.mapper.ts
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import assert from 'assert' | ||
|
||
import type { FlattenNestedTuple } from '@waiting/shared-types' | ||
import { WString } from 'win32-def/def' | ||
import type { MultipleChoiceMapper } from 'win32-def/types' | ||
|
||
import type { DefUser32 as Def } from '../api.def.js' | ||
import type { User32 as Lib } from '../api.types.js' | ||
|
||
|
||
export const funcName = 'GetClassInfoExW' | ||
type Func = Lib[typeof funcName] | ||
type Params = Parameters<Func> | ||
|
||
type DefFunc = typeof Def[typeof funcName] | ||
type DefParams = FlattenNestedTuple<DefFunc[1]> | ||
|
||
export const GetClassInfoExW_mapper: MultipleChoiceMapper<Params, DefParams> = (name, runtimeArgs, defParamsArray) => { | ||
if (name !== funcName) { return } | ||
const lpszClass = runtimeArgs[1] | ||
|
||
for (const row of defParamsArray) { | ||
assert(Array.isArray(row)) | ||
const defArg = row[1] | ||
|
||
switch (typeof lpszClass) { | ||
case 'string': { | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (defArg === WString) { // WString | ||
return row | ||
} | ||
break | ||
} | ||
|
||
case 'object': { | ||
assert(lpszClass instanceof Buffer, 'Invalid lpszClass type, must Buffer if object') | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (defArg === WString) { // WString | ||
return row | ||
} | ||
break | ||
} | ||
|
||
default: | ||
throw new Error(`Invalid lpszClass type: ${typeof lpszClass}`) | ||
} | ||
} | ||
// return [] // will throw Error | ||
} | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { MultipleChoiceMapperList, MultipleChoiceMapperSet } from 'win32-def/types' | ||
|
||
import * as GetClassInfoExW from './GetClassInfoExW.mapper.js' | ||
|
||
|
||
export const multipleChoiceMapperList: MultipleChoiceMapperList = new Map() | ||
export const multipleChoiceMapperSet: MultipleChoiceMapperSet = new Set() | ||
|
||
multipleChoiceMapperList.set(GetClassInfoExW.funcName, multipleChoiceMapperSet) | ||
multipleChoiceMapperSet.add(GetClassInfoExW.GetClassInfoExW_mapper) | ||
|
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
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
76 changes: 76 additions & 0 deletions
76
packages/win32-api/test/lib/user32/1000.RegisterClassExW.test.ts
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import assert from 'node:assert/strict' | ||
|
||
import { fileShortPath } from '@waiting/shared-core' | ||
import { WNDCLASSEXW_Factory } from 'win32-def/struct' | ||
import type { WNDCLASSEXW_Type } from 'win32-def/struct' | ||
|
||
import { User32 as Lib, Kernel32 } from '##/index.js' | ||
|
||
|
||
describe(fileShortPath(import.meta.url), () => { | ||
const lib = Lib.load() | ||
assert(lib) | ||
const libKnl = Kernel32.load() | ||
assert(libKnl) | ||
const hModule = libKnl.GetModuleHandleW(null) | ||
assert(hModule) | ||
|
||
const lpszClassName = 'test-class-name-' + Date.now().toString() | ||
const lpszMenuName = 'test-menu-name-' + Date.now().toString() | ||
|
||
describe('RegisterClassExW()', () => { | ||
|
||
it('no hInstance', () => { | ||
const { payload } = WNDCLASSEXW_Factory() | ||
assert(payload.cbSize === 80) | ||
payload.lpszClassName = lpszClassName | ||
payload.lpszMenuName = lpszMenuName | ||
|
||
const atom = lib.RegisterClassExW(payload) | ||
assert(atom) | ||
lib.UnregisterClassW(atom, 0) | ||
}) | ||
|
||
it('hInstance', () => { | ||
const { payload } = WNDCLASSEXW_Factory() | ||
assert(payload.cbSize === 80) | ||
payload.lpszClassName = lpszClassName | ||
payload.lpszMenuName = lpszMenuName | ||
payload.hInstance = hModule | ||
|
||
const atom = lib.RegisterClassExW(payload) | ||
assert(atom) | ||
lib.UnregisterClassW(atom, 0) | ||
}) | ||
|
||
|
||
it('RegisterClassExW() re-gen', async () => { | ||
const { payload: p1 } = WNDCLASSEXW_Factory() | ||
assert(p1.cbSize === 80) | ||
p1.lpszClassName = lpszClassName | ||
p1.lpszMenuName = lpszMenuName | ||
p1.hInstance = hModule | ||
|
||
const atom = lib.RegisterClassExW(p1) | ||
assert(atom) | ||
|
||
try { | ||
lib.UnregisterClassW(atom, 0) | ||
const atom2 = lib.RegisterClassExW(p1) | ||
assert(atom2) | ||
try { | ||
const atom3 = lib.RegisterClassExW(p1) | ||
assert(!atom3) | ||
} | ||
finally { | ||
lib.UnregisterClassW(atom2, 0) | ||
} | ||
} | ||
finally { | ||
lib.UnregisterClassW(atom, 0) | ||
} | ||
}) | ||
}) | ||
|
||
}) | ||
|
76 changes: 76 additions & 0 deletions
76
packages/win32-api/test/lib/user32/1001.GetClassInfoExW.test.ts
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import assert from 'node:assert/strict' | ||
|
||
import { fileShortPath } from '@waiting/shared-core' | ||
import { ucsBufferFrom } from 'win32-def' | ||
import { WNDCLASSEXW_Factory } from 'win32-def/struct' | ||
import type { WNDCLASSEXW_Type } from 'win32-def/struct' | ||
|
||
import { User32 as Lib, Kernel32 } from '##/index.js' | ||
|
||
|
||
describe(fileShortPath(import.meta.url), () => { | ||
const lib = Lib.load() | ||
assert(lib) | ||
const libKnl = Kernel32.load() | ||
assert(libKnl) | ||
const hModule = libKnl.GetModuleHandleW(null) | ||
assert(hModule) | ||
|
||
const lpszClassName = 'test-class-' + Date.now().toString() | ||
const lpszMenuName = 'test-menu-' + Date.now().toString() | ||
|
||
describe('GetClassInfoExW()', () => { | ||
it('GetClassInfoExW() pass string', () => { | ||
const { payload } = WNDCLASSEXW_Factory() | ||
assert(payload.cbSize === 80) | ||
payload.lpszClassName = lpszClassName | ||
payload.lpszMenuName = lpszMenuName | ||
payload.hInstance = hModule | ||
payload.style = 0x1000 | ||
|
||
const atom = lib.RegisterClassExW(payload) | ||
assert(atom) | ||
|
||
try { | ||
const { payload: p2 } = WNDCLASSEXW_Factory() | ||
const ret = lib.GetClassInfoExW(hModule, lpszClassName, p2) | ||
assert(ret) | ||
assert(p2.lpszClassName === lpszClassName) | ||
assert(p2.lpszMenuName === lpszMenuName) | ||
assert(p2.hInstance === hModule) | ||
assert(p2.style === 0x1000) | ||
assert(ret === atom) | ||
} | ||
finally { | ||
lib.UnregisterClassW(atom, 0) | ||
} | ||
}) | ||
|
||
it('GetClassInfoExW() pass buffer', () => { | ||
const { payload } = WNDCLASSEXW_Factory() | ||
assert(payload.cbSize === 80) | ||
payload.lpszClassName = lpszClassName | ||
payload.lpszMenuName = lpszMenuName | ||
payload.hInstance = hModule | ||
payload.style = 0x1000 | ||
|
||
const atom = lib.RegisterClassExW(payload) | ||
assert(atom) | ||
|
||
try { | ||
const { payload: p2 } = WNDCLASSEXW_Factory() | ||
const ret = lib.GetClassInfoExW(hModule, ucsBufferFrom(lpszClassName), p2) | ||
assert(ret) | ||
assert(p2.lpszClassName === lpszClassName) | ||
assert(p2.lpszMenuName === lpszMenuName) | ||
assert(p2.hInstance === hModule) | ||
assert(p2.style === 0x1000) | ||
} | ||
finally { | ||
lib.UnregisterClassW(atom, 0) | ||
} | ||
}) | ||
}) | ||
|
||
}) | ||
|