node-win32-api icon indicating copy to clipboard operation
node-win32-api copied to clipboard

compatible with regular JavaScript?

Open gittyup2018 opened this issue 3 years ago • 7 comments
trafficstars

Is this compatible with regular or plain JavaScript? or is there a plain JavaScript version? if so can you show an example because I'm not familiar with typescript, coffeescript, all the pointless JavaScript variations, etc. and I prefer normal / regular / original JavaScript.

gittyup2018 avatar Jul 13 '22 02:07 gittyup2018

Yes, it supports plan js.

  1. change import syntax to require()
  2. remove type def

simple example

import ref from 'ref-napi'
import { DModel as M } from 'win32-api'
import { Kernel32, User32 } from 'win32-api/promise'

const knl32 = Kernel32.load()
const user32 = User32.load()

const lpszClass = Buffer.from('guard64\0', 'ucs2')
const hInstanceBuffer = ref.alloc(W.HANDLE_PVOID)
const hInstanceAddr = ref.address(hInstanceBuffer)

await knl32.GetModuleHandleExW(0, lpszClass, hInstanceAddr)

to

const ref = require('ref-napi')
const { DModel as M } = require('win32-api')
const { Kernel32, User32  }  = require('win32-api/promise')  // <--- not sure this syntax

const knl32 = Kernel32.load()
const user32 = User32.load()

const lpszClass = Buffer.from('guard64\0', 'ucs2')
const hInstanceBuffer = ref.alloc(W.HANDLE_PVOID)
const hInstanceAddr = ref.address(hInstanceBuffer)

await knl32.GetModuleHandleExW(0, lpszClass, hInstanceAddr)

waitingsong avatar Jul 13 '22 03:07 waitingsong

const point = StructFactory<M.POINT>(DS.POINT)
point.x = 100

to

const point = StructFactory(DS.POINT)
point.x = 100

waitingsong avatar Jul 13 '22 03:07 waitingsong

Thanks for the quick reply!

I tried your code but I get the error:

App threw an error during load ReferenceError: W is not defined

const hInstanceBuffer = ref.alloc(W.HANDLE_PVOID)

I don't know if makes a difference but I'm trying to use it to make an electron app,

Also I downloaded your repo (to try typescript version) and tried to run "npm run bootstrap" but it could not find lerna command.

And I'm not too familiar with Win32 API but would this module let me send text to Notepad? Can you show a SendMessage example? In regular javascript please

gittyup2018 avatar Jul 14 '22 07:07 gittyup2018

try :

import {
  DModel as M,
  DTypes as W,
  DStruct as DS,
} from 'win32-api'
// to
const {
  DModel as M,
  DTypes as W,
  DStruct as DS,
} = require('win32-api')

waitingsong avatar Jul 14 '22 15:07 waitingsong

Also I downloaded your repo (to try typescript version) and tried to run "npm run bootstrap" but it could not find lerna command.

You can install lerna as global pkg

npm i -g lerna

waitingsong avatar Jul 14 '22 15:07 waitingsong

const { DModel as M, DTypes as W, DStruct as DS, } = require('win32-api')

as syntax is not supported

This works though :)

const ref = require('ref-napi') const { DModel,DTypes,DStruct} = require('win32-api') //const {DModel as M, DTypes as W,DStruct as DS} = require('win32-api')

const M=DModel; const W=DTypes; const DS=DStruct;

const { Kernel32, User32 } = require('win32-api/promise')

const knl32 = Kernel32.load() const user32 = User32.load()

const lpszClass = Buffer.from('guard64\0', 'ucs2') const hInstanceBuffer = ref.alloc(W.HANDLE_PVOID) const hInstanceAddr = ref.address(hInstanceBuffer)

async function main() { await knl32.GetModuleHandleExW(0, lpszClass, hInstanceAddr); }

Could you show me a send text to notepad example please?? that's all I really need to use this class for to hopefully replace AHK (autohotkey)I think it's the SendMessage (or PostMessage) API but not familiar with it.

gittyup2018 avatar Jul 15 '22 23:07 gittyup2018

see demo

waitingsong avatar Jul 17 '22 15:07 waitingsong