Jump to content

WM_COPYDATA return


Recommended Posts

Link to comment
Share on other sites

Link to comment
Share on other sites

It's has something to do with Random i think...

 

1 hour ago, Danyfirex said:

Try declaring  $stStr as Global on top of your script.

That's works, and also if you set it as Static in WM_COPYDATA.

Strange, but what is the explanation for this behaviour?

And also, why we need to add + 4 to the pointer?

Edited by MrCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

And should we use only + 1 for the length and not * 2 for wchar?

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Ptr($aRet[0] + 4)

should probably be 

 

Ptr($aRet[0] + DllStructGetSize(DllStructCreate('ptr')))

 

No Nevermind just realized that its to get rid of the junk at the beginning of the string

 

$stStr
Edited by Bilgus
Link to comment
Share on other sites

I think because the memory get free before reading. 

Just + 1 is ok becuase dllstructure manage size of wide string so it does *2 internally.

You add+4 to start reading after size field.

 

Link to comment
Share on other sites

Thanks.

How about adding x64 support?

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

 

3 minutes ago, Danyfirex said:

I think because the memory get free before reading.

I think so too, and I guess that's the reason why MSDN says: The receiving application should consider the data read-only. The lParam parameter is valid only during the processing of the message.

https://docs.microsoft.com/en-us/windows/win32/dataxchg/wm-copydata

May _WinAPI_SendMessageTimeout is the better option, as you can config it to wait for return.

Edited by KaFu
Link to comment
Share on other sites

On x64 the ReadProcessMemory fails...

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

It fails due to send message returns a hresult so you get your x64 ptr casted. So it would require some adjust maybe using  write memory from the callback.

Saludos

Link to comment
Share on other sites

2 hours ago, Danyfirex said:

it would require some adjust maybe using  write memory from the callback

I will very appreciate if you could show me how to do that.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

@MrCreatoR I meant something like this:

 

#AutoIt3Wrapper_UseX64=y
#include <WindowsConstants.au3>
#include <WinAPIProc.au3>
#include <WinAPIMem.au3>
#include <String.au3>

Global Const $tagCOPYDATASTRUCT = 'ulong_ptr dwData;' & _
        'dword cbData;' & _
        'ptr lpData'


Global $stStr = 0
Global $bExit = False
Global $hGUI

If $CmdLine[0] And $CmdLine[1] = '/Child' Then
    $hGUI = GUICreate('Child')
    GUIRegisterMsg($WM_COPYDATA, '_WM_COPYDATA')
    Do
        Sleep(10)
    Until $bExit
    Exit
EndIf

Run(@AutoItExe & ' /AutoIt3ExecuteScript "' & @ScriptFullPath & '" /Child')
$hChild = WinWait('[CLASS:AutoIt v3 GUI;TITLE:Child]')
Sleep(500) ; ...relax
$hTimer = TimerInit() ; ... timer
Local $sDataToSend= "A_" & _StringRepeat('Send', Random(10, 100)) & "_Y" 
$sRet = _Send($hChild,$sDataToSend)
$hTimer = Round(TimerDiff($hTimer), 5) ; ..time it took to return
MsgBox(64, 'Return - ' & $hTimer, $sRet)

Func _Send($hWnd,$sDataToSend)
    Local $tPtr = DllStructCreate("ptr Data") ;Pointer for reading vRet Value
    Local $iProcessID = @AutoItPID
    Local $tData = DllStructCreate("Ptr pReturn;long pid;wchar Data[" & (StringLen($sDataToSend) + 1) & "]")
    $tData.pReturn = DllStructGetPtr($tPtr)
    $tData.PID = $iProcessID ;Pid for write memory
    $tData.Data = $sDataToSend ;Data to Send

;~  ConsoleWrite("$tData.pid: " & $tData.PID & @CRLF)
;~  ConsoleWrite("$tData.Data: " & $tData.Data & @CRLF)
    Local $tCopyData = DllStructCreate($tagCOPYDATASTRUCT)
    $tCopyData.cbData = DllStructGetSize($tData)
    $tCopyData.lpData = DllStructGetPtr($tData)
    Local $aRet = DllCall('user32.dll', 'lresult', 'SendMessage', 'hwnd', $hWnd, 'uint', $WM_COPYDATA, 'ptr', 0, 'ptr', DllStructGetPtr($tCopyData))
