Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/deskbtm/win32-ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
nawbc committed Jun 27, 2023
2 parents bc1fd97 + 90519dd commit f5bf1e0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 57 deletions.
85 changes: 42 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
## win32-ffi

win32 api binding for js/ts that powered by node-ffi
win32 api binding for nodejs

### Install
## Install

`npm i win32-ffi`/`yarn add win32-ffi`

### Tutorial
## Usage

<br>

Expand Down Expand Up @@ -36,34 +36,33 @@ ffi.Callback(CPP.LRESULT, [CPP.INT, CPP.WPARAM, ref.refType(CPP.MOUSEHOOKSTRUCT)

---

#### Electron
### Electron

```ts

// get system endianness, cast Buffer to decimal
const bufferCastInt32 = function (buf: Buffer): number {
return os.endianness() == "LE" ?
buf.readInt32LE() : buf.readInt32BE();
return os.endianness() == "LE" ? buf.readInt32LE() : buf.readInt32BE();
};

const mainWindow = new BrowserWindow({
frame: false,
transparent: true,
webPreferences: {
nodeIntegration: true,
webSecurity: false,
webviewTag: true,
sandbox: false,
enableRemoteModule: true
}
frame: false,
transparent: true,
webPreferences: {
nodeIntegration: true,
webSecurity: false,
webviewTag: true,
sandbox: false,
enableRemoteModule: true
}
});

const decimalHwnd = bufferCastInt32(mainWindow.getNativeWindowHandle());
SetWindowPos(decimalHwnd, 0, 0, 0, 100, 100, CPP.SWP_NOZORDER);

```

#### Samples
### Samples

- Create thread

Expand All @@ -73,7 +72,7 @@ const { Win32ffi, ffi, CPP, L, NULL } = require('win32-ffi');
const { CreateThread, MessageBoxW } = new Win32ffi().winFns();

const proc = ffi.Callback(CPP.INT, [CPP.PVOID], () => {
MessageBoxW(0, L("exmpale"), null, CPP.MB_OK | CPP.MB_ICONEXCLAMATION);
MessageBoxW(0, L("exmpale"), null, CPP.MB_OK | CPP.MB_ICONEXCLAMATION);
});

CreateThread(null, 0, proc, NULL, 0, NULL);
Expand All @@ -83,27 +82,27 @@ CreateThread(null, 0, proc, NULL, 0, NULL);

```ts
const _createMouseHookProc = () => ffi.Callback(CPP.LRESULT, [CPP.INT, CPP.WPARAM, ref.refType(CPP.StructMOUSEHOOKSTRUCT)],
(nCode: TS.INT, wParam: TS.WPARAM, lParam: TS.RefStruct) => {
const mouse: TS.MOUSEHOOKSTRUCT = lParam.deref();
const pt = mouse.pt;
const { x, y } = pt;
const currentHwnd = WindowFromPoint(mouse.pt);

return CallNextHookEx(0, nCode, wParam, lParam); // need overwrite
}
)
const _mouseHook = SetWindowsHookExW(CPP.WH_MOUSE_LL, this._createMouseHookProc(), 0, 0);
const msg: TS.RefStruct = new CPP.StructMSG();
while (GetMessageW(msg.ref(), 0, 0, 0) && this._trigger) {
TranslateMessage(msg.ref());
DispatchMessageW(msg.ref());
(nCode: TS.INT, wParam: TS.WPARAM, lParam: TS.RefStruct) => {
const mouse: TS.MOUSEHOOKSTRUCT = lParam.deref();
const pt = mouse.pt;
const { x, y } = pt;
const currentHwnd = WindowFromPoint(mouse.pt);
return CallNextHookEx(0, nCode, wParam, lParam); // need overwrite
}
)

UnhookWindowsHookEx(_mouseHook);
const _mouseHook = SetWindowsHookExW(CPP.WH_MOUSE_LL, this._createMouseHookProc(), 0, 0);
const msg: TS.RefStruct = new CPP.StructMSG();

while (GetMessageW(msg.ref(), 0, 0, 0) && this._trigger) {
TranslateMessage(msg.ref());
DispatchMessageW(msg.ref());
}

UnhookWindowsHookEx(_mouseHook);
```

#### More Examples
### More Examples

**[Exmaples](./example)**

Expand All @@ -123,8 +122,8 @@ const winFns = win32ffi.winFns(); // include user32 and kernel32

const _createEnumWindowProc = () => ffi.Callback(CPP.BOOL, [CPP.HWND, CPP.LPARAM],
(hWnd: TS.HWND) => {
......
return true;
......
return true;
});

winFns.EnumWindows(this._createEnumWindowProc(), 0);
Expand All @@ -148,12 +147,12 @@ import {ref, StructType,CPP} from 'win32-ffi';
const Struct = StructType(ref);

const MSG = Struct({
hwnd: CPP.HWND,
message: CPP.UINT,
wParam: CPP.WPARAM,
lParam: CPP.LPARAM,
time: CPP.DWORD,
pt: CPP.POINT
hwnd: CPP.HWND,
message: CPP.UINT,
wParam: CPP.WPARAM,
lParam: CPP.LPARAM,
time: CPP.DWORD,
pt: CPP.POINT
});
const msg = new MSG();
console.log(msg.ref());
Expand All @@ -171,7 +170,7 @@ overwrite.ts

import { CPP, ref } from 'win32-ffi';
export const customFns = {
CallNextHookEx: [CPP.LRESULT, [CPP.HHOOK, CPP.INT, CPP.WPARAM, ref.refType(CPP.MOUSEHOOKSTRUCT)]]
CallNextHookEx: [CPP.LRESULT, [CPP.HHOOK, CPP.INT, CPP.WPARAM, ref.refType(CPP.MOUSEHOOKSTRUCT)]]
};
```

Expand All @@ -185,6 +184,6 @@ Win32ffi.assign({ user32Fns: customFns });
* win32-ffi does not include Comctl32, because almost all macro-defined functions, but you can use `SendMessage` to achieve, win32ffi provides
* **It is impossible to include all win32 api. If there is no prompt, it means you need to define it by yourself. And you can get type details from visual studio.**

# License
## License

The scripts and documentation in this project are released under the [Apache 2.0 License](./LICENSE)
13 changes: 2 additions & 11 deletions example/create-window.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
const { Win32ffi, L, CPP, StructType, ref, NULL } = require('../dist');
const { Win32ffi, L, CPP, NULL } = require('../dist');

const winFns = new Win32ffi().winFns();

const { CreateWindowExW, GetMessageW, TranslateMessage, DispatchMessageW, GetModuleHandleW } = winFns;
const sampleName = L('Sample Window Name\0');
const sampleText = L('description\0');

const tagWNDCLASSSTRUCT = {
lpszClassName: CPP.LPCWSTR,
};

const Struct = StructType(ref);

const WNDCLASSSTRUCT = 'pointer';
const StructWNDCLASSSTRUCT = Struct(tagWNDCLASSSTRUCT);

const WS_OVERLAPPED = 0x00000000;
const WS_CAPTION = 0x00c00000;
const WS_SYSMENU = 0x00080000;
Expand All @@ -28,7 +19,7 @@ const WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFR

const hInst = GetModuleHandleW(NULL);

const hWnd = CreateWindowExW(
CreateWindowExW(
0,
sampleName,
sampleText,
Expand Down
4 changes: 1 addition & 3 deletions example/mouse-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const cb = function (nCode, wParam, lParam) {
};

const _createMouseHookProc = ffi.Callback(
CPP.LRESULT, // 相当于C++ 的CALLBACK
CPP.LRESULT, // cxx CALLBACK
[CPP.INT, CPP.WPARAM, ref.refType(CPP.StructMOUSEHOOKSTRUCT)],
cb
);
Expand All @@ -33,11 +33,9 @@ const hInst = GetModuleHandleW(NULL);
const _mouseHook = SetWindowsHookExW(CPP.WH_MOUSE_LL, _createMouseHookProc, hInst, 0);
const msg = new CPP.StructMSG();

// 创建消息循环
while (GetMessageW(msg.ref(), 0, 0, 0)) {
TranslateMessage(msg.ref());
DispatchMessageW(msg.ref());
}

// 释放hook
UnhookWindowsHookEx(_mouseHook);

0 comments on commit f5bf1e0

Please sign in to comment.