across processes WM_COPYDATA can only use 0x4a Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
viv
Posts: 219
Joined: 09 Dec 2020, 17:48

across processes WM_COPYDATA can only use 0x4a

22 Jan 2024, 04:15

I want to send text messages across processes.
I chose this method. https://www.autohotkey.com/docs/v2/lib/OnMessage.htm#ExSendString

When across processes, it can only use 0x4a
If change 0x4a, it will cause error

In same process,the msg number can change and work fine
copy Sender.ahk into Receiver.ahk then change 0x004A

so across processes WM_COPYDATA can only use 0x4a?

[Mod edit: Moved topic from 'Scripts and Functions (v2)' since this is a help request. ]
just me
Posts: 9542
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: across processes WM_COPYDATA can only use 0x4a

22 Jan 2024, 04:22

0x004A is the OS defined numeric identifier of the WM_COPYDATA message.

WinUser.h:

Code: Select all

#define WM_COPYDATA                     0x004A
viv
Posts: 219
Joined: 09 Dec 2020, 17:48

Re: across processes WM_COPYDATA can only use 0x4a

22 Jan 2024, 04:42

@just me

I know that
but same process,the 0x004A can change and work fine
So the WM_COPYDATA maybe not need 0x4a?

why across processes change 0x004A will not work?
User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: across processes WM_COPYDATA can only use 0x4a

22 Jan 2024, 04:54

 OnMessage
MsgNumber wrote:The number of the message to monitor or query, which should be between 0 and 4294967295 (0xFFFFFFFF). If you do not wish to monitor a system message (that is, one below 0x0400), it is best to choose a number greater than 4096 (0x1000) to the extent you have a choice. This reduces the chance of interfering with messages used internally by current and future versions of AutoHotkey.
There are areas where users can specify themselves, and there are areas with established agreements between processes.
WM_COPYDATA clearly belongs to the latter category. All processes are designed to use 0x004A for such purposes.
Each process can have only one callback for 0x004A, so if there is a need to distinguish between processes when receiving a callback, consider using wParam appropriately.
In the official example, 0 is being passed, but in reality, you can pass any arbitrary value.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
just me
Posts: 9542
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: across processes WM_COPYDATA can only use 0x4a  Topic is solved

22 Jan 2024, 04:59

Because any message number other than 0x004A will not be recognized and handled as WM_COPYDATA by the system.

Using WM_COPYDATA to marshal message parameters since the window manager otherwise doesn’t know how
viv
Posts: 219
Joined: 09 Dec 2020, 17:48

Re: across processes WM_COPYDATA can only use 0x4a

22 Jan 2024, 05:09

@just me
Thx
I may understand
In across processes, use 0x4a system may process the data for read
In same processe, use other msg number system my not process the date
But it in same processe, so the data can be read

@Seven0528

COPYDATASTRUCT first parameter can also define use for
https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-copydatastruct
User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: across processes WM_COPYDATA can only use 0x4a

22 Jan 2024, 05:19

 @viv
I was trying to talk about the purpose of SendMessageTimeout's wParam, not the first dwData of COPYDATASTRUCT...
Ok. I had no idea what you were asking. I'm done. Sorry.
Last edited by Seven0528 on 22 Jan 2024, 06:36, edited 1 time in total.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
viv
Posts: 219
Joined: 09 Dec 2020, 17:48

Re: across processes WM_COPYDATA can only use 0x4a

22 Jan 2024, 05:31

@Seven0528
WM_COPYDATA is a msg id,it not a data
COPYDATASTRUCT is what WM_COPYDATA to send

you say wParam to distinguish between processes
I say in the data to send can define usefor
it a better way to distinguish userfor not by processes
User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: across processes WM_COPYDATA can only use 0x4a

22 Jan 2024, 05:36

 I am well aware of that. (I frequently use WM_COPYDATA.)
Could you provide a bit more detail about your question?
No matter how many times I read it, I couldn't grasp what you were asking or how @just me response addressed your curiosity.
Do you want to send some data within the same process? Why would you want to do that...?
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
viv
Posts: 219
Joined: 09 Dec 2020, 17:48

Re: across processes WM_COPYDATA can only use 0x4a

22 Jan 2024, 05:49

@Seven0528
in same process is to test it can work fine
it just a test,not the final usefor

In across processes,generally memory can't read each other.
I didn't realize that.
so i change the msg number the send the same memory(data)
it not work

@just me tell me send data with 0x4a
system may process the memory for read in diffcult processe
Descolada
Posts: 1183
Joined: 23 Dec 2021, 02:30

Re: across processes WM_COPYDATA can only use 0x4a

22 Jan 2024, 05:58

@viv WM_COPYDATA is the name for the value 0x004A. If you use any other number than that then it is NOT WM_COPYDATA. For example 0x0201 is WM_LBUTTONDOWN.
WM_COPYDATA is special in the way that @just me described, in the sense that the data is marshaled. That means the receiving process gets a copy of the data contained in COPYDATASTRUCT, but not the original data itself. If you send COPYDATASTRUCT without WM_COPYDATA then the memory addresses will be invalid: the receiving process would need to use ReadProcessMemory instead to access the data. If you are sending data inside the same process (for some unknown reason... you should never need that in AHK) then you could indeed use a custom message number created by RegisterWindowMessage, since the address space will be the same inside one process and the data doesn't need to be marshaled to be read by normal means (eg NumGet, StrGet).
I've outlined other ways to send data as well in my tutorial about IPC.
User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: across processes WM_COPYDATA can only use 0x4a

