Skip to content

Commit e8fb79f

Browse files
committed
refactor(all): more code cleanup
1 parent 35ba4ff commit e8fb79f

File tree

7 files changed

+126
-137
lines changed

7 files changed

+126
-137
lines changed

source/demos/createwindow.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ import {
33
RegisterClassEx, LoadCursor, LoadIcon, WNDCLASSEX,
44
CreateWindowEx, ShowWindow, UpdateWindow, DefWindowProc,
55
GetMessage, TranslateMessage, DispatchMessage, PostQuitMessage,
6-
MessageBox,
7-
type HINSTANCE, type WPARAM, type LPARAM, type HWND, type MSG,
8-
FormatMessage
6+
FormatMessage, MessageBox,
7+
type HINSTANCE, type WPARAM, type LPARAM, type HWND, type MSG
98
} from 'libwin32'
109
import { CS_, CW_, FORMAT_MESSAGE_, IDC_, IDI_, MB_, SW_, WM_, WS_, WS_EX_ } from 'libwin32/consts'
1110

12-
const windowClass = "NodeApp"
13-
const windowName = "Window Demo!"
14-
const appTitle = "A NodeJS app using the Win32 API"
11+
const windowClass = 'libwin32_app'
12+
const windowName = 'libwin32 demo: CreateWindow'
1513

