Skip to content

Commit b17090a

Browse files
committed
refactor: Major code overhaul; greatly simplify Koffi usage; improve code readability
1 parent 73bcba7 commit b17090a

30 files changed

+354
-412
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ See [releases](https://github.com/Septh/libwin32/releases) on Github.
7171

7272
#### Added in 0.5.0
7373
* kernel32.dll
74+
* GetModuleHandleEx
7475
* QueryFullProcessImageName
76+
* SetLastError
7577

7678
#### Added in 0.4.0
7779
*Many thanks to [@shrirajh](https://github.com/shrirajh) for the impressive work on [b2bf65b](https://github.com/Septh/libwin32/commit/b2bf65b6d20dbe7dae4e48341176a8407c135c46)!*

source/ctypes.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ export const cLPCWSTR = koffi.types.str16
1717
export const cLPWSTR = koffi.types.str16
1818

1919
// Pointers.
20-
export const cLPVOID = koffi.pointer('LPVOID', koffi.types.void)
21-
export const cPDWORD = koffi.pointer('PDWORD', koffi.types.uint32)
22-
export const cLPDWORD = koffi.pointer('LPDWORD', koffi.types.uint32)
20+
export const cLPVOID = koffi.pointer(koffi.types.void)
21+
export const cPDWORD = koffi.pointer(koffi.types.uint32)
22+
export const cLPDWORD = koffi.pointer(koffi.types.uint32)
2323
export const cINT_PTR = koffi.types.int64
2424

2525
// Apis.
26-
export const cHANDLE = koffi.pointer('HANDLE', koffi.opaque())
27-
export const cLPHANDLE = koffi.pointer('LPHANDLE', cHANDLE)
28-
export const cPHANDLE = koffi.pointer('PHANDLE', cHANDLE)
26+
export const cHANDLE = koffi.pointer(koffi.opaque())
27+
export const cPHANDLE = koffi.pointer(cHANDLE)
28+
export const cLPHANDLE = koffi.pointer(cHANDLE)
2929
export const cHINSTANCE = koffi.alias('HINSTANCE', cHANDLE)
3030
export const cWPARAM = koffi.types.uint64 // == UINT_PTR
3131
export const cLPARAM = koffi.types.int64 // == LONG_PTR
@@ -34,20 +34,21 @@ export const cLRESULT = koffi.types.int64 // == LONG_PTR
3434
export const cATOM = koffi.types.uint16 // == WORD
3535

3636
// TypeScript base types.
37-
export type HANDLE<Kind extends string> = koffi.IKoffiCType & { __kind: Kind }
38-
export type HINSTANCE = HANDLE<'HINSTANCE'>
39-
export type WPARAM = number | HANDLE<string>
40-
export type LPARAM = number | BigInt | HANDLE<string>
41-
export type HRESULT = number | HANDLE<string>
42-
export type LRESULT = number | BigInt | HANDLE<string>
37+
export type __HANDLE__<Kind extends string> = koffi.IKoffiCType & { __kind: Kind }
38+
export type HANDLE = __HANDLE__<string> // Any kind of handle
39+
export type HINSTANCE = __HANDLE__<'HINSTANCE'>
40+
export type WPARAM = number | HANDLE
41+
export type LPARAM = number | BigInt | HANDLE
42+
export type HRESULT = number | HANDLE
43+
export type LRESULT = number | BigInt | HANDLE
4344
export type ATOM = number | string
4445

4546
// GUID structure
46-
export const cGUID = koffi.struct('GUID', {
47+
export const cGUID = koffi.struct({
4748
Data1: koffi.types.uint32,
4849
Data2: koffi.types.uint16,
4950
Data3: koffi.types.uint16,
50-
Data4: koffi.array('uint8', 8),
51+
Data4: koffi.array(koffi.types.uint8, 8),
5152
})
5253

5354
// TypeScript type for GUID

source/demos/enumdesktopwindows.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import {
2-
EnumDesktopWindows, GetWindowText
2+
EnumWindows, GetWindowText
33
} from 'libwin32'
44

55
const filters = [
66
'MSCTFIME UI', 'Default IME'
77
]
88

9-
EnumDesktopWindows((hwnd, lParam) => {
9+
EnumWindows((hwnd, lParam) => {
1010
const title = GetWindowText(hwnd)
1111
if (title.length && !filters.includes(title))
1212
console.log(title)

source/private.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ export class Win32Dll implements Disposable {
2727
}
2828
}
2929

30+
export const textEncoder = /*#__PURE__*/new TextEncoder()
3031
export const textDecoder = /*#__PURE__*/new TextDecoder('utf-16')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* dwFlags parameter for GetModuleHandleEx
3+
*/
4+
export enum GET_MODULE_HANDLE_EX_FLAG_ {
5+
PIN = 0x00000001,
6+
UNCHANGED_REFCOUNT = 0x00000002,
7+
FROM_ADDRESS = 0x00000004
8+
}

source/win32/consts/MF.ts

+26-27
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1-
21
/**
32
* Menu Flags
43
*/
54
export enum MF_ {
6-
BYCOMMAND = 0x00000000,
7-
BYPOSITION = 0x00000400,
8-
SEPARATOR = 0x00000800,
9-
REMOVE = 0x00001000,
10-
APPEND = 0x00000100,
11-
DELETE = 0x00000200,
12-
INSERT = 0x00000000,
13-
CHANGE = 0x00000080,
14-
ENABLED = 0x00000000,
15-
GRAYED = 0x00000001,
16-
DISABLED = 0x00000002,
17-
UNCHECKED = 0x00000000,
18-
CHECKED = 0x00000008,
5+
BYCOMMAND = 0x00000000,
6+
BYPOSITION = 0x00000400,
7+
SEPARATOR = 0x00000800,
8+
REMOVE = 0x00001000,
9+
APPEND = 0x00000100,
10+
DELETE = 0x00000200,
11+
INSERT = 0x00000000,
12+
CHANGE = 0x00000080,
13+
ENABLED = 0x00000000,
14+
GRAYED = 0x00000001,
15+
DISABLED = 0x00000002,
16+
UNCHECKED = 0x00000000,
17+
CHECKED = 0x00000008,
1918
USECHECKBITMAPS = 0x00000200,
20-
STRING = 0x00000000,
21-
BITMAP = 0x00000004,
22-
OWNERDRAW = 0x00000100,
23-
POPUP = 0x00000010,
24-
MENUBARBREAK = 0x00000020,
25-
MENUBREAK = 0x00000040,
26-
UNHILITE = 0x00000000,
27-
HILITE = 0x00000080,
28-
DEFAULT = 0x00001000,
29-
SYSMENU = 0x00002000,
30-
HELP = 0x00004000,
31-
RIGHTJUSTIFY = 0x00004000,
32-
MOUSESELECT = 0x00008000
19+
STRING = 0x00000000,
20+
BITMAP = 0x00000004,
21+
OWNERDRAW = 0x00000100,
22+
POPUP = 0x00000010,
23+
MENUBARBREAK = 0x00000020,
24+
MENUBREAK = 0x00000040,
25+
UNHILITE = 0x00000000,
26+
HILITE = 0x00000080,
27+
DEFAULT = 0x00001000,
28+
SYSMENU = 0x00002000,
29+
HELP = 0x00004000,
30+
RIGHTJUSTIFY = 0x00004000,
31+
MOUSESELECT = 0x00008000
3332
}

source/win32/consts/NIF.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
1+
/**
2+
* Values for NOTIFYICONDATA.uFlags
3+
*/
24
export const enum NIF_ {
35
MESSAGE = 0x0001,
46
ICON = 0x0002,
@@ -8,4 +10,4 @@ export const enum NIF_ {
810
GUID = 0x0020,
911
REALTIME = 0x0040,
1012
SHOWTIP = 0x0080,
11-
}
13+
}

source/win32/consts/NIM.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
1+
/**
2+
* Values for the dwMessagge parameter of Shell_NotifyIcon()
3+
*/
24
export const enum NIM_ {
35
ADD = 0x00000000,
46
MODIFY = 0x00000001,
57
DELETE = 0x00000002,
68
SETFOCUS = 0x00000003,
79
SETVERSION = 0x00000004,
8-
}
10+
}

source/win32/consts/TPM.ts

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
21
/**
32
* Track Popup Menu Flags
43
*/
54
export enum TPM_ {
6-
LEFTBUTTON = 0x0000,
7-
RIGHTBUTTON = 0x0002,
8-
LEFTALIGN = 0x0000,
9-
CENTERALIGN = 0x0004,
10-
RIGHTALIGN = 0x0008,
11-
TOPALIGN = 0x0000,
12-
VCENTERALIGN = 0x0010,
13-
BOTTOMALIGN = 0x0020,
14-
HORIZONTAL = 0x0000,
15-
VERTICAL = 0x0040,
16-
NONOTIFY = 0x0080,
17-
RETURNCMD = 0x0100,
18-
RECURSE = 0x0001,
5+
LEFTBUTTON = 0x0000,
6+
RIGHTBUTTON = 0x0002,
7+
LEFTALIGN = 0x0000,
8+
CENTERALIGN = 0x0004,
9+
RIGHTALIGN = 0x0008,
10+
TOPALIGN = 0x0000,
11+
VCENTERALIGN = 0x0010,
12+
BOTTOMALIGN = 0x0020,
13+
HORIZONTAL = 0x0000,
14+
VERTICAL = 0x0040,
15+
NONOTIFY = 0x0080,
16+
RETURNCMD = 0x0100,
17+
RECURSE = 0x0001,
1918
HORPOSANIMATION = 0x0400,
2019
HORNEGANIMATION = 0x0800,
2120
VERPOSANIMATION = 0x1000,
2221
VERNEGANIMATION = 0x2000,
23-
NOANIMATION = 0x4000,
24-
LAYOUTRTL = 0x8000
22+
NOANIMATION = 0x4000,
23+
LAYOUTRTL = 0x8000
2524
}

source/win32/kernel32/console.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import { kernel32 } from './_lib.js'
22
import { cHWND, type HWND } from '../user32/window.js'
33

4-
// #region Types
5-
6-
// #endregion
7-
8-
// #region Functions
9-
104
/**
115
* Retrieves the window handle used by the console associated with the calling process.
126
*
137
* https://learn.microsoft.com/en-us/windows/console/getconsolewindow
148
*/
15-
export const GetConsoleWindow: () => HWND = /*#__PURE__*/kernel32.func('GetConsoleWindow', cHWND, [])
16-
17-
// #endregion
9+
export const GetConsoleWindow: () => HWND | null = /*#__PURE__*/kernel32.func('GetConsoleWindow', cHWND, [])

source/win32/kernel32/error.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { kernel32 } from './_lib.js'
2-
import { cDWORD } from '../../ctypes.js'
3-
4-
// #region Types
5-
// #endregion
6-
7-
// #region Functions
2+
import { cDWORD, cVOID } from '../../ctypes.js'
83

94
/**
105
* Retrieves the calling thread's last-error code value.
@@ -13,4 +8,9 @@ import { cDWORD } from '../../ctypes.js'
138
*/
149
export const GetLastError: () => number = /*#__PURE__*/kernel32.func('GetLastError', cDWORD, [])
1510

16-
// #endregion
11+
/**
12+
* Sets the last-error code for the calling thread.
13+
*
14+
* https://learn.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-setlasterror
15+
*/
16+
export const SetLastError: (dwErrcode: number) => void = /*#__PURE__*/kernel32.func('SetLastError', cVOID, [ cDWORD ])

source/win32/kernel32/module.ts

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import { koffi } from '../../private.js'
2-
import { cHINSTANCE, cLPCWSTR, type HINSTANCE } from '../../ctypes.js'
2+
import { cBOOL, cDWORD, cHINSTANCE, cLPCWSTR, type HINSTANCE } from '../../ctypes.js'
3+
import { GET_MODULE_HANDLE_EX_FLAG_ } from '../consts/GET_MODULE_HANDLE_EX_FLAG.js'
34
import { kernel32 } from './_lib.js'
45

5-
// #region Types
6-
6+
// HMODULEs can be used in place of HINSTANCEs
77
export const cHMODULE = koffi.alias('HMODULE', cHINSTANCE)
88
export type HMODULE = HINSTANCE
99

10-
// #endregion
11-
12-
// #region Functions
13-
1410
/**
1511
* Retrieves a module handle for the specified module.
1612
*
@@ -20,4 +16,26 @@ export const GetModuleHandle: (
2016
lpModuleName: string | null
2117
) => HMODULE = /*#__PURE__*/kernel32.func('GetModuleHandleW', cHMODULE, [ cLPCWSTR ])
2218

23-
// #endregion
19+
/**
20+
* Retrieves a module handle for the specified module and increments the module's reference count
21+
* unless GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT is specified.
22+
* The module must have been loaded by the calling process.
23+
*
24+
* https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulehandleexw
25+
*/
26+
/*#__NO_SIDE_EFFECTS__*/
27+
export function GetModuleHandleEx(
28+
dwFlags: GET_MODULE_HANDLE_EX_FLAG_,
29+
lpModuleName: string | null
30+
): HMODULE | null {
31+
const hModule: [ HMODULE | null ] = [ null ]
32+
return _GetModuleHandleExW(dwFlags, lpModuleName, hModule) === 0
33+
? null
34+
: hModule[0]
35+
}
36+
37+
const _GetModuleHandleExW:(
38+
dwFlags: GET_MODULE_HANDLE_EX_FLAG_,
39+
lpModuleName: string | null,
40+
phModule: [ HMODULE | null ]
41+
) => number = /*#__PURE__*/kernel32.func('GetModuleHandleExW', cBOOL, [ cDWORD, cLPCWSTR, koffi.out(koffi.pointer(cHMODULE)) ])

source/win32/kernel32/process.ts

+7-14
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,26 @@ import { koffi, textDecoder } from '../../private.js'
22
import { cBOOL, cDWORD, cHANDLE, cLPDWORD, cLPWSTR, type HANDLE } from '../../ctypes.js'
33
import { kernel32 } from './_lib.js'
44

5-
// #region Types
6-
// #endregion
7-
8-
// #region Functions
9-
105
/**
116
* Retrieves the full name of the executable image for the specified process.
127
*
138
* https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-queryfullprocessimagenamew
149
*/
1510
/*#__NO_SIDE_EFFECTS__*/
1611
export function QueryFullProcessImageName(
17-
hProcess: HANDLE<''>,
12+
hProcess: HANDLE,
1813
dwFlags: number
1914
): string | null {
2015
const exeName = new Uint16Array(256)
21-
const dwSize = [ exeName.length ] as [ number ]
22-
return _QueryFullProcessImageName(hProcess, dwFlags, exeName, dwSize) === 0
16+
const dwSize: [ number ] = [ exeName.length ]
17+
return _QueryFullProcessImageNameW(hProcess, dwFlags, exeName, dwSize) === 0
2318
? null
2419
: textDecoder.decode(exeName).slice(0, dwSize[0])
2520
}
2621

27-
const _QueryFullProcessImageName: (
28-
hProcess: HANDLE<string>,
22+
const _QueryFullProcessImageNameW: (
23+
hProcess: HANDLE,
2924
dwFlags: number,
30-
lpExeName: Uint16Array,
31-
lpdwSize: [ number ]
25+
exeName: Uint16Array,
26+
dwSize: [ number ]
3227
) => number = /*#__PURE__*/kernel32.func('QueryFullProcessImageNameW', cBOOL, [ cHANDLE, cDWORD, koffi.out(cLPWSTR), koffi.inout(cLPDWORD) ])
33-
34-
// #endregion

0 commit comments

Comments
 (0)