News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Interprocess communication

Started by Mikl__, August 18, 2022, 11:29:24 AM

Previous topic - Next topic

Greenhorn

Yes, now it works. But I have to run 14r.exe first so that it starts 14s.exe to get it working.  :eusa_dance:
Kole Feut un Nordenwind gift en krusen Büdel un en lütten Pint.

Mikl__

jj2007
Va bene!


Greenhorn
You can first run 14s.exe, and then 14r.exe, they only work in pairs.

Greenhorn

Quote from: Mikl__ on October 03, 2022, 11:24:12 PM
jj2007
Va bene!


Greenhorn
You can first run 14s.exe, and then 14r.exe, they only work in pairs.

If I do so I get an exception, no matter which "send" button I press.  :sad:
Kole Feut un Nordenwind gift en krusen Büdel un en lütten Pint.

Mikl__

QuoteIf I do so I get an exception, no matter which "send" button I press.
The more such messages I receive, the faster I will correct errors.

Greenhorn

I have to fix the 'Just In Time Debugger' issue, then I can tell you more.

But at the moment I'm cooking pears, beans and bacon ... (yummy)
Kole Feut un Nordenwind gift en krusen Büdel un en lütten Pint.

Mikl__

QuoteBut at the moment I'm cooking pears, beans and bacon ... (yummy)

I know



TimoVJL

Quote from: Greenhorn on October 03, 2022, 11:22:28 PM
Yes, now it works. But I have to run 14r.exe first so that it starts 14s.exe to get it working.  :eusa_dance:
only this way apps works
May the source be with you

Mikl__


Greenhorn

Quote from: TimoVJL on October 03, 2022, 11:56:18 PM
Quote from: Greenhorn on October 03, 2022, 11:22:28 PM
Yes, now it works. But I have to run 14r.exe first so that it starts 14s.exe to get it working.  :eusa_dance:
only this way apps works
Quote from: Mikl__ on October 03, 2022, 11:58:46 PM
Thank you, Timo

OK, got it. Thanks.


Quote from: Mikl__ on October 03, 2022, 11:43:16 PM
QuoteBut at the moment I'm cooking pears, beans and bacon ... (yummy)

I know
Kole Feut un Nordenwind gift en krusen Büdel un en lütten Pint.

Mikl__

Hi All!
Does anyone have a working example of sending a text string for illustrating interprocess communication via OLE? Many thanks in advance!

jj2007

Do you mean OLE, RPC or DDE?

include \masm32\MasmBasic\MasmBasic.inc
  Init
  SendWordCommands ; prepare a DDE session; Word must be running
  .if eax
SendWordCommands '[FileNewDefault:InsertFile "\Masm32\include\Windows.inc"]'
SendWordCommands '[MsgBox "Cute"]'
  .else
MsgBox 0, "MS Word doesn't answer", "Sorry", MB_OK
  .endif
  SendWordCommands exit ; finish DDE session
EndOfCode

Mikl__

Ciao jj!
Grazie mille per l'esempio!
I mean COM, not DDE

TimoVJL

Using COM for IPC
https://learn.microsoft.com/en-us/windows/win32/ipc/interprocess-communications#using-com-for-ipc
https://stackoverflow.com/questions/31096671/how-to-use-com-to-transfer-data-between-two-processes-for-ipc-inter-process-c
May the source be with you

Mikl__


Mikl__

#59
DLL
An important feature of the use of dynamic libraries is the possibility of simultaneous use of one library by several modules. A dynamic library becomes an extension of the application that launched it, being loaded into the address space of the process. The data of one process is available from the dynamic library, and the data of the dynamic library is available to another process. When linking a dynamic library, the data segment is declared as a shared section. When you run an application that will load a dynamic link library, this area of memory will be shared by all applications.



Transferring text, pictures and music between apps is almost instantaneous!

