乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 24|回复: 1

[转帖]在程序之间传送数据

[复制链接]

26

主题

589

帖子

10

银币

中流砥柱

Rank: 25

铜币
693
发表于 2004-3-19 22:48:00 | 显示全部楼层 |阅读模式
Pass String Data Between Applications Using SendMessage
Step-by-Step Example
[ol]
  • Convert the string to a byte array using the CopyMemory() API.
  • Obtain the address of the byte array using the VarPtr() intrinsic function and copy the address and length of the byte array into a COPYDATASTRUCT structure.
  • Pass the COPYDATASTRUCT to another application using the WM_COPYDATA message, setting up the other application to receive the message.
  • Unpack the structure on the target system using CopyMemory(), and convert the byte array back to a string using the StrConv() intrinsic function.
    [/ol]
    The next section shows you how to create a sample program that demonstrates passing string data from one application to another.
    Steps to Create the Sample
    To create this sample, you will create two separate projects; a sending project and a target project.
    Create the target application:
    [ol]
  • Start a new Standard EXE project in Visual Basic. Form1 is created by default. This project will be your target application.
  • Add a Label control to Form1.
  • Copy the following code to the Code window of Form1:       Private Sub Form_Load()
              gHW = Me.hWnd
              Hook
              Me.Caption = "Target"
              Me.Show
              Label1.Caption = Hex$(gHW)
          End Sub      Private Sub Form_Unload(Cancel As Integer)
              Unhook
          End Sub
  • Add a module to the project and paste the following code in the Module1 code window:       Type COPYDATASTRUCT
                  dwData As Long
                  cbData As Long
                  lpData As Long
          End Type      Public Const GWL_WNDPROC = (-4)
          Public Const WM_COPYDATA = &H4A
          Global lpPrevWndProc As Long
          Global gHW As Long      'Copies a block of memory from one location to another.
          Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
             (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)      Declare Function CallWindowProc Lib "user32" Alias _
             "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As _
             Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As _
             Long) As Long      Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
             (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As _
             Long) As Long      Public Sub Hook()
              lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, _
              AddressOf WindowProc)
              Debug.Print lpPrevWndProc
          End Sub      Public Sub Unhook()
              Dim temp As Long
              temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
          End Sub      Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, _
             ByVal wParam As Long, ByVal lParam As Long) As Long
              If uMsg = WM_COPYDATA Then
                  Call mySub(lParam)
              End If
              WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, _
                 lParam)
          End Function      Sub mySub(lParam As Long)
              Dim cds As COPYDATASTRUCT
              Dim buf(1 To 255) As Byte          Call CopyMemory(cds, ByVal lParam, Len(cds))          Select Case cds.dwData
               Case 1
                  Debug.Print "got a 1"
               Case 2
                  Debug.Print "got a 2"
               Case 3
                  Call CopyMemory(buf(1), ByVal cds.lpData, cds.cbData)
                  a$ = StrConv(buf, vbUnicode)
                  a$ = Left$(a$, InStr(1, a$, Chr$(0)) - 1)
                  Form1.Print a$
              End Select
          End Sub
  • Save the project and minimize the Visual Basic IDE.
    [/ol]
    Create the Sending Application:
    [ol]
  • Start a second instance of the Visual Basic IDE and create a new Standard EXE project in Visual Basic. Form1 is created by default.
  • Add a CommandButton to Form1.
  • Copy the following code to the Code window of Form1:       Private Type COPYDATASTRUCT
                  dwData As Long
                  cbData As Long
                  lpData As Long
          End Type      Private Const WM_COPYDATA = &H4A      Private Declare Function FindWindow Lib "user32" Alias _
             "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName _
             As String) As Long      Private Declare Function SendMessage Lib "user32" Alias _
             "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal _
             wParam As Long, lParam As Any) As Long      'Copies a block of memory from one location to another.
          Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
             (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)      Private Sub Command1_Click()
              Dim cds As COPYDATASTRUCT
              Dim ThWnd As Long
              Dim buf(1 To 255) As Byte      ' Get the hWnd of the target application
              ThWnd = FindWindow(vbNullString, "Target")
              a$ = "It Works!"
          ' Copy the string into a byte array, converting it to ASCII
              Call CopyMemory(buf(1), ByVal a$, Len(a$))
              cds.dwData = 3
              cds.cbData = Len(a$) + 1
              cds.lpData = VarPtr(buf(1))
              i = SendMessage(ThWnd, WM_COPYDATA, Me.hwnd, cds)
          End Sub      Private Sub Form_Load()
          ' This gives you visibility that the target app is running
          ' and you are pointing to the correct hWnd
              Me.Caption = Hex$(FindWindow(vbNullString, "Target"))
          End Sub
  • Save the project.
    [/ol]
    Running the Sample:
    [ol]
  • Restore the target application and press the F5 key to run the project. Note that the value of the hWnd displayed in the label.
  • Restore the sending application and press the F5 key to run the project. Verify that the hWnd in the form caption matches the hWnd in the label on the target application. Click the CommandButton and the text message should be displayed on the form of the target application. [/ol]
  • 回复

    使用道具 举报

    12

    主题

    135

    帖子

    5

    银币

    初露锋芒

    Rank: 3Rank: 3Rank: 3

    铜币
    183
    发表于 2004-3-21 19:29:00 | 显示全部楼层
    你的程序很好,不过看懂的好像不多。
    回复

    使用道具 举报

    发表回复

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    • 微信公众平台

    • 扫描访问手机版

    • 点击图片下载手机App

    QQ|关于我们|小黑屋|乐筑天下 繁体中文

    GMT+8, 2025-8-15 13:04 , Processed in 1.056459 second(s), 57 queries .

    © 2020-2025 乐筑天下

    联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表