VB inter-process communication

Source: Internet
Author: User
ArticleDirectory
    • There are n methods for inter-process communication, common include DDE, memory file ing, pipelines, custom messages, winSock and so on. This article describes the communication between processes based on memory file ing.
There are n methods for inter-process communication, common include DDE, memory file ing, pipelines, custom messages, winSock and so on. This article describes the communication between processes based on memory file ing.

CreateProgramACodeAs follows:

Version 5.00:
Begin VB. Form form1
Caption = "form1"
Clientheight = 1, 3090
Clientleft = 60
Clienttop = 450
Clientwidth = 4680
Linktopic = "form1"
Scaleheight = 3090
Scalewidth = 4680
Startupposition = 3' window default
Begin VB. commandbutton command1
Caption = "command1"
Height = 555
Left = 1, 1170
Tabindex = 0
Maximum = 960
Width = 1245
End
End
Attribute vb_name = "form1"
Attribute vb_globalnamespace = false
Attribute vb_creatable = false
Attribute vb_predeclaredid = true
Attribute vb_exposed = false
Private declare function createfilemapping lib "Kernel32" alias "createfilemappinga" (byval hfile as long, lpfilemappigattributes as any, byval flprotect as long, byval dwmaximumsizehigh as long, byval ready as long, byval lpname as string) as long
Private declare function mapviewoffile lib "Kernel32" (byval hfilemappingobject as long, byval dwdesiredaccess as long, byval dwfileoffsethigh as long, byval dwfileoffsetlow as long, byval same as long) as long
Private declare function unmapviewoffile lib "Kernel32" (lpbaseaddress as any) as long
Private declare function closehandle lib "Kernel32" (byval hobject as long) as long
Private declare function getcurrentprocess lib "Kernel32" () as long
Private declare sub copymemory lib "Kernel32" alias "rtlmovememory" (destination as any, source as any, byval length as long)
Private declare function writeprocessmemory lib "Kernel32" (byval hprocess as long, lpbaseaddress as any, lpbuffer as any, byval nsize as long, lpnumberofbyteswritten as long) as long
Private declare function readprocessmemory lib "Kernel32" (byval hprocess as long, lpbaseaddress as any, lpbuffer as any, byval nsize as long, lpnumberofbyteswritten as long) as long

Private type mytype
Hfile as long
Strtmp (259) as byte
End type

private sub commandementclick ()
dim hmap as long, pmap as long, M as mytype, J as long, H as long, N as mytype, bytes (9) as byte
hmap = createfilemapping (getcurrentprocess, byval 0, 983071, 0,256, "mytest ")
If hmap <> 0 and hmap <>-1 then
M. hfile = 123
copymemory M. strtmp (0), byval "123", lenb ("123")

Pmap = mapviewoffile (hmap, 983071, 0, 0,256)
'Write the information to the shared address space. The following writeprocessmemory can also be used here.
Copymemory byval pmap, M, Len (m)
If pmap <> 0 then
'H = writeprocessmemory (getcurrentprocess, byval pmap, M, Len (M), J)
'Try to read the data, or use copymemory
'H = readprocessmemory (getcurrentprocess, byval pmap, N, Len (N), J)
End if
End if
End sub

 

Then create the source code of program B as follows:

Version 5.00:
Begin VB. Form form1
Caption = "form1"
Clientheight = 1, 3090
Clientleft = 60
Clienttop = 450
Clientwidth = 4680
Linktopic = "form1"
Scaleheight = 3090
Scalewidth = 4680
Startupposition = 3' window default
Begin VB. commandbutton command1
Caption = "command1"
Height = 795
Left = 1, 630
Tabindex = 0
Maximum = 690
Width = 1425
End
End
Attribute vb_name = "form1"
Attribute vb_globalnamespace = false
Attribute vb_creatable = false
Attribute vb_predeclaredid = true
Attribute vb_exposed = false
Private declare function createfilemapping lib "Kernel32" alias "createfilemappinga" (byval hfile as long, lpfilemappigattributes as any, byval flprotect as long, byval dwmaximumsizehigh as long, byval ready as long, byval lpname as string) as long
Private declare function openfilemapping lib "Kernel32" alias "openfilemappinga" (byval dwdesiredaccess as long, byval binherithandle as long, byval lpname as string) as long
Private declare function mapviewoffile lib "Kernel32" (byval hfilemappingobject as long, byval dwdesiredaccess as long, byval dwfileoffsethigh as long, byval dwfileoffsetlow as long, byval same as long) as long
Private declare function unmapviewoffile lib "Kernel32" (lpbaseaddress as any) as long
Private declare function closehandle lib "Kernel32" (byval hobject as long) as long
Private declare function getcurrentprocess lib "Kernel32" () as long
Private declare sub copymemory lib "Kernel32" alias "rtlmovememory" (destination as any, source as any, byval length as long)
Private declare function readprocessmemory lib "Kernel32" (byval hprocess as long, lpbaseaddress as any, lpbuffer as any, byval nsize as long, lpnumberofbyteswritten as long) as long
Private declare function OpenProcess lib "Kernel32" (byval dwdesiredaccess as long, byval binherithandle as long, byval dwprocessid as long) as long
Private type mytype
Hfile as long
Strtmp (259) as byte
End type

Private sub commandementclick ()
Dim hmap as long, pmap as long, M as mytype, PM as long
Dim h as long
PM = varptr (m)
Hmap = openfilemapping983071, 0, "mytest ")
If hmap <> 0 and hmap <>-1 then
Pmap = mapviewoffile (hmap, 983071 0, 0, Len (m ))
If pmap <> 0 then
Copymemory M, byval pmap, Len (m)

end if
end sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.