Server application text (15s.asm); GUI #
include win64a.inc
includelib IPC.lib
ExeToDll PROTO :qword,:dword,:dword
DllToExe PROTO :qword,:dword,:dword
ID_TXT equ 100
ID_SEND_TXT equ 101
ID_SEND_ICO equ 102
ID_SEND_WAV equ 103
IDC_DIALOG equ 200
IDC_ICON1 equ 500
IDC_IMG1 equ 104
.code
WinMain proc ;dummy:qword
enter 30h,0
mov r9d,256
mov [rbp-10h],r9
and     qword ptr [rbp-8],LR_DEFAULTCOLOR
invoke LoadImageA,IMAGE_BASE,IDC_ICON1,IMAGE_ICON;,0,0,LR_DEFAULTCOLOR
mov r9d,offset DialogProc
mov [rbp-10h],rax
invoke DialogBoxParamA,IMAGE_BASE,IDC_DIALOG,HWND_DESKTOP
invoke RtlExitUserProcess,NULL
WinMain endp
DialogProc proc hWnddlg:QWORD,uMsg:QWORD,wParam:QWORD,lParam:QWORD
local buffer:qword
local cdata:COPYDATASTRUCT
local FSize:dword
local hFile:dword
local p:qword
local szReadWrite:qword ;number of bytes actually read or write
local hResource:qword
local pResource:qword
local temp:qword
mov hWnddlg,rcx
mov lParam,r9
cmp edx,WM_CLOSE
je wmCLOSE
cmp edx,WM_COMMAND
je wmCOMMAND
cmp edx,WM_INITDIALOG
        jne wmBYE
wmINITDIALOG:invoke GetDlgItem,,IDC_IMG1
invoke SendMessageA,eax,STM_SETIMAGE,IMAGE_ICON,lParam
jmp wmBYE
wmSEND_TXT:invoke GlobalAlloc,GPTR,256
mov buffer,rax
invoke  GetDlgItemTextA,hWnddlg,ID_TXT,eax,256
        or      eax,eax
        jz      wmBYE
        inc     eax
        mov FSize,eax
        invoke ExeToDll,buffer,eax,CF_TEXT
; Найти окно получателя
mov edx,offset szWin
        invoke  FindWindowA,NULL
        or      eax,eax
        jz      wmBYE
; Отправить данные получателю
mov r9d,FSize
shl r9,8
or r9,CF_TEXT
mov qword ptr[rsp+20h],SMTO_ABORTIFHUNG
mov qword ptr[rsp+28h],5000
and qword ptr[rsp+30h],0
invoke  SendMessageTimeoutA,eax,WM_USER+1,hWnddlg
jmp wmBYE
wmSEND_ICO:mov edx,p1
invoke FindResource,0,,RT_ICON; find the resource
mov hResource,rax
invoke  SizeofResource,0,eax ; get its size
mov FSize,eax
invoke LoadResource,0,hResource ; load the resource
invoke LockResource,eax;pResource
mov p,rax
;-------------------------------------------------
        mov ecx,256
and qword ptr[rsp+30h],LR_DEFAULTCOLOR;LR_DEFAULTCOLOR=0
mov [rsp+20h],rcx
mov [rsp+28h],rcx
mov edx,FSize
invoke  CreateIconFromResourceEx,p,,TRUE,30000h;270376,TRUE,30000h
mov [rsp+20h],rax
invoke  SendDlgItemMessageA,hWnddlg,IDC_IMG1,STM_SETIMAGE,IMAGE_ICON
xor p1,11b
mov edx,FSize
invoke ExeToDll,p,,CF_BITMAP
; Найти окно получателя
mov edx,offset szWin
        invoke  FindWindowA,NULL
        or      eax,eax
        jz      wmBYE
; Отправить данные получателю
mov r9d,FSize
shl r9,8
or r9,CF_BITMAP
mov qword ptr[rsp+20h],SMTO_ABORTIFHUNG
mov qword ptr[rsp+28h],5000
and qword ptr[rsp+30h],0
invoke  SendMessageTimeoutA,eax,WM_USER+1,hWnddlg
jmp wmBYE
wmSEND_WAV:mov ecx,offset wav_file
invoke CreateFile,,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,\
        FILE_ATTRIBUTE_ARCHIVE,0
        mov hFile,eax
        invoke GetFileSize,eax,0
mov FSize,eax
        invoke GlobalAlloc,GPTR,eax
mov p,rax
        lea r9d,szReadWrite
and qword ptr[rsp+20h],0
        invoke ReadFile,hFile,eax,FSize
        invoke CloseHandle,hFile
mov edx,FSize
invoke ExeToDll,p,,CF_WAVE
; Find recipient window
mov edx,offset szWin
        invoke  FindWindowA,NULL
        or      eax,eax
        jz      wmBYE
; Send data to recipient
mov r9d,FSize
shl r9,8
or r9,CF_WAVE
mov qword ptr[rsp+20h],SMTO_ABORTIFHUNG
mov qword ptr[rsp+28h],500
and qword ptr[rsp+30h],0
invoke  SendMessageTimeoutA,eax,WM_USER+1,hWnddlg
        jmp wmBYE
