Results 1 to 9 of 9

Thread: [Resovled] VirtualAllocEx help...

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    11

    [Resovled] VirtualAllocEx help...

    OK...I have no idea how to use this function in C#, I know it's possible...I'd really appreciate it if someone would show me how to SIMPLY do it...like in notepad...positive rating to all whom help. For those who don't know what it is..it is an API call used to allocate memory in an external process all allocated addresses have the memory value of 0 but they are non-static. Thanks in advanced...and if you have any questions or I didn't make my self clear (it's quite possible) let me know!
    Last edited by jackyChang; Jul 22nd, 2007 at 02:43 PM.

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2.0] VirtualAllocEx help...

    Hi there,
    You'll use the DllImportAttribute class to provide the information needed to call the function from the DLL. According to pinvoke.net the signature looks like this:
    Code:
    [DllImport("kernel32.dll")]
    static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress,
       uint dwSize, uint flAllocationType, uint flProtect);
    After that is in your class, you can call VirtualAllocEx, pass it the arguments and your off.

    A search for DllImport will turn up plenty of help. Good luck!

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    11

    Re: [2.0] VirtualAllocEx help...

    one last question...exactly how would i pass the hProcess argument when i call VirtualAllocEx? Is it possible for it to be process name?
    Edit:Grammatical errors...didn't see them when i was writing it the first time...
    Last edited by jackyChang; Jul 20th, 2007 at 10:38 PM.

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2.0] VirtualAllocEx help...

    The name won't work. You'll need an IntPtr to your process's handle and then you can pass that.

  5. #5
    Junior Member
    Join Date
    May 2004
    Posts
    26

    Re: [2.0] VirtualAllocEx help...

    For the first parameter of VirtualAllocEx, you need to pass a handle to an external process that has been opened for read/write via an OpenProcess call (search pinvoke.net for it as well).

    Generally, you will find the hWnd of a window using FindWindow, get its ProcessID using GetWindowThreadProcessId, call OpenProcess with the PID, and then pass the return of that OpenProcess call as the first parameter for VirtualAllocEx. All of these have to be imported ala nmadd's suggestion so look them up on pinvoke.net.

  6. #6
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2.0] VirtualAllocEx help...

    I haven't used VirtualAllocEx before, but if all you need is a handle of a process, you can try something like this:

    Code:
                Process[] p = Process.GetProcessesByName("firefox");
                if (p.Length > 0)
                {
                    IntPtr hFF = p[0].Handle;
                    MessageBox.Show("FireFox handle: " + hFF.ToString());
                }
    Last edited by nmadd; Jul 21st, 2007 at 11:37 PM. Reason: forgot a little semicolon

  7. #7
    Junior Member
    Join Date
    May 2004
    Posts
    26

    Re: [2.0] VirtualAllocEx help...

    If p[0] holds the ProcessID, that can be passed directly to OpenProcess without the need for a FindWindow or GetWindowThreadProcessId call. I forgot about the Process object.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    11

    Re: [2.0] VirtualAllocEx help...

    Quote Originally Posted by nmadd
    I haven't used VirtualAllocEx before, but if all you need is a handle of a process, you can try something like this:

    Code:
                Process[] p = Process.GetProcessesByName("firefox");
                if (p.Length > 0)
                {
                    IntPtr hFF = p[0].Handle;
                    MessageBox.Show("FireFox handle: " + hFF.ToString());
                }
    great! thanks that worked, if i could give two reps...i would, thanks. also thanks to the other guy...

  9. #9
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2.0] VirtualAllocEx help...

    Glad to help.

    If you're all done with this question, you can mark this thread as "Resolved" and let everybody know. Good luck with your project.

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