;~  ConsoleWrite("$tData.pReturn: " & $tData.pReturn & @CRLF)
;~  ConsoleWrite("$tPtr.Data: " & $tPtr.Data & @CRLF)
    Local $tSize = _WinAPI_ReadProcessMemoryEx(WinGetProcess($hWnd), Ptr($tPtr.Data), 'long iSize')
    Local $stStr = _WinAPI_ReadProcessMemoryEx(WinGetProcess($hWnd), Ptr($tPtr.Data + 4), 'wchar sStr[' & $tSize.iSize & ']')

    Return DllStructGetData($stStr, 'sStr')
EndFunc   ;==>_Send

Func _WM_COPYDATA($hWnd, $iMsg, $wParam, $lParam)
    $bExit = True
    Local $vRet = "A_" & _StringRepeat('Return', Random(10, 100)) & "_Y" ; Return
    Local $iLen = (StringLen($vRet) + 1)
    Local $tCopyData = DllStructCreate($tagCOPYDATASTRUCT, $lParam)
    Local $tData = DllStructCreate("Ptr pReturn;long PID;wchar Data[" & ($tCopyData.cbData - (@AutoItX64 ? 12 : 8)) & "]", $tCopyData.lpData)
    $stStr = DllStructCreate('long iSize;wchar sStr[' & $iLen & ']')
    DllStructSetData($stStr, 'iSize', $iLen)
    DllStructSetData($stStr, 'sStr', $vRet)
    Return _WinAPI_WriteProcessMemoryPtr($tData.PID, $tData.pReturn, DllStructGetPtr($stStr))
EndFunc   ;==>_WM_COPYDATA


Func _WinAPI_WriteProcessMemoryPtr($iProcessID, $pPointer, $ptPtr)
    Local $iSYNCHRONIZE = (0x00100000), $iSTANDARD_RIGHTS_REQUIRED = (0x000F0000)
    Local $iPROCESS_ALL_ACCESS = ($iSTANDARD_RIGHTS_REQUIRED + $iSYNCHRONIZE + 0xFFF)
    Local $hProcess, $Struct, $StructPtr, $StructSize, $iWriten

    $hProcess = _WinAPI_OpenProcess($iPROCESS_ALL_ACCESS, False, $iProcessID)
    If @error Then Return SetError(@error, 1, $Struct)

    Local $Struct = DllStructCreate("ptr Data")
    $StructSize = DllStructGetSize($Struct)
    $StructPtr = DllStructGetPtr($Struct)
    $Struct.Data = $ptPtr

    Local $bWrite = _WinAPI_WriteProcessMemory($hProcess, $pPointer, $StructPtr, $StructSize, $iWriten)
    _WinAPI_CloseHandle($hProcess)

    Return SetError(@error, $iWriten, $bWrite)
EndFunc   ;==>_WinAPI_WriteProcessMemoryPtr



Func _WinAPI_ReadProcessMemoryEx($iProcessID, $pPointer, $sStructTag)
    Local $iSYNCHRONIZE = (0x00100000), $iSTANDARD_RIGHTS_REQUIRED = (0x000F0000)
    Local $iPROCESS_ALL_ACCESS = ($iSTANDARD_RIGHTS_REQUIRED + $iSYNCHRONIZE + 0xFFF)
    Local $hProcess, $Struct, $StructPtr, $StructSize, $Read

    $hProcess = _WinAPI_OpenProcess($iPROCESS_ALL_ACCESS, False, $iProcessID)
    If @error Then Return SetError(@error, 1, $Struct)

    $Struct = DllStructCreate($sStructTag)
    $StructSize = DllStructGetSize($Struct)
    $StructPtr = DllStructGetPtr($Struct)

    _WinAPI_ReadProcessMemory($hProcess, $pPointer, $StructPtr, $StructSize, $Read)
    _WinAPI_CloseHandle($hProcess)

    Return SetError(@error, $Read, $Struct)
EndFunc   ;==>_WinAPI_ReadProcessMemoryEx

Saludos

 

Edited by Danyfirex
Edit
Link to comment
Share on other sites

12 hours ago, Danyfirex said:

I meant something like this:

Thank you very much!🍻

If by any chance you will visit Israel some day, beer evening on me!

Just one small question, $iPROCESS_ALL_ACCESS can be replaced with $PROCESS_ALL_ACCESS from APIConstants.au3?

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

You're welcome. I think yes compare if they have same value. I'm away of computer for testing.

PD: haha I'll PM you when I travel to Israel 😂 maybe I can do it in my dreams meanwhile 😁😄

 

Saludos

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...