wmCOMMAND:cmp r8d,BN_CLICKED shl 16 + ID_SEND_TXT
        je      wmSEND_TXT
        cmp r8d,BN_CLICKED shl 16 + ID_SEND_ICO
        je      wmSEND_ICO
cmp r8d,BN_CLICKED shl 16 + ID_SEND_WAV
        je      wmSEND_WAV
cmp     r8d,BN_CLICKED shl 16 + IDCANCEL
        jne     wmBYE
wmCLOSE:invoke EndDialog,,0
wmBYE: xor eax,eax
leave
retn
DialogProc endp
;---------------------------------------
.data
szWin db 'DLL Reciever',0
wav_file db '..\Images\03.wav',0
p1 dd 1
end
resources (15s.rc)#include "resource.h"
#define ID_TXT 100
#define ID_SEND_TXT 101
#define ID_SEND_ICO 102
#define ID_SEND_WAV 103
#define IDC_IMG1 104
#define IDC_DIALOG 200
#define IDC_ICON1 500
IDC_ICON1 ICON "..\\Images\\icon1.ico"
IDC_ICON2 ICON "..\\Images\\icon2.ico"
IDC_DIALOG DIALOGEX 200, 0, 212, 154
STYLE DS_MODALFRAME | DS_CENTER | WS_CAPTION | WS_MINIMIZEBOX |
WS_SYSMENU | WS_VISIBLE | WS_OVERLAPPED | DS_MODALFRAME | DS_3DLOOK
CAPTION "DLL Sender"
{
CONTROL "",-1,BUTTON,BS_GROUPBOX | WS_CHILD | WS_VISIBLE,2,-1,207,24
CONTROL "",IDC_IMG1,STATIC,SS_ICON | WS_CHILD | WS_VISIBLE,3,40,128,128
CONTROL "Write something and click 'Send Text'",ID_TXT,EDIT,ES_LEFT |
ES_AUTOHSCROLL|WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP,5,7,199,13
CONTROL "Send Text", ID_SEND_TXT, BUTTON, BS_DEFPUSHBUTTON | WS_CHILD |
WS_VISIBLE | WS_TABSTOP, 149, 27, 60, 15
CONTROL "Send ICO", ID_SEND_ICO, BUTTON, BS_PUSHBUTTON | WS_CHILD |
WS_VISIBLE | WS_TABSTOP, 149, 45, 60, 15
CONTROL "Send WAV", ID_SEND_WAV, BUTTON, BS_PUSHBUTTON | WS_CHILD |
WS_VISIBLE | WS_TABSTOP, 149, 65, 60, 15
CONTROL "Exit",IDCANCEL,BUTTON,BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE |
WS_TABSTOP, 149, 85, 60, 15
}
Application client text (15r.asm); GUI #
include win64a.inc
includelib IPC.lib
ExeToDll PROTO :qword,:dword,:dword
DllToExe PROTO :qword,:dword,:dword
IDC_DIALOG equ 200
ID_TXT equ 100
ID_ICON equ 102
IDC_ICON1 equ 500
.code
WinMain proc dummy:qword
        invoke  DialogBoxParamA,IMAGE_BASE,IDC_DIALOG,HWND_DESKTOP,qword ptr(offset DialogProc),0
        invoke  RtlExitUserProcess,NULL
WinMain endp
DialogProc proc hWnddlg:qword,msg:qword,wParam:qword,lParam:qword
local p:qword
local FSize:dword
local lpwiocb:WAVEHDR
local hWaveOut:qword
mov hWnddlg,rcx
mov lParam,r9
cmp edx,WM_CLOSE
je wmCLOSE
cmp edx,WM_USER+1
je wmUSER_1
cmp edx,WM_COMMAND
je wmCOMMAND
        cmp edx,WM_INITDIALOG
jne wmBYE
wmINITDIALOG:;mov rcx,hWnd
; Найти окно получателя
        invoke  FindWindowA,0,&szWin
        or      eax,eax
        jnz wmBYE
invoke MessageBoxA,hWnddlg,&Error1,&szAppName,MB_ICONEXCLAMATION or MB_YESNO
cmp eax,IDNO
jz      wmBYE
invoke WinExec,&szService,SW_SHOW
jmp wmBYE
wmUSER_1:cmp r9b,CF_TEXT
je COPY_TEXT
cmp     r9b,CF_BITMAP; Is this the correct data type?
je COPY_ICON
        cmp     r9b,CF_WAVE
        jne wmBYE
