waitingsong / node-win32-api

win32 api
MIT License
451 stars 56 forks source link

TypeScript Compile Error #29

Closed BobbyShoe closed 5 months ago

BobbyShoe commented 3 years ago

Hello,

I am getting this error when trying to use the win32-api in a typescript file and then compiling it with tsc.

"node_modules/win32-def/dist/lib/ffi.model.d.ts:26:18 - error TS2430: Interface 'FFIBuffer' incorrectly extends interface 'Buffer'. The types of 'ref(...).readObject' are incompatible between these types. Type '(offset?: number | undefined) => string' is not assignable to type '(buffer: Buffer, offset?: number | undefined) => Object'. Types of parameters 'offset' and 'buffer' are incompatible. Type 'Buffer' is not assignable to type 'number'.

26 export interface FFIBuffer extends Buffer {"

Below is my typescript class. I was basically just trying to run the sample to rename the calculator app. Any suggestions?

Thanks, Bobby

import { FModel, K, U } from 'win32-api' import { HANDLE } from 'win32-def/dist/lib/win.model';

export class WinAutomationService { private knl32: FModel.ExpandFnModel<K.Win32Fns>; private user32: FModel.ExpandFnModel<U.Win32Fns>;

constructor() {
    this.knl32 = K.load();
    this.user32 = U.load();
}

public setTitle() {
    const title: string = 'Calculator\0';

    const lpszWindow = Buffer.from(title, 'ucs2');
    const hWnd: HANDLE = this.user32.FindWindowExW(0, 0, null, lpszWindow);

    if (typeof hWnd === 'number' && hWnd > 0
        || typeof hWnd === 'bigint' && hWnd > 0
        || typeof hWnd === 'string' && hWnd.length > 0) {
        console.log('buf: ', hWnd)

        // Change title of the Calculator
        const res = this.user32.SetWindowTextW(hWnd, Buffer.from('Node-Calculator\0', 'ucs2'))

        if (!res) {
            console.log('SetWindowTextW failed')
        }
        else {
            console.log('window title changed')
        }
    }
}

}

Also here are all the node modules in my project if there happens to be a conflict here

+-- @electron-forge/cli@6.0.0-beta.54 +-- @electron-forge/maker-deb@6.0.0-beta.54 +-- @electron-forge/maker-rpm@6.0.0-beta.54 +-- @electron-forge/maker-squirrel@6.0.0-beta.54 +-- @electron-forge/maker-zip@6.0.0-beta.54 +-- @types/ffi@0.2.3 +-- @types/node-static@0.7.5 +-- asar@3.0.3 +-- electron@11.0.3 +-- electron-builder@22.9.1 +-- electron-squirrel-startup@1.0.0 +-- node@15.4.0 +-- node-static@0.7.11 +-- webpack@5.10.3 +-- webpack-cli@4.2.0 +-- win32-api@9.6.0 `-- win32-def@9.4.0

waitingsong commented 3 years ago

pls try this:

import { K, U } from 'win32-api'

const knl32 = K.load()
const user32 = U.load()

export class WinAutomationService {
  private knl32: typeof knl32;
  private user32: typeof user32;

  constructor() {
    this.knl32 = knl32;
    this.user32 = user32;
  }
  // ....
}

AND this:

const hWnd: HANDLE = this.user32.FindWindowExW(0, 0, null, lpszWindow); // wrong parameter order
const hWnd: HANDLE = this.user32.FindWindowExW(0, 0, lpszWindow, null); // correct
justanotheranonymoususer commented 3 years ago

I have the same problem. @BobbyShoe, did you find a solution?

waitingsong commented 5 months ago

Types updated