Results 1 to 2 of 2

Thread: Trouble using VirtualAllocEx

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2001
    Location
    New Zealand
    Posts
    5

    Unhappy Trouble using VirtualAllocEx

    I'm using a tip I found at Nullsoft, where you can allocate some memory in a remote process, write to it from the other process, and then retrieve it back into another process - my VB app.

    Bit tricky - and I am a little out of my depth here. The reason I'm doing it this complex way is because this call:

    pBase = SendMessageLong(hWndWinamp%, WM_USER, 0&, IPC_GETPLAYLISTFILE)

    Only works for plugins for winamp - ie. they must exist in the same process.

    So here's the plan - according to the tip I got.
    Turns out it's working fine up to the point where we do the VirtualAllocEx. I'm finding the window, getting the thread and opening the process ok, but the V.AllocEx is returning 0.

    Public Sub getCurrentFilename()

    Dim ret As Long
    Dim dwProcessId As Long
    Dim hWndWinamp As Integer
    Dim hProcess As Long
    Dim pWinampBuf As Long
    Dim pBase As Long
    Dim szBuf As String
    Dim temp As Long

    hWndWinamp% = FindWindow(winampClass, 0)
    'Find winamp

    ret = GetWindowThreadProcessId(hWndWinamp%, dwProcessId&)
    'Get its process

    hProcess& = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, dwProcessId&)
    'Open it up

    pWinampBuf = VirtualAllocEx(hProcess&, Null, 10&, MEM_RESERVE Or MEM_COMMIT, PAGE_READWRITE)
    'Allocate some space there

    pBase = SendMessageLong(hWndWinamp%, WM_USER, 0&, IPC_GETPLAYLISTFILE)
    'Do our winamp command

    ret = ReadProcessMemory(hProcess&, pBase&, szBuf$, 4&, temp&)
    'read it back into the VB app.

    'Free it as well - but that's not in here yet!
    End Sub

    Here's my declares:

    Declare Function FindWindow Lib "user32" Alias _
    "FindWindowA" (ByVal lpClassName As String, _
    ByVal lpWindowName As Long) As Long

    Declare Function SendMessageLong Lib "user32" Alias _
    "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, ByVal lParam As Long) As Long

    Public Declare Function GetWindowThreadProcessId Lib "user32" _
    (ByVal hwnd As Long, lpdwProcessId As Long) As Long

    Declare Function OpenProcess Lib "kernel32" _
    (ByVal dwDesiredAccess As Long, _
    ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long

    Public Declare Function VirtualAllocEx Lib "kernel32" _
    (ByVal hProcess As Long, lpAddress As Any, dwSize As Any, _
    ByVal flAllocationType As Long, ByVal flProtect As Long) As Long

    Public Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, _
    lpBaseAddress As Long, lpBuffer As String, ByVal nSize As Long, _
    lpNumberOfBytesWritten As Any) As Long

    Public Const winampClass = "Winamp v1.x"

    ' Windows Const
    Public Const WM_USER = &H400

    Public Const IPC_GETPLAYLISTFILE As Long = 211


    Public Const PROCESS_VM_OPERATION = &H8&
    Public Const PROCESS_VM_READ = &H10&
    Public Const PROCESS_VM_WRITE = &H206

    Public Const MEM_RESERVE = &H2000
    Public Const MEM_COMMIT = &H1000
    Public Const PAGE_READWRITE = &H8

    Please help me!

  2. #2

    Thread Starter
    New Member
    Join Date
    May 2001
    Location
    New Zealand
    Posts
    5
    Looks like nobody's got any ideas for this one - I really would like some help though.

    Has anyone every used the VirtualAllocEx API call before? If so, in what way and what context? I'm sure there's something simple I've got wrong here

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