22 Jan 2024, 06:31

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force


;  1.
F1::  {
    SendBuffer("Hello", A_ScriptName " ahk_class AutoHotkey")
}
OnMessage 0x7000, ReceiveBuffer
ReceiveBuffer(wParam, lParam, msg, hwnd)
{
    StringAddress := NumGet(lParam, 2*A_PtrSize, "Ptr")
    CopyOfData := StrGet(StringAddress)
    ToolTip A_ScriptName "`n" CopyOfData
    return true
}
SendBuffer(StringToSend, TargetScriptTitle)
{
    CopyDataStruct := Buffer(3*A_PtrSize)
    SizeInBytes := (StrLen(StringToSend) + 1) * 2
    NumPut( "Ptr", SizeInBytes
          , "Ptr", StrPtr(StringToSend)
          , CopyDataStruct, A_PtrSize)
    Prev_DetectHiddenWindows := DetectHiddenWindows(True)
    Prev_TitleMatchMode := SetTitleMatchMode(2)
    RetValue := SendMessage(0x7000, 0, CopyDataStruct,, TargetScriptTitle,,,, 4000)
    DetectHiddenWindows Prev_DetectHiddenWindows
    SetTitleMatchMode Prev_TitleMatchMode
    return RetValue
}


;  2.
F2::  {
    static Str:="Hello" ;  https://www.autohotkey.com/docs/v2/lib/StrPtr.htm#Remarks
    SetData(&Str, &DataStruct)
    GetData(&DataStruct)
}
SetData(&Str, &DataStruct)    {
    DataStruct := Buffer(3*A_PtrSize)
    SizeInBytes := (StrLen(Str) + 1) * 2
    NumPut( "Ptr", SizeInBytes
          , "Ptr", StrPtr(Str)
          , DataStruct, A_PtrSize)
}
GetData(&DataStruct)    {
    StringAddress := NumGet(DataStruct, 2*A_PtrSize, "Ptr")
    Data := StrGet(StringAddress)
    ToolTip A_ScriptName "`n" Data
}



F5::Run A_ScriptFullPath
 @viv
Ah, I now understand your situation.
It seems like you were NOT attempting to use WM_COPYDATA.
Instead, you just passed the address of a Buffer to yourself using SendMessage and OnMessage.
Last edited by Seven0528 on 22 Jan 2024, 06:38, edited 1 time in total.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
viv
Posts: 219
Joined: 09 Dec 2020, 17:48

Re: across processes WM_COPYDATA can only use 0x4a

22 Jan 2024, 06:36

@Descolada

Thx.
It's name is WM_COPYDATA
so it is a copy
I change the 0x4a
so it not a copy....
I understand

I just test, In across processes
lParam's memory address is changed
User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: across processes WM_COPYDATA can only use 0x4a

22 Jan 2024, 07:00

 @viv
Your words are correct.
However, that doesn't mean WM_COPYDATA is impossible within the same process. It's just useless...
I'm glad to be of help!

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
OnMessage 0x004A, ReceiveBuffer ;  WM_COPYDATA
OnMessage 0x7000, ReceiveBuffer ;  The Msg value set arbitrarily by the user


F2::SendBuffer(0x004A, "Hello", A_ScriptName " ahk_class AutoHotkey")
F3::SendBuffer(0x7000, "Hello", A_ScriptName " ahk_class AutoHotkey")
    

ReceiveBuffer(wParam, lParam, msg, hwnd)
{
    StringAddress := NumGet(lParam, 2*A_PtrSize, "Ptr")
    CopyOfData := StrGet(StringAddress)
    Msgbox "Msg`t`t:  " format("0x{:04X}",msg) "`nReceiver Address`t:  " lParam
    return true
}
SendBuffer(msg, StringToSend, TargetScriptTitle)
{
    CopyDataStruct := Buffer(3*A_PtrSize)
    SizeInBytes := (StrLen(StringToSend) + 1) * 2
    NumPut( "Ptr", SizeInBytes
          , "Ptr", StrPtr(StringToSend)
          , CopyDataStruct, A_PtrSize)
    Prev_DetectHiddenWindows := DetectHiddenWindows(True)
    Prev_TitleMatchMode := SetTitleMatchMode(2)
    RetValue := SendMessage(msg, 0, CopyDataStruct,, TargetScriptTitle,,,, 4000)
    Msgbox "Msg`t`t:  " format("0x{:04X}",msg) "`nSender Address`t:  " CopyDataStruct.Ptr
    DetectHiddenWindows Prev_DetectHiddenWindows
    SetTitleMatchMode Prev_TitleMatchMode
    return RetValue
}
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
viv
Posts: 219
Joined: 09 Dec 2020, 17:48

Re: across processes WM_COPYDATA can only use 0x4a

22 Jan 2024, 07:10

@Seven0528

i dont say it not change in same processe
WM_COPYDATA client -> system -> system return a copy -> client
usermsg client -> system -> client
all msg need system forward

by the way
you can use A_ScriptHwnd instead A_ScriptName " ahk_class AutoHotkey"

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Descolada, teadrinker and 35 guests