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

How to send WM_COPYDATA message with a string

Open TerryChan opened this issue 4 years ago • 7 comments

With node-win32-api, I tried to send a WM_COPYDATA message to another win32 application with window, but it failed, the win32 application didn't receive the messsage. But when I send WM_QUIT and WM_COMMAND mesage, it worked very well image

And when I used another win32 applciation to send the WM_COPYDATA message, it can received the message and the string send WM_COPYDATA : image received the message: image

Do you know how to send WM_COPYDATA with a string by node-win32-api? Or do you know is there any other method that I can send a string to another win32 application in NodeJS? Thank you in advance.

TerryChan avatar Apr 02 '20 16:04 TerryChan

Is it working?

rest = user32.sendMessageW(hWnd, 74, 100, Buffer.from([100]))
// or
rest = user32.sendMessageW(hWnd, 74, 100, Buffer.from(['foo\0', 'ucs2']))

waitingsong avatar Apr 03 '20 01:04 waitingsong

@waitingsong No ,they doesn't work. image Looks like the type of the argument is not correct

TerryChan avatar Apr 03 '20 06:04 TerryChan

@TerryChan Would you like to provide the code.

waitingsong avatar Apr 04 '20 07:04 waitingsong

Here is the code:

var { Kernel32, User32 }  = require('win32-api')
// import * as ref from 'ref-napi'

const knl32 = Kernel32.load()
const user32 = User32.load()  // load all apis defined in lib/{dll}/api from user32.dll

const title = 'WindowsDesktop\0'    // null-terminated string

const lpszWindow = Buffer.from(title, 'ucs2')
const hWnd = 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('window handle: ', hWnd)

  // Send WM_QUIT message, success
  // res = user32.SendMessageW(hWnd, 2, 0, 0)

  // Send WM_COMMAND message, success
  res = user32.SendMessageW(hWnd, 273, 1000, 3)
  console.log('WM_COMMAND', res)

  // send WM_COPYDATA (74) message, fail
  res = user32.PostMessageW(hWnd, 74, 100, 100)
  // rest = user32.SendMessageW(hWnd, 74, 100, Buffer.from([100]))
  // rest = user32.SendMessageW(hWnd, 74, 100, Buffer.from(['foo\0', 'ucs2']))
  // res = user32.PostMessageW(hWnd, 74, 100, 100)

  console.log('WM_COPYDATA', res)
}

TerryChan avatar Apr 07 '20 06:04 TerryChan

pls try this demo with version ^9.4.0

waitingsong avatar Jun 07 '20 21:06 waitingsong

Is there any way to receive WM_COPYDATA?

browserWindow.hookWindowMessage(0x4A /* = WM_COPYDATA */, (wParam, lParam) => {
                console.log('wParam = ', wParam);
                console.log('lParam = ', lParam);
            });

only sth like

wParam =  <Buffer@0x0000018C305F7160 00 00 00 00 00 00 00 00>
lParam =  <Buffer@0x0000018C305F6EC0 a0 f2 cf 42 8a 00 00 00>

How to get the data from WM_COPYDATA?

scil avatar May 05 '21 13:05 scil

Is there any way to receive WM_COPYDATA?

browserWindow.hookWindowMessage(0x4A /* = WM_COPYDATA */, (wParam, lParam) => {
                console.log('wParam = ', wParam);
                console.log('lParam = ', lParam);
            });

only sth like

wParam =  <Buffer@0x0000018C305F7160 00 00 00 00 00 00 00 00>
lParam =  <Buffer@0x0000018C305F6EC0 a0 f2 cf 42 8a 00 00 00>

How to get the data from WM_COPYDATA?

retrieveStructFromPtrAddress() may help you: demo

waitingsong avatar Jul 17 '22 15:07 waitingsong