1614
function wndProc(hWnd: HWND, uMmsg: WM_, wParam: WPARAM, lParam: LPARAM) {
1715

@@ -33,18 +31,19 @@ function wndProc(hWnd: HWND, uMmsg: WM_, wParam: WPARAM, lParam: LPARAM) {
3331

3432
function WinMain(hInstance: HINSTANCE, nCmdShow: SW_): number {
3533

36-
using wcex = new WNDCLASSEX(wndProc) // Note: cbSize is set by the WNDCLASSEX constructor
34+
const wcex = new WNDCLASSEX() // Note: cbSize is set by the WNDCLASSEX constructor
3735
wcex.lpszClassName = windowClass
3836
wcex.style = CS_.HREDRAW | CS_.VREDRAW
3937
wcex.hInstance = hInstance
38+
wcex.lpfnWndProc = wndProc
4039
wcex.hCursor = LoadCursor(null, IDC_.ARROW)
4140
wcex.hIcon = LoadIcon(hInstance, IDI_.APPLICATION)
4241
wcex.hIconSm = LoadIcon(hInstance, IDI_.APPLICATION)
43-
wcex.hbrBackground = 13 as any // Note: brushes are not yet implemented. 13 is the standard background.
42+
wcex.hbrBackground = 13 as any // Note: brushes are not yet implemented. 13 is the standard background.
4443

4544
const atom = RegisterClassEx(wcex)
4645
if (!atom) {
47-
MessageBox(null, "Call to RegisterClassEx failed!", appTitle, MB_.OK | MB_.ICONERROR)
46+
MessageBox(null, 'Call to RegisterClassEx failed!', 'libwin32', MB_.OK | MB_.ICONERROR)
4847
return 1
4948
}
5049

@@ -58,7 +57,7 @@ function WinMain(hInstance: HINSTANCE, nCmdShow: SW_): number {
5857
if (!hWnd) {
5958
const err = GetLastError()
6059
const msg = FormatMessage(FORMAT_MESSAGE_.FROM_SYSTEM, null, err, 0)
61-
MessageBox(null, "Call to CreateWindow failed!\n" + msg, appTitle, MB_.OK | MB_.ICONERROR)
60+
MessageBox(null, 'Call to CreateWindowEx failed!\n' + msg, 'libwin32', MB_.OK | MB_.ICONERROR)
6261
return err
6362
}
6463

source/win32/kernel32/error.ts

+25-25
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@ import { cDWORD, cLPCVOID, cLPWSTR, cVOID, type HMODULE } from '../ctypes.js'
33
import type { FORMAT_MESSAGE_ } from '../consts.js'
44
import { kernel32 } from './_lib.js'
55

6+
/**
7+
* Formats a message string.
8+
*
9+
* Note: the Arguments parameter is not yet supported. Right now, you can use FormatMessage()
10+
* to retrieve the message text for a system-defined error returned by GetLastError().
11+
*
12+
* https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessagew
13+
*/
14+
export function FormatMessage(dwFlags: FORMAT_MESSAGE_ | number, lpSource: HMODULE | string | null, dwMessageId: number, dwLanguageId: number): string {
15+
FormatMessage.fn ??= kernel32.func('FormatMessageW', cDWORD, [ cDWORD, cLPCVOID, cDWORD, cDWORD, cLPWSTR, cDWORD, '...' as any ])
16+
17+
const out = new Uint16Array(2048)
18+
const len = FormatMessage.fn(
19+
dwFlags, lpSource, dwMessageId, dwLanguageId,
20+
out, out.length,
21+
'int', 0 // Fake va_list
22+
)
23+
return textDecoder.decode(out.slice(0, len))
24+
}
25+
26+
/** @internal */
27+
export declare namespace FormatMessage {
28+
export var fn: koffi.KoffiFunc<(dwFlags: number, lpSource: HMODULE | string | null, dwMessageId: number, dwLanguageId: number, lpBuffer: Uint16Array, nSize: number, ...args: any[]) => number>
29+
}
30+
631
/**
732
* Retrieves the calling thread's last-error code value.
833
*
@@ -32,28 +57,3 @@ export function SetLastError(dwErrcode: number): void {
3257
export declare namespace SetLastError {
3358
export var fn: koffi.KoffiFunc<(dwErrcode: number) => void>
3459
}
35-
36-
/**
37-
* Formats a message string.
38-
*
39-
* Note: the Arguments parameter is not yet supported. Right now, you can use FormatMessage()
40-
* to retrieve the message text for a system-defined error returned by GetLastError().
41-
*
42-
* https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessagew
43-
*/
44-
export function FormatMessage(dwFlags: FORMAT_MESSAGE_ | number, lpSource: HMODULE | string | null, dwMessageId: number, dwLanguageId: number): string {
45-
FormatMessage.fn ??= kernel32.func('FormatMessageW', cDWORD, [ cDWORD, cLPCVOID, cDWORD, cDWORD, cLPWSTR, cDWORD, '...' as any ])
46-
47-
const out = new Uint16Array(2048)
48-
const len = FormatMessage.fn(
49-
dwFlags, lpSource, dwMessageId, dwLanguageId,
50-
out, out.length,
51-
'int', 0 // Fake va_list
52-
)
53-
return textDecoder.decode(out.slice(0, len))
54-
}
55-
56-
/** @internal */
57-
export declare namespace FormatMessage {
58-
export var fn: koffi.KoffiFunc<(dwFlags: number, lpSource: HMODULE | string | null, dwMessageId: number, dwLanguageId: number, lpBuffer: Uint16Array, nSize: number, ...args: any[]) => number>
59-
}

source/win32/kernel32/process.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,33 @@ export declare namespace CloseHandle {
2121
}
2222

2323
/**
24-
* Opens an existing local process object.
24+
* Retrieves a pseudo handle for the current process.
2525
*
26-
* https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocess
26+
* https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess
2727
*/
28-
export function OpenProcess(dwDesiredAccess: PSAR_ | number, bInheritHandle: boolean, dwProcessId: number): HANDLE | null {
29-
OpenProcess.fn ??= kernel32.func('OpenProcess', cHANDLE, [ cDWORD, cBOOL, cDWORD ])
30-
return OpenProcess.fn(dwDesiredAccess, Number(bInheritHandle), dwProcessId)
28+
export function GetCurrentProcess(): HANDLE {
29+
GetCurrentProcess.fn ??= kernel32.func('GetCurrentProcess', cHANDLE, [])
30+
return GetCurrentProcess.fn()
3131
}
3232

3333
/** @internal */
34-
export declare namespace OpenProcess {
35-
export var fn: koffi.KoffiFunc<(dwDesiredAccess: PSAR_ | number, bInheritHandle: number, dwProcessId: number) => HANDLE>
34+
export declare namespace GetCurrentProcess {
35+
export var fn: koffi.KoffiFunc<() => HANDLE>
3636
}
3737

3838
/**
39-
* Retrieves a pseudo handle for the current process.
39+
* Opens an existing local process object.
4040
*
41-
* https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess
41+
* https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocess
4242
*/
43-
export function GetCurrentProcess(): HANDLE {
44-
GetCurrentProcess.fn ??= kernel32.func('GetCurrentProcess', cHANDLE, [])
45-
return GetCurrentProcess.fn()
43+
export function OpenProcess(dwDesiredAccess: PSAR_ | number, bInheritHandle: boolean, dwProcessId: number): HANDLE | null {
44+
OpenProcess.fn ??= kernel32.func('OpenProcess', cHANDLE, [ cDWORD, cBOOL, cDWORD ])
45+
return OpenProcess.fn(dwDesiredAccess, Number(bInheritHandle), dwProcessId)
4646
}
4747

4848
/** @internal */
49-
export declare namespace GetCurrentProcess {
50-
export var fn: koffi.KoffiFunc<() => HANDLE>
49+
export declare namespace OpenProcess {
50+
export var fn: koffi.KoffiFunc<(dwDesiredAccess: PSAR_ | number, bInheritHandle: number, dwProcessId: number) => HANDLE>
5151
}
5252

5353
/**

source/win32/user32.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
export { user32 } from './user32/_lib.js'
2-
export * from './user32/class.js'
2+
export * from './user32/wndclass.js'
33
export * from './user32/cursor.js'
44
export * from './user32/icon.js'
55
export * from './user32/image.js'
66
export * from './user32/menu.js'
77
export * from './user32/message.js'
88
export * from './user32/messagebox.js'
9-
export * from './user32/point.js'
109
export * from './user32/window.js'

source/win32/user32/cursor.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { koffi } from '../private.js'
12
import {
23
cBOOL, cLPCWSTR, cHANDLE,
4+
cPOINT, type POINT,
35
type HINSTANCE, type HCURSOR
46
} from '../ctypes.js'
57
import type { IDC_ } from '../consts.js'
68
import { user32 } from './_lib.js'
7-
import type { koffi } from '../private.js'
89

910
/**
1011
* Destroys a cursor and frees any memory the cursor occupied.
@@ -21,6 +22,21 @@ export declare namespace DestroyCursor {
2122
export var fn: koffi.KoffiFunc<(hCursor: HCURSOR) => number>
2223
}
2324

25+
/**
26+
* Retrieves the cursor's position, in screen coordinates.
27+
*
28+
* https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getcursorpos
29+
*/
30+
export function GetCursorPos(lpPoint: POINT): number {
31+
GetCursorPos.fn ??= user32.func('GetCursorPos', cBOOL, [ koffi.out(cPOINT) ])
32+
return GetCursorPos.fn(lpPoint)
33+
}
34+
35+
/** @internal */
36+
export declare namespace GetCursorPos {
37+
export var fn: koffi.KoffiFunc<(lpPoint: POINT) => number>
38+
}
39+
2440
/**
2541
* Loads the specified cursor resource from the executable (.exe) file associated with an application instance.
2642
*

source/win32/user32/point.ts

-18
This file was deleted.

0 commit comments

Comments
 (0)