Results 1 to 1 of 1

Thread: Callback from other app

  1. #1

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    34

    Callback from other app

    Hi!
    For copying text from RichTextBox I used copy to clipboard and read out, but It's not a stable method, it is not working on every PC!
    Is it possible to use EM_STREAMOUT from other app?
    It uses a callback but It cant locate my callback address.
    I'm stuck here:
    vb Code:
    1. Option Explicit
    2. Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
    3. Private Declare Function VirtualAllocEx Lib "kernel32.dll" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
    4. Private Declare Function VirtualFreeEx Lib "kernel32.dll" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
    5. Private Declare Function WriteProcessMemory Lib "kernel32.dll" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
    6. Private Declare Function ReadProcessMemory Lib "kernel32.dll" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
    7. Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
    8. 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
    9. Private Const WM_USER = &H400
    10. Private Const EM_STREAMIN = (WM_USER + 73)
    11. Private Const EM_STREAMOUT = (WM_USER + 74)
    12. Private Const SF_TEXT = &H1
    13. Private Const SF_RTF = &H2
    14. Private Const SF_UNICODE = &H10         ' Unicode data of some kind
    15. Private Const PROCESS_VM_OPERATION As Long = &H8
    16. Private Const PROCESS_VM_READ As Long = &H10
    17. Private Const PROCESS_VM_WRITE As Long = &H20
    18.  
    19. Private Const MEM_COMMIT As Long = &H1000
    20. Private Const MEM_RELEASE As Long = &H8000&
    21.  
    22. Private Const PAGE_READWRITE As Long = &H4
    23.  
    24. Private Type EDITSTREAM
    25.     dwCookie As Long
    26.     dwError As Long
    27.     pfnCallback As Long
    28. End Type
    29.  
    30. Private Sub Command1_Click()
    31. Dim es As EDITSTREAM
    32. Dim pStreamMemory As Long
    33. Dim ProcessID As Long
    34. Dim phandle As Long
    35.     hwnd= 'Write here the hwnd or use FindWindowEx api to locate the richtextbox
    36.     GetWindowThreadProcessId hwnd, ProcessID
    37.     phandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, ProcessID)
    38.     pStreamMemory = VirtualAllocEx(phandle, 0, 12, MEM_COMMIT, PAGE_READWRITE)
    39.     If pStreamMemory = 0 Then Stop
    40.     es.dwCookie = 55 'put your own data in here, it is passed to the callback function
    41.     es.pfnCallback = GetAddress(AddressOf EditStreamCallback)
    42.     Call WriteProcessMemory(phandle, pStreamMemory, es, 12, 0)
    43.     call SendMessage(hwnd, EM_STREAMOUT, SF_RTF, byval pStreamMemory)
    44.     Call VirtualFreeEx(phandle, pStreamMemory, 12, MEM_RELEASE)
    45.     Call CloseHandle(phandle)
    46.  
    47. End Sub
    In a bas module:
    vb Code:
    1. Option Explicit
    2. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    3.  
    4. Public Function EditStreamCallback(ByVal dwCookie As Long, ByVal pbBuff As Long, ByVal cb As Long, ByVal pcb As Long) As Long
    5. Dim bArr() As Byte
    6. Dim strBuff As String
    7.  
    8.     ReDim bArr(cb - 1)
    9.     CopyMemory bArr(0), ByVal pbBuff, cb
    10.     ' the data is in the byte array
    11.     ' do with it what you want
    12.     ' probably you want to save it to a file,
    13.     ' but for the example I put it in a string
    14.     strBuff = StrConv(bArr, vbUnicode)
    15. End Function
    16.  
    17. Public Function GetAddress(ByVal lAddress As Long) As Long
    18.     GetAddress = lAddress
    19. End Function
    Last edited by Sajty; Jul 9th, 2007 at 10:18 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width