Jump to content
Sign in to follow this  
Followers 0

[VB.NET] Memory Address

10 posts in this topic

Posted

I have a good handle on vb.net programming, but im planning to expand my knowledge.

I feel like fooling around with some code, could someone shed some light on how I would fool around with memory address's on a process.

Share this post


Link to post
Share on other sites

Posted

There was something known as the VarPtr function in VB (actually, in the BASIC language itself), but after some quick Googling, it isn't found in VB.NET, most likely due to the fact that it is "unmanaged". You might try digging something up along the lines of a VB.NET VarPtr replacement, however. For example, I found something with System.Runtime.InteropServices.GCHandle as well as something called BitConverter. I'm out of my element when it comes to .NET, but I figured that might help to get you started at least. Those last two seem wrong for memory, but I didn't look them up so they might be able to manipulate memory, despite their names.

Share this post


Link to post
Share on other sites

Posted

I wouldn't suggest messing with a .NET process' memory (due to CRL and other unpredictable aspects) so you should probably stick with other dummy processes and I'm not sure if .NET offers memory reading APIs either, so you'd probably be best using WinAPI in this:

Just look into OpenProcess, ReadProcessMemory, WriteProcessMemory, etc.

Share this post


Link to post
Share on other sites

Posted

[quote name='x0r' post='589295475' date='Mar 26 2008, 23:34']I wouldn't suggest messing with a .NET process' memory (due to CRL and other unpredictable aspects) so you should probably stick with other dummy processes and I'm not sure if .NET offers memory reading APIs either, so you'd probably be best using WinAPI in this:

Just look into OpenProcess, ReadProcessMemory, WriteProcessMemory, etc.[/quote]


ReadProcessMemory, how would I do that?
Maybe something like this?


[code]Imports System
Imports System.Diagnostics
Imports System.ComponentModel

Sub ReadMem()
Dim myProcesses As Process = Process.GetProcessesByName(pinball)
Dim memRead as String = myProcesses.ReadProcessMemory[/code]

Anything close to what your thinking?

Share this post


Link to post
Share on other sites

Posted

VB.NET doesn't natively supported memory pointers and allocations, but this can be accomplished through .NET Framework classes like IntPtr, Marshal and GCHandle. There are probably other types as well, but I've never used them.

IntPtr is used as a pointer to a platform-specific integer. Marshal can read and write data at a specific memory location (leveraging IntPtr's).

Example:[code]Dim pointer as IntPtr
Dim number as Integer = 20
pointer = Marshal.AllocHGlobal(4)
Marshal.WriteInt32(pointer, number)[/code]

Example:[code]Dim anotherInteger as Integer = Marshal.ReadInt32(pointer)[/code]

Example:[code]Dim message as String = "I know, I'm awesome..."
Dim pointer as IntPtr = Marshal.StringToHGlobalAuto(message)[/code]

Example:[code]Dim anotherMessage as String = Marshal.PtrToStringAuto(pointer)[/code]

You can see that the Marshal class acts as a broker between the program code and the memory operation.

You can also use the Marshal class on VB.NET Structures:

Example:[code]Public Structure MyStruct
Dim x as Integer
Dim y as Integer
End Structure

Dim structure1 as MyStruct
structure1.x = 1
structure1.y = 2

Dim pointer = Marshal.AllocHGlobal(Marshal.SizeOf(structure1))
Marshal.StructureToPtr(structure1, pointer, True)

Dim structure2 as MyStruct = Marshal.PtrToStructure(pointer, New MyStruct().GetType)[/code]

Objects are handled differently, because of their Garbage Handling requirements (they are Managed Types). You can use a GCHandle to read and write objects.

Example:[code]Public Class MyClass
Public Name As String
Public Age as Integer
End Class

Dim handle as GCHandle
Dim person as MyClass
person.Name = "Matthew Abbott"
person.Age = 24

handle = GCHandle.Alloc(person)[/code]

Example:[code]Dim person2 as MyClass = handle.Target
handle.Free()[/code]

You should check out the System.Runtime.InteropServices namespace on MSDN and see what you can come up with.

Hope that helps.

Adapted from: [url="http://www.dotnetbips.com/articles/44bad06d-3662-41d3-b712-b45546cd8fa8.aspx"]DotNetBips.com[/url]

Share this post


Link to post
Share on other sites

Posted

wow, thank you SOOOOOO much.

Share this post


Link to post
Share on other sites

Posted

Since I can't edit my post, sorry for the double post.

--> Would you also happen to know how to access WINAPI functions in vb.net?

Share this post


Link to post
Share on other sites

Posted

The WINAPI functionality is accessed in a similar way to the C# alternative. This is known as P/Invoke (Platform Invoke), which allows managed code to invoke unmanaged code residing in libraries on the Host computer.

In VB.NET, to declare a WINAPI function, you do the following (e.g. CopyMemory):[code]Public Static Class WINAPI
Declare Static Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDst As IntPtr, ByVal pSrc As String, ByVal length As Long)
End Class[/code]

In the above example, I have declared a static class, so we can access our WINAPI functions from a single location, without instantiation, ie., you would call WINAPI.CopyMemory(...).

You should look into P/Invoke for more information on using external unmanaged code from .NET.

Also, check out [url="http://www.pinvoke.net"]http://www.pinvoke.net[/url] for more information.

Share this post


Link to post
Share on other sites

Posted

Does this code works for two different applications? I mean if I alloc one object in the first application, pass the memory address to the other, is the other able to start another object (same instance) pointing to the same object. Can we use the same object in different applications? If not what would be the best way to do so? Thank you very much in advance, it's been a long time I'm trying to make it work, thanks again.

Share this post


Link to post
Share on other sites

Posted

I would leave inter-application communication to something like IPC, TCP/UDP sockets, etc... the newer WCF (Windows Communication Foundation) classes make this so much easier.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.