waitingsong / node-win32-api

win32 api
MIT License
430 stars 55 forks source link

compatible with regular JavaScript? #38

Closed gittyup2018 closed 2 months ago

gittyup2018 commented 2 years ago

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.

waitingsong commented 2 years ago

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 commented 2 years ago
const point = StructFactory<M.POINT>(DS.POINT)
point.x = 100

to

const point = StructFactory(DS.POINT)
point.x = 100
gittyup2018 commented 2 years ago

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

waitingsong commented 2 years ago

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 commented 2 years ago

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
gittyup2018 commented 2 years ago

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.

waitingsong commented 2 years ago

see demo