shr r9,8
mov FSize,r9d
invoke DllToExe
mov p,rax
mov r8,p
;"RIFF"+(File size)+(File Type Header)+(Format chunk marker)+(Length of format data) = 14h
        add r8d,14h
        invoke waveOutOpen,&hWaveOut,WAVE_MAPPER,,0,0,WAVE_ALLOWSYNC
; Preparing the header for output
lea edi,lpwiocb
mov edx,edi
xor eax,eax
mov ecx,(sizeof WAVEHDR)/8
rep stosq
mov rax,p
;"RIFF"+(File size)+(File Type Header)+(Format chunk marker)+(Length of format data)+\
;(Type of format)+(Number of Channels)+(Sample Rate)+(Sample Rate*BitsPerSample*Channels/8)+\
;(BitsPerSample*Channels/8)+(Bits per sample)+("data" chunk header)+(Size of the data section)=2Ch
add rax,2Ch
mov [rdx].WAVEHDR.lpData,rax ;data block address
mov eax,FSize
sub eax,2Ch
mov [rdx].WAVEHDR.dwBufferLength,eax ;data block size
invoke waveOutPrepareHeader,hWaveOut,,sizeof WAVEHDR
; Start playing the block
invoke waveOutWrite,hWaveOut,&lpwiocb,sizeof WAVEHDR
@@: test lpwiocb.dwFlags,WHDR_DONE
jz @b
invoke waveOutUnprepareHeader,hWaveOut,&lpwiocb,sizeof WAVEHDR
invoke waveOutClose,hWaveOut
jmp wmBYE
COPY_TEXT:invoke DllToExe
invoke SetDlgItemTextA,hWnddlg,ID_TXT,eax
jmp wmBYE
COPY_ICON:shr r9,8
mov FSize,r9d
invoke DllToExe
; Create HICON directly from memory
invoke  CreateIconFromResourceEx,eax,FSize,TRUE,30000h,256,256,LR_DEFAULTCOLOR
invoke  SendDlgItemMessageA,hWnddlg,ID_ICON,STM_SETIMAGE,IMAGE_ICON,eax
jmp wmBYE
wmCOMMAND:cmp r8d,BN_CLICKED shl 16 + IDCANCEL
jne wmBYE
wmCLOSE:invoke EndDialog,,0
wmBYE: xor eax,eax
leave
retn
DialogProc endp
.data
szWin   db "DLL Sender",0
Error1   db "The server is not running.",10,"Run?",0
szAppName db "DLL Reciever",0
szService db "15s",0
ClassName db 'DLGCLASS',0
end
client application resources (15r.rc)#include "resource.h"
#define IDC_DIALOG 200
#define ID_TXT 100
#define ID_ICON 102
#define ID_PROGRESS 106
#define ID_TXT1 107
IDC_DIALOG DIALOGEX 0,0,212,154
STYLE DS_MODALFRAME | DS_CENTER | WS_CAPTION | WS_MINIMIZEBOX |
WS_SYSMENU | WS_VISIBLE | WS_OVERLAPPED | DS_MODALFRAME | DS_3DLOOK
CAPTION "DLL Reciever"
{
CONTROL "",-1,BUTTON,BS_GROUPBOX | WS_CHILD | WS_VISIBLE, 2,-3,207,24
CONTROL "", ID_TXT, STATIC, SS_LEFT | SS_NOPREFIX | WS_CHILD |
WS_VISIBLE | WS_BORDER | WS_TABSTOP, 5, 5, 199, 13
CONTROL "Exit", IDCANCEL, BUTTON, BS_DEFPUSHBUTTON | WS_CHILD |
WS_VISIBLE | WS_TABSTOP, 149, 25, 60, 15
CONTROL "",ID_ICON, STATIC, SS_ICON | WS_CHILD | WS_VISIBLE,3,40,128,128
}
IPC.dll text; DLL #
include win64a.inc
.code
DllMain proc hInstDLL:QWORD, reason:QWORD, unused:QWORD
     mov eax,TRUE
     leave
     ret
DllMain Endp
DllToExe proc
     lea rax,bufferDLL
     ret
DllToExe endp
ExeToDll proc buffer:qword,FSize:dword,type0:dword
     mov buffer,rcx
     mov FSize,edx
     lea rdi,bufferDLL
     mov ecx,FSize
     mov rsi,buffer
     rep movsb
     leave
     ret
ExeToDll endp
.data?
bufferDLL db 8000000 dup(?)
end
IPC.def textLIBRARY IPC
EXPORTS ExeToDll
EXPORTS DllToExe
in attachment ASM-/RC-/EXE-/DLL-/DEF-/LIB-files